部分保存managedObjectContext [英] Save managedObjectContext partly
问题描述
我已经在中有3个事务
: A
, B
, C
。
如何只保存第一个 A
事务,但在上下文中保留未保存的 B
, C
。
How can I save only the first A
transaction, but keep unsaved B
, C
on context.
插入A
,插入B
,插入C
, commit A
,
不幸的是 NSManagedObjectContext
code> commit A ,但保存所有事务。
Unfortunately NSManagedObjectContext
can not do commit A
, but save all the transactions.
推荐答案
也许我错过了一些东西。你的目标是保存一个特定的对象,例如 A
,只有当 A
的数据被提交的服务器。我错了吗?
Maybe I missing something. Your goal is to save a specific object, say A
, only and only if A
's data has been committed to the server. Am I wrong?
如果是这样,您可以利用 NSOperation
类。将您的工作(保存到持久存储或对服务器的提交)包装到 NSOperation
子类中。在它们之间添加依赖关系。
If this is the case, you can take advantage of NSOperation
class. Wrap your work (a save to the persistent store or a commit to the server) into an NSOperation
subclass. Add a dependency between them.
只是一个例子。
let serverOperation : NSOperation = ...
let localOperation : NSOperation = ...
localOperation.addDependency(serverOperation)
let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperations([serverOperation, localOperation], waitUntilFinished: false)
另一种方法是,提交操作被视为唯一的交易。
Another way could be that the save and commit operations are considered as a unique transaction.
让我知道如果它不清楚。
Let me know if something it's not clear.
这篇关于部分保存managedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!