Shared CoreData between apps

夙愿已清 提交于 2020-01-03 06:19:32

问题


My question somewhat similar to How to have multiple apps - one Core Data?. But I am not unable to replicate as suggested in the answer.

I have two applications. One applications (1st App) allows user to do all sort of things and save in coredata.

Other application (2nd App) is a service application. Here I want the service to get notified everytime the coredata is updated(any changes like create, delete, update done).

If I use following notification in 2nd App, this notification does not get fired:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(modelUpdated)
                                             name:NSManagedObjectContextDidSaveNotification
                                           object:nil];

If I had an UI based application or even a background application in that case I can use NSDistributedNotification.

But I want something better than distributed notifications.

Please give me some hints so that I can move ahead.

NOTE: This app is not going through AppStore, so Sandboxing doesn't come under consideration.


回答1:


If you are going to distribute your app yourself, you can do almost anything you want.

You can certainly attach a PSC in each application to the same SQLite file. No problem there.

As for knowing when the file has been modified, you can use kqueue or dispatch_source to monitor when the file has changed. However, just knowing that it has changed does not tell you what has changed, meaning your watcher process will need to refetch to get its MOC updated.

If this is sufficient, then it is probably the easiest route to take.

If you need more granular notification, then there is no getting around having to notify any interested parties what specifically has changed in the store. You can roll your own, use XPC, or simply use NSDistributedNotification (remember the latter is neither safe, nor guaranteed).

What you should do is observe NSManagedObjectContextDidSaveNotification on any MOC that is directly attached to the PSC. In that handler, you will create a notification similar to the one you received. Except, you should merely send sets of object IDs in URI representation.

Now, your observers can get detailed information about what objects were inserted, updated, and deleted.

Again, if you don't want to go through all this, you can use traditional OS mechanisms to observe that the file changed, and just refetch. If you choose this route, one thing you can do to help... Keep a "last modified" date for each object, and index on that attribute. Then, you can at least query objects that have changed since the last time you loaded the database. There are a number of other options to use here... the basic idea is that if you monitor via the OS, you only get told that something changed... you have to figure out what... if that matters.

For sandboxed apps, one of the few solutions available is to share data via an XPC launched daemon.

EDIT

distributedNotification I don't want to use, actually this wont work for a deomon/service app. And your other point MOCDidSaveNoti is not observed at all. I tried both before posting this question. – Anoop Vaidya

Of course NSManagedObjectContextDidSaveNotification will not tell you what has changed in another process. It has no idea what changed, and has no idea about MOCs in other processes. I am sorry I wrote so poorly to make you think it would.

That notification is to be observed in any MOC that is changing the data store, for the sole purpose of propagating that information to other processes. The notification handler should then send a remote notification (however you want to do it... SHM, pipe, message queue, XPC, smoke signal, etc), with the object IDs of all the objects that have changed.

Any process that wants to know about the changed data store will then need to watch for the remote notification (however you choose to send it).

It really does not matter what you want to do... you are limited by what's available.

You have two basic choices:

  1. observe a general change notification that the store has changed (kqueue, dispatch_source, etc). However, all you know is that the store changed, meaning that you will have to perform a complete refetch.

  2. Send a remote notification whenever the store is saved, passing the object IDs of what has changed, and have other processes watch for that remote notification and update their MOC accordingly.



来源:https://stackoverflow.com/questions/23653687/shared-coredata-between-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!