Chris
Chris

Reputation: 91

iOS8 Extension : synchronize CoreData with containing app

I am working on a Widget that will display some info that are also present in my application. I shared my CoreData store between the app extension and containing app without issue.

Now, when saving data to the persistent store from my Widget, I need to catch the event in my app to merge the NSManagedObjectContext (and vice versa) If I don't, trying to save the context from the containing app after the Widget save makes the app crash.

Registering to NSManagedObjectContextDidSaveNotification do not help here.

Someone can clue me in on how to do that ?

Upvotes: 2

Views: 607

Answers (1)

Richard
Richard

Reputation: 31

Having worked on this I think the best way is to reset the context on your app extension on ViewDidLoad and then refetch all of your NSManagedObject.

To update the app, I reset the context on didBecomeActive but only at times when the app extension has told the app it needs to update. This can be done by passing a bool through your app groups NSUserDefaults. By doing this you don't reset the context when it is not necessary and you therefore don't lose performance every time your app is opened.

Upvotes: 3

Related Questions