0
votes

I am trying to figure out how to design a core-data setup.

I have one NSManagedObjectContext that runs in the main thread which is getting updated by another background thread(it has its own context) updating the main context by using the NSManagedObjectContextDidSaveNotification callback: This part of the system works fine.

My problem is when trying to save user data and how to send an immediately request to the web server for saving this data, while still adding it to the main context. The entity object is important because the request for saving the user data on the server is generated by looking at its attributes. And this entity object should be correctly updated if it has successfully updated the data on the web-server (from the background context to the main context).

So for example if i would create a new NSEntityDescription linked to the main context, do i listen to the NSManagedObjectContextDidSaveNotification and then create a background thread for which i would create a new context and send the entity object id. This would then update the main context and setting the object entity to sent?

1
Please show rather than describe your code. Thank you. - aqua
My question is more a design setup - what code do i show for that? - Architect

1 Answers

0
votes

Make the change to your managedObjects in your main thread's mananagedObjectContext (but don't save your moc), send off a request to the server to make the change (using something like NSURLConnection -sendAsynchronous request), in your completion handler on success fire off your own NSNotification to alert your controller it's now safe to save the moc. If it fails, you could alert the user, use core data's undo, or something along those lines.