How do I share a Core Data store between processes using NSDistributedNotifications?

前端 未结 6 1958
星月不相逢
星月不相逢 2020-12-29 15:24

Background

I\'ve already posted a question about the basics of sharing a Core Data store between processes.

I\'m trying to implement the rec

相关标签:
6条回答
  • 2020-12-29 15:58

    You're looking for - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag I believe.

    This will refresh the object with the info in the persistent store, merging changes if you want.

    0 讨论(0)
  • 2020-12-29 16:01

    This works, except for sandboxes apps. You can't send a notification with a user info dict. Instead consider some other IPC like XPC or DO.

    On a side note, using NSDustributedNotificationCenter is not always 100% if the system is busy.

    0 讨论(0)
  • 2020-12-29 16:02

    I had this exact same issue with an iPhone app that I've been working on. In my case, the solution involved setting the Context's stalenessInterval to something suitably infinitesimal (e.g., 0.5 seconds).

    0 讨论(0)
  • 2020-12-29 16:02

    Setting stalenessInterval of managed object context works. My case involves multiple threads instead of process though.

    0 讨论(0)
  • 2020-12-29 16:12

    I used the method in

    http://www.mlsite.net/blog/?p=518

    then every object is correctly faulted but the faults are fetch in cache so still no update

    I had to do [moc stalenessInterval = 0];

    And it finally worked, with relationship.

    0 讨论(0)
  • 2020-12-29 16:17

    I'd go with Mike's suggestion and just watch the store file for changes.

    Though it may not be the most efficient, I've had success using - [NSManagedObjectContext reset] from a second process when there's a change to a store. In my case case, the code is fairly linear — all I do is run a fetch request for some data after resetting. I don't know how this will work with bindings and a complicated UI, but you may be able to post a notification to manually update things if it's not handled automatically.

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