From c15afcb381cdb46d3d85c0cd0624df8e598e4a3b Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Tue, 24 May 2016 00:40:13 +0900 Subject: [PATCH] Allow flushing UnsafeDataTransactions to notify its ListMonitors and ObjectMonitors without saving changes to the DataStack (fixes #71) --- .../UnsafeDataTransaction.swift | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/CoreStore/Saving and Processing/UnsafeDataTransaction.swift b/CoreStore/Saving and Processing/UnsafeDataTransaction.swift index 0ef1b94..008431c 100644 --- a/CoreStore/Saving and Processing/UnsafeDataTransaction.swift +++ b/CoreStore/Saving and Processing/UnsafeDataTransaction.swift @@ -91,6 +91,30 @@ public final class UnsafeDataTransaction: BaseDataTransaction { self.context.undo() } + /** + Immediately flushes all pending changes to the transaction's observers. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data. + + - Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`) + - throws: an error thrown from `closure`, or an error thrown by Core Data (usually validation errors or conflict errors) + */ + public func flush() throws { + + try self.context.save() + } + + /** + Flushes all pending changes to the transaction's observers at the end of the `closure`'s execution. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data. + + - Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`) + - parameter closure: the closure where changes can be made prior to the flush + - throws: an error thrown from `closure`, or an error thrown by Core Data (usually validation errors or conflict errors) + */ + public func flush(@noescape closure: () throws -> Void) throws { + + try closure() + try self.context.save() + } + /** Redo's the last undone change to the transaction. */ @@ -122,7 +146,7 @@ public final class UnsafeDataTransaction: BaseDataTransaction { /** Returns the `NSManagedObjectContext` for this unsafe 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: + - Important: Note that it is the developer's responsibility to ensure the following: - that the `UnsafeDataTransaction` 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 `UnsafeDataTransaction`'s `commit(...)` method, or by calling `save()` manually on the context, its parent, and all other ancestor contexts if there are any. */