expose DetachedDataTransaction's context and allow creating children detached transactions (https://github.com/JohnEstropia/CoreStore/issues/9)

This commit is contained in:
John Rommel Estropia
2015-07-26 21:20:48 +09:00
parent 2dae2ae39c
commit 1c6085ad82
2 changed files with 31 additions and 2 deletions

View File

@@ -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
)
)
}
}

View File

@@ -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