0

I am building sync functionality between an iPad and a web server. I'm using an approach pretty close to the one described here. I only have one type of object, let's called it a Story, that has to be synchronized. It is a Core Data entity (managed object).

The remaining problem I have to solve is knowing "whenever something changes and needs to be synchronized to the server." One approach would be to go find every piece of code that modifies a Story and have it also set some needsSyncing flag. That does not seem elegant and it seems that over time, developers could forget to update the flag for new types of modification.

Do Core Data objects have a way to observe themselves, so any time any change is made, a particular method is executed? That would make this pretty easy.

Another option might be using the isUpdated method right before doing a save operation on the managed object context. You'd either have to have save called in only one place or do this at every place you save (sounds like the first option). I guess I could make a helper method that goes through all Story objects right before saving and see if any of them need their flag to be set. The only drawback to that is that I'd be traversing all Story objects in the system for any save, even for saves that have nothing to do with a Story.

Anyway I'll stop trying to guess the solution out loud - does anyone have experience with a good way to do this?

Community
  • 1
  • 1
Greg Smalter
  • 6,571
  • 9
  • 42
  • 63

1 Answers1

3

SDK has you covered. See the NSManagedObjectContext class reference, at the very end of the page, the MOC will post notifications that you can subscribe to, including NSManagedObjectContextObjectsDidChangeNotification. You can listen for these and do the update call pretty much coincident with saving the MOC.

danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks. This worked better than the willSave I was starting to try, mainly because I can stop observing it when I do a full sync. – Greg Smalter May 02 '12 at 19:29