Welcome, Guest
Username Password: Remember me

Core Data, persisting new objects
(1 viewing) (1) Guest

TOPIC: Core Data, persisting new objects

Core Data, persisting new objects 1 year, 8 months ago #1

Hi. I recently started using Sensible table views with Core Data. One thing I can't figure out is why it does not always persist newly added objects. I have found that in both the core data sample that the app does not persist new objects added with the + button all the time. In my app I did manage to get it to save a couple but was unsure why it saved those and not the others. I did not that once I got them in there they saved properly as I have set up the delegate to save after an item is edited or added. Any idea why new objects are not persisting?

- (void)tableViewModel:(SCTableViewModel *)tableViewModel itemEditedForSectionAtIndexPath:(NSIndexPath *)indexPath item:(NSObject *)item{

[DataSource sharedManager] save];
}

- (void)tableViewModel:(SCTableViewModel *)tableViewModel itemAddedForSectionAtIndexPath:(NSIndexPath *)indexPath item:(NSObject *)item{

[DataSource sharedManager] save];
}
  • codezy
  • OFFLINE
  • Fresh Boarder
  • Posts: 3
  • Karma: 1

Re: Core Data, persisting new objects 1 year, 8 months ago #2

Disregard that. It appears to be working fine now although I am not sure what I changed! Perhaps validation was stopping the save, I do not know.
  • codezy
  • OFFLINE
  • Fresh Boarder
  • Posts: 3
  • Karma: 1

Re: Core Data, persisting new objects 1 year, 8 months ago #3

Hi Codezy
Any idea what caused objects not to save? I have same problem and I can't figure out what is causing it. I just tried original Core Data sample app and it won't work either?
BR/jsaarinen
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0

Re: Core Data, persisting new objects 1 year, 8 months ago #4

How are you guys persisting your data? On iOS 4, both of these methods should be implemented:

 
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSError *error = nil;
if (managedObjectContext_ != nil) {
if ([managedObjectContext_ hasChanges] && ![managedObjectContext_ save:&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.
*/

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
 
NSError *error = nil;
if (managedObjectContext_ != nil) {
if ([managedObjectContext_ hasChanges] && ![managedObjectContext_ save:&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.
*/

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
 


Have you guys also tested this on an actual device or on the simulator?
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
Last Edit: 1 year, 8 months ago by tarekskr.

Re: Core Data, persisting new objects 1 year, 8 months ago #5

Hi Tarek
That part seems to be okay; in Core Data sample app and in my own code. I have ran apps only on simulator.
BR/jsaarinen
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0

Re: Core Data, persisting new objects 1 year, 8 months ago #6

Hi Tarek
I tried this in device and when application is run from XCode objects are not saved, but when I start application from device itself objects are persistent. It seems that same goes for simulator, starting applications in XCode (build and run) will not only result non-persistent objects, but it will also clear all the previously saved objects.

It's not the end of the world, but it sure makes testing bit harder.

BR/jsaarinen
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0
Time to create page: 1.79 seconds