问题
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