diff --git a/CoreStore/Saving and Processing/DataStack+Transaction.swift b/CoreStore/Saving and Processing/DataStack+Transaction.swift index 250add9..12c096c 100644 --- a/CoreStore/Saving and Processing/DataStack+Transaction.swift +++ b/CoreStore/Saving and Processing/DataStack+Transaction.swift @@ -62,7 +62,7 @@ public extension DataStack { } /** - Begins a non-contiguous transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms. A detached transaction object should typically be only used from the main queue. + Begins a non-contiguous transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms. - returns: a `DetachedDataTransaction` instance where creates, updates, and deletes can be made. */ @@ -70,6 +70,10 @@ public extension DataStack { return DetachedDataTransaction( mainContext: self.rootSavingContext, - queue: .Main) + queue: .createSerial( + "com.coreStore.dataStack.detachedTransactionQueue", + targetQueue: .UserInitiated + ) + ) } } diff --git a/CoreStore/Saving and Processing/DetachedDataTransaction.swift b/CoreStore/Saving and Processing/DetachedDataTransaction.swift index 63ac19c..d636b7a 100644 --- a/CoreStore/Saving and Processing/DetachedDataTransaction.swift +++ b/CoreStore/Saving and Processing/DetachedDataTransaction.swift @@ -36,6 +36,18 @@ public final class DetachedDataTransaction: BaseDataTransaction { // MARK: Public + /** + Returns the `NSManagedObjectContext` for this detached transaction. Use only for cases where external frameworks need an `NSManagedObjectContext` instance to work with. + + Note that it is the developer's responsibility to ensure the following: + - that the `DetachedDataTransaction` that owns this context should be strongly referenced and prevented from being deallocated during the context's lifetime + - that all saves will be done either through the `DetachedDataTransaction`'s `commit(...)` method, or by calling `save()` manually on the context, its parent, and all other ancestor contexts if there are any. + */ + public var internalContext: NSManagedObjectContext { + + return self.context + } + /** Saves the transaction changes asynchronously. For a `DetachedDataTransaction`, multiple commits are allowed, although it is the developer's responsibility to ensure a reasonable leeway to prevent blocking the main thread. @@ -50,6 +62,19 @@ public final class DetachedDataTransaction: BaseDataTransaction { } } + /** + Begins a child transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms. + + - returns: a `DetachedDataTransaction` instance where creates, updates, and deletes can be made. + */ + public func beginDetached() -> DetachedDataTransaction { + + return DetachedDataTransaction( + mainContext: self.context, + queue: self.transactionQueue + ) + } + // MARK: Internal