Refresh NSManagedObjectContext

后端 未结 1 1743
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 20:37

I am writing a WatchKit extension for an iPhone application. I share Core Data using App Groups. This is working, the data is shared. When a new NSManagedObject

相关标签:
1条回答
  • 2021-01-06 21:21

    The simple way is just to have the app reload its data. Re-do any fetches so that you get the latest data from the persistent store.

    If you want to make it more sophisticated, do something like this:

    In the watch extension, for every new/changed/deleted object,

    • Call objectID to get the NSManagedObjectID
    • Convert the object ID to a string URIRepresentation
    • Pass these strings in the MMWormhole message

    In the app, when receiving the message,

    • Use [NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:] to convert the strings back to an NSManagedObjectID
    • Use [NSManagedObjectContext existingObjectWithID:] to get the managed object corresponding to the object ID.

    Now you know which objects need refreshing.

    0 讨论(0)
提交回复
热议问题