应用程序之间共享CoreData [英] Shared CoreData between apps

查看:122
本文介绍了应用程序之间共享CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点类似于如何拥有多个应用程序 - 一个核心数据?。但我不能像答案中建议的那样复制。



我有两个应用程序。一个应用程序(第一个应用程序)允许用户做所有类型的事情并保存在coredata中。



其他应用程序(第二个应用程序)是一个服务应用程序。在这里,我想让服务获得通知每次coredata更新(任何更改,如创建,删除,更新完成)。



如果我在第二个应用程式中使用下列通知,则此通知不会被触发:

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

如果我有一个基于UI的应用程序,甚至是后台应用程序,我可以使用 NSDistributedNotification



但我想要的东西比分布式通知更好。



请给我一些提示,以便我可以继续前进。



这个应用程序没有通过AppStore,所以沙盒没有考虑。

解决方案

如果你打算自己发布你的应用程序,你几乎可以做任何你想要的。



你可以附加PSC在每个应用程序到同一个SQLite文件。



至于知道文件何时被修改,可以使用kqueue或dispatch_source监视文件何时更改。但是,只是知道它已更改并不会告诉您更改了什么,这意味着您的观察者进程需要重新获取更新其MOC。



如果这是足够的,那么它可能是最简单的路线。



如果你需要更详细的通知,那么就没有必要通知任何感兴趣的派对什么在商店有什么具体改变。你可以自己滚动,使用XPC,或者使用 NSDistributedNotification (记住后者既不安全也不保证)。



您应该做的是在直接附加到PSC的任何MOC上观察 NSManagedObjectContextDidSaveNotification 。在该处理程序中,您将创建一个类似于您收到的通知。除非,您只需要以URI表示形式发送对象ID集合。



现在,您的观察者可以获得有关插入,更新和删除对象的详细信息。再次,如果你不想经历所有这些,你可以使用传统的操作系统机制来观察文件改变,只是重新获取。如果你选择这个路由,你可以做一件事帮助...保持每个对象的最后修改日期,并索引该属性。然后,您可以至少查询自上次加载数据库以来发生更改的对象。有一些其他的选择,在这里使用...基本的想法是,如果你通过操作系统监视,你只能得到告诉一些改变...你必须弄清楚什么...如果这很重要。 p>

对于沙盒应用程序,可用的几种解决方案之一是通过XPC启动的守护程序共享数据。



EDIT


distributedNotification我不想使用,实际上这不会工作
deomon / service app。你的另一个点MOCDidSaveNoti不是
。我试过两个发布这个问题。 - Anoop
Vaidya


当然 NSManagedObjectContextDidSaveNotification 什么在另一个过程中发生了变化。它不知道什么改变了,并且不知道在其他过程中的MOCs。对不起,我写得很差,让你以为它会这样。



这个通知要在改变数据存储的任何MOC中遵守,将该信息传播到其他进程。然后通知处理程序应该发送一个远程通知(但是你想要做它... SHM,管道,消息队列,XPC,烟雾信号等),与所有已更改的对象的对象ID。



任何想了解更改的数据存储的进程都需要注意远程通知(但您选择发送)。


$ b



你有两个基本的选择:


  1. 观察商店已更改的一般更改通知(kqueue,dispatch_source等)。


  2. 每当保存商店时发送远程通知,您必须执行完整的重新提取。传递已更改的对象ID,并让其他进程监视该远程通知并相应地更新其MOC。



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.

解决方案

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.

这篇关于应用程序之间共享CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆