简体   繁体   English

在核心数据的上下文之间共享非持久对象?

[英]Sharing Non-Persistent Objects Between Contexts in Core Data?

I was wondering if there is a way to share an NSManagedObject between two or more NSManagedObjectContext objects running in the same thread. 我想知道是否有一种方法可以在同一线程中运行的两个或多个NSManagedObjectContext对象之间共享NSManagedObject

I have the following problem: I have one main context shared through all my code in the application and several different contexts that are created for each remote fetch request that I issue. 我有以下问题:我在应用程序中的所有代码中共享了一个主上下文,并且为我发出的每个远程获取请求创建了多个不同的上下文。 (I created a custom class that fetches remotely and inserts all the objects found in the server in his own NSManagedObjectContext ). (我创建了一个自定义类,该类可以远程获取并将在服务器中找到的所有对象插入其自己的NSManagedObjectContext )。 Those fetch requests may run simultaneously since they use NSURLConnection objects that may end at different times. 这些获取请求可以同时运行,因为它们使用的NSURLConnection对象可能在不同的时间结束。 If the same remote object gets fetched by different connections, I will end up with duplicates at the moment of saving and merging the context with the main one. 如果通过不同的连接获取同一远程对象,那么在保存上下文并将其与主要对象合并时,我将得到重复。 (That is, objects that have the same remote ID but a different objectID ). (即,具有相同远程ID但具有不同objectID )。

One possible solution would be to save (and so persist) every object as soon as it is created but I can't do that because it may have some relationships that may still have not been filled and won't validate during the save operation. 一种可能的解决方案是在创建每个对象后立即保存(并永久保存),但我不能这样做,因为它可能有些关系可能尚未填充,并且在保存操作期间无法验证。

I'm really looking forward to a method that allows you to share the same non-persistent instance of an object between context. 我真的很期待一种允许您在上下文之间共享对象的相同非持久实例的方法。 If anybody has encountered this issue and came up with a solution, I would be pleased to know! 如果有人遇到此问题并提出解决方案,我将很高兴知道!

Context cannot communicate between each other save through their stores. 上下文无法通过存储相互之间进行通信。 However, you can insert a managed object with a nil managed object context and it will be independent (albeit without relationships) of any context. 但是,您可以插入一个具有零个托管对象上下文的托管对象,并且它将独立于任何上下文(尽管没有关系)。 You could pass that independent managed object around however you wished and insert it into a context when you needed to persist it. 您可以随意传递该独立的受管对象,并在需要持久化该对象时将其插入上下文。 This is dangerous but possible. 这很危险,但可能。

However, if you're not running each connection on a separate thread then you don't gain anything by having multiple context. 但是,如果您没有在单独的线程上运行每个连接,那么通过拥有多个上下文就不会有任何收获。 Each connection object will activate its delegate in sequence on the main thread. 每个连接对象将在主线程上依次激活其委托。 In this case, your easiest solution would be to use the same delegate for all the connections and let the delegate handle the insertions into a single context. 在这种情况下,最简单的解决方案是对所有连接使用相同的委托,并让委托在单个上下文中处理插入。 To prevent duplication, just do a fetch on the remoteID and see if you get back an extant object before inserting a new object for that remoteID. 为了防止重复,只需在remoteID上进行访存,然后查看是否在为该remoteID插入新对象之前取回现有对象。

I don't think what you want to do is possible. 我认为您想做的事是不可能的。 I mean if you want to share changes between different contexts, you got to use notifications and merge it whenever did save or did change occur. 我的意思是,如果您想在不同的上下文之间共享更改,则必须使用通知并在保存或发生更改时将其合并。 But in your case, I'd say just use 1 context and save in the end. 但是对于您而言,我想说的只是使用1个上下文并保存到最后。 Or a less elegant way: save all the remote ids temporary in your app and check before inserting new ones. 或一种不太优雅的方法:将所有远程ID临时保存在您的应用中,并在插入新ID之前进行检查。 In this case, you can continue use multiple contexts and save after each didfinishloading. 在这种情况下,您可以继续使用多个上下文并在每次didfinishloading之后保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从内存角度来看,具有非持久性存储的NSArray和Core Data如何工作? - How does NSArray and Core Data with a non-persistent store work from a memory perspective? 创建核心日期实体实例,但不希望将其存储(非持久性) - Create a Core Date Entity Instance But not want it to be stored(non-persistent) 商店之间的核心数据共享对象 - core data sharing objects between stores 将非持久性变量添加到nsmangedobject - adding non-persistent variables to nsmangedobject 尽管objectID字符串比较正常,但MagicalRecord + Core Data在上下文之间找不到对象 - MagicalRecord + Core Data not finding objects between contexts despite objectID string comparison working fine 核心数据:具有多个上下文的独立持久性存储与具有单个上下文的独立持久性存储 - Core Data: Separate Persistent Stores with Multiple Contexts vs. Separate Persistent Stores with Single Context 创建临时CoreData实体(在非持久性MagicalRecord上下文中)? - Create temporary CoreData entities (in a non-persistent MagicalRecord context)? 有没有办法在进程之间共享Core Data存储? - Is there a way of sharing a Core Data store between processes? iPhone应用程序之间的核心数据存储共享 - Core Data Store Sharing between iPhone Apps 用于在对象之间共享数据的单例模式 - Singleton pattern for sharing data between objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM