How can I tell whether an `NSManagedObject` has been deleted?

前端 未结 5 1531
[愿得一人]
[愿得一人] 2020-11-27 11:06

I have an NSManagedObject that has been deleted, and the context containing that managed object has been saved. I understand that isDeleted returns

5条回答
  •  北海茫月
    2020-11-27 11:30

    Due to my recent experience implementing iCloud in my iOS app that relies on Core Data for persistence, I realized that the best way is observing the framework's notifications. At least, better than relying on some obscure methods that may, or may not tell you if some managed object was deleted.

    For 'pure' Core Data apps you should observe NSManagedObjectContextObjectsDidChangeNotification on the main thread. The notification's user info dictionary contains sets with the managed objects' objectIDs that were inserted, deleted and updated.

    If you find your managed object's objectID in one of these sets, then you can update your application and UI in some nice way.

    That's it... for more information, give a chance to Apple's Core Data Programming Guide, Concurrency with Core Data chapter. There's a section "Track Changes in Other Threads Using Notifications", but don't forget to check the previous one "Use Thread Confinement to Support Concurrency".

提交回复
热议问题