Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-13-2011, 05:14 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default CoreData: Save possible in AppDelegate and not in ViewController

Hi Folks,

I use the following code to save data into CoreData from my AppDelegate when the App is starting:

Code:
 NSManagedObjectContext *context = [self managedObjectContext];
        
        Stamm *stamm = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"Stamm" 
                                   inManagedObjectContext:context];
        stamm.vendor = @"Vendor";
According to that I have in my AppDelegate:

Code:
/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSManagedObjectContext *) managedObjectContext {
	
    if (managedObjectContext != nil) {
        return managedObjectContext;
    }
	
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {
	
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}




/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
	
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
	
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"A1HandyFahrschein.sqlite"]];
	
	NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
		/*
		 Replace this implementation with code to handle the error appropriately.
		 
		 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
		 
		 Typical reasons for an error here include:
		 * The persistent store is not accessible
		 * The schema for the persistent store is incompatible with current managed object model
		 Check the error message to determine what the actual problem was.
		 */
		NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
		abort();
    }    
	
    return persistentStoreCoordinator;
}
When I am trying to save from a ViewController (I am using the same code as above in my ViewController Class" the Data will not be saved.

What I am missing?

Thanks and BR,

Stefan
StefanL is offline   Reply With Quote
Old 11-14-2011, 02:26 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by StefanL View Post
When I am trying to save from a ViewController (I am using the same code as above in my ViewController Class" the Data will not be saved.
Well, you don't actually have any code shown in your post to save the data. All you show is inserting a new object, but never saving the changes.

Also, it's not clear what you mean when you say you are using the same code as above in your view controller. Can you post the code from your view controller?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 11-30-2011, 01:58 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 4
p0sitan0 is on a distinguished road
Default CoreData: Save possible in AppDelegate and not in ViewController

Hi StefanL and dljeffery,
I'm working on my first App and am attempting to implement Core Data in a UIViewcontroller while following guides and sample code that use a UITableviewcontroller (Apple's books and recipes sample Core Data Apps), and I am experiencing PERHAPS(?) a similar problem as you have:

I run similar "boiler plate" code as yours in the AppDelegate and successfully read, add, and change data in the delegate. However in the UIViewcontroller code, before I get a chance to read or change any data, my App aborts in viewDidLoad, where I'm running this code:

NSManagedObjectContext *context = [self managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// My debugging attempt here produces "null" values (!!!) on the console for each of these logs:
NSLog(@"Context: %@",context);
NSLog(@"PS Coord : %@",context.persistentStoreCoordinator);
NSLog(@"MOM : %@", context.persistentStoreCoordinator.managedObjectMo del);

// And then there here my program aborts with the error message just below:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activations" inManagedObjectContext:[self managedObjectContext]];
// Error message on the console:
"+entityForName: could not locate an NSManagedObjectModel for entity name"


I am wondering that I don't know what UIViewcontroller needs to do to make use what was set up in the App Delegate?

If you have found a solution I would be very interested to learn what you did!
Thank you,
Bill
p0sitan0 is offline   Reply With Quote
Old 11-30-2011, 03:33 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Sounds like you just never set a value for managedObjectContext on your controller instance after creating the instance and before pushing/displaying it.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 12-02-2011, 12:36 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 4
p0sitan0 is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
Sounds like you just never set a value for managedObjectContext on your controller instance after creating the instance and before pushing/displaying it.
Thank you, I realize now I could use the App Delegate in the instance and added 2 lines just before the fetch call, which solved the problem, thank you again:

managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
context = managedObjectContext;

Best regards,
Bill
p0sitan0 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
17 members and 373 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, chiataytuday, dre, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, mbadegree, n00b, pbart, QuantumDoja, Retouchable, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0