WIP: CSImportableObject

This commit is contained in:
John Estropia
2016-03-25 17:57:26 +09:00
parent 707445a169
commit 1ff635d8b5
14 changed files with 670 additions and 148 deletions

View File

@@ -36,12 +36,12 @@ import CoreData
public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
/**
Saves the transaction changes. This method should not be used after the `commit()` method was already called once.
Saves the transaction changes. This method should not be used after the `-commitWithCompletion:` method was already called once.
- parameter completion: the block executed after the save completes. Success or failure is reported by the `CSSaveResult` argument of the block.
*/
@objc
public func commit(completion: ((result: CSSaveResult) -> Void)?) {
public func commitWithCompletion(completion: ((result: CSSaveResult) -> Void)?) {
self.swift.commit { (result) in
@@ -49,133 +49,93 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
}
}
// /**
// Begins a child transaction synchronously where NSManagedObject creates, updates, and deletes can be made. This method should not be used after the `commit()` method was already called once.
//
// - parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
// - returns: a `SaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
// */
// public func beginSynchronous(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to begin a child transaction from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to begin a child transaction from an already committed \(typeName(self))."
// )
//
// return SynchronousDataTransaction(
// mainContext: self.context,
// queue: self.childTransactionQueue,
// closure: closure).performAndWait()
// }
//
//
// // MARK: BaseDataTransaction
//
// /**
// Creates a new `NSManagedObject` with the specified entity type.
//
// - parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
// - returns: a new `NSManagedObject` instance of the specified entity type.
// */
// public override func create<T: NSManagedObject>(into: Into<T>) -> T {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to create an entity of type \(typeName(T)) from an already committed \(typeName(self))."
// )
//
// return super.create(into)
// }
//
// /**
// Returns an editable proxy of a specified `NSManagedObject`. This method should not be used after the `commit()` method was already called once.
//
// - parameter object: the `NSManagedObject` type to be edited
// - returns: an editable proxy for the specified `NSManagedObject`.
// */
// @warn_unused_result
// public override func edit<T: NSManagedObject>(object: T?) -> T? {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to update an entity of type \(typeName(object)) from an already committed \(typeName(self))."
// )
//
// return super.edit(object)
// }
//
// /**
// Returns an editable proxy of the object with the specified `NSManagedObjectID`. This method should not be used after the `commit()` method was already called once.
//
// - parameter into: an `Into` clause specifying the entity type
// - parameter objectID: the `NSManagedObjectID` for the object to be edited
// - returns: an editable proxy for the specified `NSManagedObject`.
// */
// @warn_unused_result
// public override func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to update an entity of type \(typeName(T)) from an already committed \(typeName(self))."
// )
//
// return super.edit(into, objectID)
// }
//
// /**
// Deletes a specified `NSManagedObject`. This method should not be used after the `commit()` method was already called once.
//
// - parameter object: the `NSManagedObject` type to be deleted
// */
// public override func delete(object: NSManagedObject?) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entity of type \(typeName(object)) from an already committed \(typeName(self))."
// )
//
// super.delete(object)
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter object1: the `NSManagedObject` type to be deleted
// - parameter object2: another `NSManagedObject` type to be deleted
// - parameter objects: other `NSManagedObject`s type to be deleted
// */
// public override func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entities from an already committed \(typeName(self))."
// )
//
// super.delete(([object1, object2] + objects).flatMap { $0 })
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter objects: the `NSManagedObject`s type to be deleted
// */
// public override func delete<S: SequenceType where S.Generator.Element: NSManagedObject>(objects: S) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entities from an already committed \(typeName(self))."
// )
//
// super.delete(objects)
// }
/**
Begins a child transaction synchronously where `NSManagedObject` creates, updates, and deletes can be made. This method should not be used after the `-commitWithCompletion:` method was already called once.
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
- returns: a `CSSaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
*/
@objc
public func beginSynchronous(closure: (transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
return bridge {
self.swift.beginSynchronous { (transaction) in
closure(transaction: transaction.objc)
}
}
}
// MARK: BaseDataTransaction
/**
Creates a new `NSManagedObject` with the specified entity type.
- parameter into: the `CSInto` clause indicating the destination `NSManagedObject` entity type and the destination configuration
- returns: a new `NSManagedObject` instance of the specified entity type.
*/
@objc
public override func createInto(into: CSInto) -> NSManagedObject {
return self.swift.create(into.swift)
}
/**
Returns an editable proxy of a specified `NSManagedObject`. This method should not be used after the `-commitWithCompletion:` method was already called once.
- parameter object: the `NSManagedObject` type to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
@objc
@warn_unused_result
public override func editObject(object: NSManagedObject?) -> NSManagedObject? {
return self.swift.edit(object)
}
/**
Returns an editable proxy of the object with the specified `NSManagedObjectID`. This method should not be used after the `-commitWithCompletion:` method was already called once.
- parameter into: a `CSInto` clause specifying the entity type
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
@objc
@warn_unused_result
public override func editInto(into: CSInto, objectID: NSManagedObjectID) -> NSManagedObject? {
return self.swift.edit(into.swift, objectID)
}
/**
Deletes a specified `NSManagedObject`. This method should not be used after the `-commitWithCompletion:` method was already called once.
- parameter object: the `NSManagedObject` type to be deleted
*/
@objc
public override func deleteObject(object: NSManagedObject?) {
self.swift.delete(object)
}
/**
Deletes the specified `NSManagedObject`s.
- parameter objects: the `NSManagedObject`s type to be deleted
*/
@objc
public override func deleteObjects(objects: [NSManagedObject]) {
self.swift.delete(objects)
}
// MARK: CoreStoreBridge
internal typealias SwiftType = AsynchronousDataTransaction
internal override var swift: AsynchronousDataTransaction {
return super.swift as! AsynchronousDataTransaction

View File

@@ -120,7 +120,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
// MARK: Inspecting Pending Objects
/**
Returns all pending `NSManagedObject`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s that were inserted to the transaction. This method should not be called after the `-commit*:` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were inserted to the transaction.
*/
@@ -132,7 +132,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObject`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s of the specified type that were inserted to the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were inserted to the transaction.
@@ -145,7 +145,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s that were inserted to the transaction. This method should not be called after the `-commit*:` method was called.
- returns: an `NSSet` of pending `NSManagedObjectID`s that were inserted to the transaction.
*/
@@ -157,7 +157,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s of the specified type that were inserted to the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were inserted to the transaction.
@@ -170,7 +170,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObject`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s that were updated in the transaction. This method should not be called after the `-commit*:` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were updated to the transaction.
*/
@@ -182,7 +182,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObject`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s of the specified type that were updated in the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were updated in the transaction.
@@ -195,7 +195,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s that were updated in the transaction. This method should not be called after the `-commit*:` method was called.
- returns: an `NSSet` of pending `NSManagedObjectID`s that were updated in the transaction.
*/
@@ -207,7 +207,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s of the specified type that were updated in the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were updated in the transaction.
@@ -220,7 +220,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObject`s that were deleted from the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s that were deleted from the transaction. This method should not be called after the `-commit*:` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were deleted from the transaction.
*/
@@ -232,7 +232,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObject`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObject`s of the specified type that were deleted from the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were deleted from the transaction.
@@ -245,7 +245,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
@@ -258,7 +258,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `-commit*:` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
@@ -290,6 +290,8 @@ public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
// MARK: CoreStoreBridge
internal typealias SwiftType = BaseDataTransaction
public required init(_ swiftObject: BaseDataTransaction) {
self.swiftObject = swiftObject

View File

@@ -2,11 +2,32 @@
// CSCoreStore+Setup.swift
// CoreStore
//
// Created by John Rommel Estropia on 2016/03/17.
// Copyright © 2016 John Rommel Estropia. All rights reserved.
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CSCoreStore
public extension CSCoreStore {

View File

@@ -24,7 +24,6 @@
//
import Foundation
import CoreData
// MARK: - CSCoreStore
@@ -38,7 +37,7 @@ public final class CSCoreStore: NSObject {
/**
The default `CSDataStack` instance to be used. If `defaultStack` is not set before the first time accessed, a default-configured `DataStack` will be created.
Changing the `defaultStack` is thread safe, but it is recommended to setup `DataStacks` on a common queue (e.g. the main queue).
Changing the `defaultStack` is thread safe, but it is recommended to setup stacks on a common queue (e.g. the main queue).
*/
@objc
public class var defaultStack: CSDataStack {

View File

@@ -23,6 +23,9 @@
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CSError

View File

@@ -0,0 +1,53 @@
//
// CSImportableObject.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CSImportableObject
@objc
public protocol CSImportableObject: class, AnyObject {
/**
Return `true` if an object should be created from `source`. Return `false` to ignore and skip `source`. The default implementation returns `true`.
- parameter source: the object to import from
- parameter transaction: the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
- returns: `true` if an object should be created from `source`. Return `false` to ignore.
*/
@objc
static func shouldInsertFromImportSource(source: AnyObject, inTransaction transaction: CSBaseDataTransaction) -> Bool
/**
Implements the actual importing of data from `source`. Implementers should pull values from `source` and assign them to the receiver's attributes. Note that throwing from this method will cause subsequent imports that are part of the same `importObjects(:sourceArray:)` call to be cancelled.
- parameter source: the object to import from
- parameter transaction: the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
*/
@objc
func didInsertFromImportSource(source: AnyObject, inTransaction transaction: CSBaseDataTransaction) throws
}

View File

@@ -36,7 +36,7 @@ import CoreData
public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreBridge {
/**
Initializes an SQLite store interface from the given SQLite file URL. When this instance is passed to the `CSDataStack`'s `addStorage()` methods, a new SQLite file will be created if it does not exist.
Initializes an SQLite store interface from the given SQLite file URL. When this instance is passed to the `CSDataStack`'s `-addStorage*:` methods, a new SQLite file will be created if it does not exist.
- parameter fileURL: the local file URL for the target SQLite persistent store. Note that if you have multiple configurations, you will need to specify a different `fileURL` explicitly for each of them.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration. Note that if you have multiple configurations, you will need to specify a different `fileURL` explicitly for each of them.
@@ -57,7 +57,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreBridge {
}
/**
Initializes an SQLite store interface from the given SQLite file name. When this instance is passed to the `CSDataStack`'s `addStorage()` methods, a new SQLite file will be created if it does not exist.
Initializes an SQLite store interface from the given SQLite file name. When this instance is passed to the `CSDataStack`'s `-addStorage*:` methods, a new SQLite file will be created if it does not exist.
- Warning: The default SQLite file location for the `CSLegacySQLiteStore` and `CSSQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `CSLegacySQLiteStore` instead of `CSSQLiteStore`.
- parameter fileName: the local filename for the SQLite persistent store in the "Application Support/<bundle id>" directory (or the "Caches/<bundle id>" directory on tvOS). Note that if you have multiple configurations, you will need to specify a different `fileName` explicitly for each of them.

View File

@@ -35,6 +35,104 @@ import CoreData
@objc
public final class CSSaveResult: NSObject, CoreStoreBridge {
/**
`true` if the `commit` operation for the transaction succeeded, either because the save succeeded or because there were no changes to save. Returns `false` to indicate failure.
*/
@objc
public var isSuccess: Bool {
return self.swift.boolValue
}
/**
`true` if the `commit` operation for the transaction failed, or `false` otherwise. When `true`, the `error` property returns the actual `NSError` for the failure.
*/
@objc
public var isFailure: Bool {
return !self.swift.boolValue
}
/**
`true` if the `commit` operation for the transaction succeeded and if there was an actual change made. Returns `false` otherwise.
*/
@objc
public var hasChanges: Bool {
guard case .Success(let hasChanges) = self.swift else {
return false
}
return hasChanges
}
/**
The `NSError` for a failed `commit` operation, or `nil` if the `commit` succeeded
*/
@objc
public var error: NSError? {
guard case .Failure(let error) = self.swift else {
return nil
}
return error.objc
}
/**
If the result was a success, the `success` block is executed with a `BOOL` argument that indicates if there were any changes made. If the result was a failure, the `failure` block is executed with an `NSError` argument pertaining to the actual error.
The blocks are executed immediately as `@noescape` and will not be retained.
- parameter success: the block to execute on success. The block passes a `BOOL` argument that indicates if there were any changes made.
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actuall error.
*/
@objc
public func handleSuccess(@noescape success: (hasChanges: Bool) -> Void, @noescape failure: (error: NSError) -> Void) {
switch self.swift {
case .Success(let hasChanges):
success(hasChanges: hasChanges)
case .Failure(let error):
failure(error: error.objc)
}
}
/**
If the result was a success, the `success` block is executed with a `BOOL` argument that indicates if there were any changes made. If the result was a failure, this method does nothing.
The block is executed immediately as `@noescape` and will not be retained.
- parameter success: the block to execute on success. The block passes a `BOOL` argument that indicates if there were any changes made.
*/
@objc
public func handleSuccess(@noescape success: (hasChanges: Bool) -> Void) {
guard case .Success(let hasChanges) = self.swift else {
return
}
success(hasChanges: hasChanges)
}
/**
If the result was a failure, the `failure` block is executed with an `NSError` argument pertaining to the actual error. If the result was a success, this method does nothing.
The block is executed immediately as `@noescape` and will not be retained.
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actuall error.
*/
@objc
public func handleFailure(@noescape failure: (error: NSError) -> Void) {
guard case .Failure(let error) = self.swift else {
return
}
failure(error: error.objc)
}
// MARK: NSObject

View File

@@ -97,7 +97,7 @@ public protocol CSLocalStorage: CSStorageInterface {
var mappingModelBundles: [NSBundle] { get }
/**
Options that tell the `DataStack` how to setup the persistent store
Options that tell the `CSDataStack` how to setup the persistent store
*/
@objc
var localStorageOptions: Int { get }

View File

@@ -0,0 +1,162 @@
//
// CSSynchronousDataTransaction.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CSSynchronousDataTransaction
/**
The `CSSynchronousDataTransaction` serves as the Objective-C bridging type for `SynchronousDataTransaction`.
*/
@objc
public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
/**
Saves the transaction changes and waits for completion synchronously. This method should not be used after the `-commitAndWait` method was already called once.
- returns: a `CSSaveResult` containing the success or failure information
*/
@objc
public func commitAndWait() -> CSSaveResult {
return bridge {
self.swift.commitAndWait()
}
}
/**
Begins a child transaction synchronously where `NSManagedObject` creates, updates, and deletes can be made. This method should not be used after the `-commitAndWait` method was already called once.
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
- returns: a `CSSaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
*/
@objc
public func beginSynchronous(closure: (transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
return bridge {
self.swift.beginSynchronous { (transaction) in
closure(transaction: transaction.objc)
}
}
}
// MARK: BaseDataTransaction
/**
Creates a new `NSManagedObject` with the specified entity type.
- parameter into: the `CSInto` clause indicating the destination `NSManagedObject` entity type and the destination configuration
- returns: a new `NSManagedObject` instance of the specified entity type.
*/
@objc
public override func createInto(into: CSInto) -> NSManagedObject {
return self.swift.create(into.swift)
}
/**
Returns an editable proxy of a specified `NSManagedObject`. This method should not be used after the `-commitAndWait` method was already called once.
- parameter object: the `NSManagedObject` type to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
@objc
@warn_unused_result
public override func editObject(object: NSManagedObject?) -> NSManagedObject? {
return self.swift.edit(object)
}
/**
Returns an editable proxy of the object with the specified `NSManagedObjectID`. This method should not be used after the `-commitAndWait` method was already called once.
- parameter into: a `CSInto` clause specifying the entity type
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
@objc
@warn_unused_result
public override func editInto(into: CSInto, objectID: NSManagedObjectID) -> NSManagedObject? {
return self.swift.edit(into.swift, objectID)
}
/**
Deletes a specified `NSManagedObject`. This method should not be used after the `-commitAndWait` method was already called once.
- parameter object: the `NSManagedObject` type to be deleted
*/
@objc
public override func deleteObject(object: NSManagedObject?) {
return self.swift.delete(object)
}
/**
Deletes the specified `NSManagedObject`s.
- parameter objects: the `NSManagedObject`s to be deleted
*/
public override func deleteObjects(objects: [NSManagedObject]) {
self.swift.delete(objects)
}
// MARK: CoreStoreBridge
internal typealias SwiftType = SynchronousDataTransaction
internal override var swift: SynchronousDataTransaction {
return super.swift as! SynchronousDataTransaction
}
public required init(_ swiftObject: SynchronousDataTransaction) {
super.init(swiftObject)
}
required public init(_ swiftObject: BaseDataTransaction) {
fatalError("init has not been implemented")
}
}
// MARK: - SynchronousDataTransaction
extension SynchronousDataTransaction {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSSynchronousDataTransaction
}

View File

@@ -0,0 +1,167 @@
//
// CSUnsafeDataTransaction.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CSUnsafeDataTransaction
/**
The `CSUnsafeDataTransaction` serves as the Objective-C bridging type for `UnsafeDataTransaction`.
*/
@objc
public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
/**
Saves the transaction changes asynchronously. For a `CSUnsafeDataTransaction`, multiple commits are allowed, although it is the developer's responsibility to ensure a reasonable leeway to prevent blocking the main thread.
- parameter completion: the block executed after the save completes. Success or failure is reported by the `CSSaveResult` argument of the block.
*/
@objc
public func commit(completion: ((result: CSSaveResult) -> Void)?) {
self.swift.commit { (result) in
completion?(result: result.objc)
}
}
/**
Saves the transaction changes and waits for completion synchronously. For a `CSUnsafeDataTransaction`, multiple commits are allowed, although it is the developer's responsibility to ensure a reasonable leeway to prevent blocking the main thread.
- returns: a `CSSaveResult` containing the success or failure information
*/
@objc
public func commitAndWait() -> CSSaveResult {
return bridge {
self.swift.commitAndWait()
}
}
/**
Rolls back the transaction.
*/
@objc
public func rollback() {
self.swift.rollback()
}
/**
Undo's the last change made to the transaction.
*/
@objc
public func undo() {
self.swift.undo()
}
/**
Redo's the last undone change to the transaction.
*/
@objc
public func redo() {
self.swift.redo()
}
/**
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.
To support "undo" methods such as `-undo`, `-redo`, and `-rollback`, use the `-beginSafeWithSupportsUndo:` method passing `true` to the argument. Without "undo" support, calling those methods will raise an exception.
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
*/
@objc
@warn_unused_result
public func beginUnsafe() -> CSUnsafeDataTransaction {
return bridge {
self.swift.beginUnsafe()
}
}
/**
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.
- prameter supportsUndo: `-undo`, `-redo`, and `-rollback` methods are only available when this parameter is `true`, otherwise those method will raise an exception. Note that turning on Undo support may heavily impact performance especially on iOS or watchOS where memory is limited.
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
*/
@objc
@warn_unused_result
public func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
return bridge {
self.swift.beginUnsafe(supportsUndo: supportsUndo)
}
}
/**
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:
- that the `CSUnsafeDataTransaction` 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 `CSUnsafeDataTransaction`'s `-commit:` or `-commitAndWait` method, or by calling `-save:` manually on the context, its parent, and all other ancestor contexts if there are any.
*/
@objc
public var internalContext: NSManagedObjectContext {
return self.swift.context
}
// MARK: CoreStoreBridge
internal typealias SwiftType = UnsafeDataTransaction
internal override var swift: UnsafeDataTransaction {
return super.swift as! UnsafeDataTransaction
}
public required init(_ swiftObject: UnsafeDataTransaction) {
super.init(swiftObject)
}
required public init(_ swiftObject: BaseDataTransaction) {
fatalError("init has not been implemented")
}
}
// MARK: - UnsafeDataTransaction
extension UnsafeDataTransaction {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSUnsafeDataTransaction
}

View File

@@ -63,6 +63,11 @@ internal func bridge<T: CoreStoreBridgeable where T == T.ObjCType.SwiftType>(@no
return closure().objc
}
internal func bridge<T: CoreStoreBridgeable where T == T.ObjCType.SwiftType>(@noescape closure: () -> T?) -> T.ObjCType? {
return closure()?.objc
}
internal func bridge<T: CoreStoreBridgeable where T == T.ObjCType.SwiftType>(@noescape closure: () throws -> T) throws -> T.ObjCType {
do {