UA-17470720-3

Jump to content


Photo
- - - - -

Core Data, persisting new objects


  • Please log in to reply
24 replies to this topic

#1 Paul T

Paul T

    Starting Out

  • STV 2.0
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 10 September 2010 - 06:08 AM

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];
}

#2 Paul T

Paul T

    Starting Out

  • STV 2.0
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 10 September 2010 - 08:46 AM

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.

#3 Juha Saarinen

Juha Saarinen

    Jr. Member

  • STV 2.0
  • PipPip
  • 11 posts
Reputation: 0
Neutral

Posted 11 September 2010 - 05:41 AM

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

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 11 September 2010 - 05:55 AM

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?

#5 Juha Saarinen

Juha Saarinen

    Jr. Member

  • STV 2.0
  • PipPip
  • 11 posts
Reputation: 0
Neutral

Posted 11 September 2010 - 06:21 AM

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

#6 Juha Saarinen

Juha Saarinen

    Jr. Member

  • STV 2.0
  • PipPip
  • 11 posts
Reputation: 0
Neutral

Posted 11 September 2010 - 10:37 PM

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

#7 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 12 September 2010 - 05:04 AM

Are you able to send us (to our support email) a project that is not saving? Also, what Xcode version are you using? Thanks a lot jsaarinen.

If anyone is also experiencing this please let me know, thanks guys!

#8 Juha Saarinen

Juha Saarinen

    Jr. Member

  • STV 2.0
  • PipPip
  • 11 posts
Reputation: 0
Neutral

Posted 12 September 2010 - 07:18 AM

Hi Tarek
It's easy because I have this problem with out of box Core Data sample application. I'm using Xcode version 3.2.4.
BR/jsaarinen

#9 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 10 October 2010 - 01:38 PM

I am running into this problem as well in both my application and the sample. 1.6RC.

#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 October 2010 - 02:45 PM

I am guessing that you're quitting the simulator before actually quitting the app, thus the app never gets the chance to save the Core Data changes. Please tap the home button on the simulator first before closing it, and all should save well. Also, everything should be working perfectly on the device itself. Hope this helps :)

#11 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 10 October 2010 - 02:58 PM

Is there a way to invoke a save manually? Or are you saying that on the device, it will save right after you add?

#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 October 2010 - 03:09 PM

Sure, to save manually, you just need to call NSManagedObject's "save" method.

On the device (and simulator), the changes are saved at two places:

1- applicationWillTerminate: This usually happens in pre iOS 4.0 operating systems when the user taps the home button.

2- applicationDidEnterBackground: This happens when an application is sent to the background as part of multitasking (iOS 4).

No matter what OS you're using, one of these methods is guaranteed to get fired when you tap the home button. For more on this, please have a look at these methods in the Core_Data_AppAppDelegate.m file in our Core Data sample application. The implementation for these methods have been taken right out of Apple's Core Data template.

#13 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 10 October 2010 - 03:12 PM

OK, gotcha. I just didn't know if SCTableViewModel had its own method/property to do this.

Thanks!

#14 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 10 October 2010 - 03:39 PM

I have added the following to the Core_Data_App sample's TasksViewController (and also added to the .h), but it is not saving at all. Once I remove this (thus saving in the delegate), it does not save. I also tested this on the device.

It doesn't make a lot of sense. I have extensive use of Core Data in my app and it is having the same problem. Is SCTableViewModel creating a new entity at the time of cell creation or at a later time?


- (void)tableViewModel:(SCTableViewModel *) tableViewModel  didInsertRowAtIndexPath:(NSIndexPath *) indexPath 
{
	// Get managedObjectContext from application delegate
	NSManagedObjectContext *managedObjectContext = [(Core_Data_AppAppDelegate *)[UIApplication sharedApplication].delegate managedObjectContext];
		
	NSError *error = nil;
	if (managedObjectContext != nil) {
		if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
	
			NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
			abort();
		} 
	}
}


#15 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 October 2010 - 03:48 PM

Would you please place a breakpoint at both applicationWillTerminate and applicationDidEnterBackground and then tap the home button to see if they're being called at all? (please use our sample app and do a clean build) If they're being called, are there any errors produced? NSLog should get called if any error occurs.

#16 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 10 October 2010 - 05:37 PM

applicationDidEnterBackground did get called and there were no errors produced. I did a clean also.

#17 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 11 October 2010 - 02:57 AM

Hi Tommy,

Again with the original sample application, in applicationDidEnterBackground, would you please check the value [managedObjectContext hasChanges] before the save method is called? (e.g. BOOL hasChanges = [managedObjectContext hasChanges];). I'll also send you our latest RC version just in case. Thanks!

#18 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 11 October 2010 - 03:21 AM

hasChanges = NO

I also tried this with the 1.6RC2.

I have checked the SQLite DB just to be sure it is not saving (using SQLite Manager add-on for Firefox) and there is no data being saved.

#19 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 11 October 2010 - 03:57 AM

If hasChanges is FALSE, then, as per the code in applicationDidEnterBackground, the save method won't get called.

This is how I am doing the test:



Are we both doing the same thing here? Also, would you please give me some info about the Xcode & SDK versions you're using? Thanks.

#20 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 11 October 2010 - 04:32 AM

It works as you have demonstrated if I take the save code out of:

"- (void)tableViewModel:(SCTableViewModel *) tableViewModel didInsertRowAtIndexPath:(NSIndexPath *) indexPath"


But, I would like to save at the time of insert if possible. When I have the save code in both the delegate method above and in the applicationDidEnterBackground method, it doesn't save at all.


I am not able to get it to save at all in my own application, but there may be other issues there.

I am using XCode 3.2.4 and the 4.1 SDK.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users