Save managedObjectContext partly

♀尐吖头ヾ 提交于 2019-12-12 01:22:57

问题


I have 3 transaction already in NSManagedObjectContext: A, B, C. They were performed / added to context in the same order.

How can I save only the first A transaction, but keep unsaved B, C on context.

insert A, insert B, insert C, commit A, ..

Unfortunately NSManagedObjectContext can not do commit A, but save all the transactions.


回答1:


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?

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.

Just an example.

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.



来源:https://stackoverflow.com/questions/30479793/save-managedobjectcontext-partly

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