WIP: SaveResult bridge

This commit is contained in:
John Rommel Estropia
2016-03-25 07:57:09 +09:00
parent 90369cf994
commit 707445a169
11 changed files with 189 additions and 59 deletions

View File

@@ -29,7 +29,7 @@ import CoreData
// MARK: - CoreStoreError
public enum CoreStoreError: ErrorType, CustomStringConvertible, CustomDebugStringConvertible, Equatable {
public enum CoreStoreError: ErrorType, CustomStringConvertible, CustomDebugStringConvertible, Hashable {
/**
A failure occured because of an unknown error.
@@ -94,6 +94,31 @@ public enum CoreStoreError: ErrorType, CustomStringConvertible, CustomDebugStrin
}
// MARK: Hashable
public var hashValue: Int {
let code = self._code
switch self {
case .Unknown:
return code.hashValue
case .DifferentStorageExistsAtURL(let existingPersistentStoreURL):
return code.hashValue ^ existingPersistentStoreURL.hashValue
case .MappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
return code.hashValue ^ localStoreURL.hashValue ^ targetModel.hashValue ^ targetModelVersion.hashValue
case .ProgressiveMigrationRequired(let localStoreURL):
return code.hashValue ^ localStoreURL.hashValue
case .InternalError(let NSError):
return code.hashValue ^ NSError.hashValue
}
}
// MARK: Internal
internal init(_ error: ErrorType?) {

View File

@@ -24,6 +24,7 @@
//
import Foundation
import CoreData
// MARK: - CSAsynchronousDataTransaction
@@ -34,34 +35,20 @@ import Foundation
@objc
public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
// /**
// Saves the transaction changes. This method should not be used after the `commit()` method was already called once.
//
// - parameter completion: the block executed after the save completes. Success or failure is reported by the `SaveResult` argument of the block.
// */
// public func commit(completion: (result: SaveResult) -> Void = { _ in }) {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to commit a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to commit a \(typeName(self)) more than once."
// )
//
// self.isCommitted = true
// let group = GCDGroup()
// group.enter()
// self.context.saveAsynchronouslyWithCompletion { (result) -> Void in
//
// self.result = result
// completion(result: result)
// group.leave()
// }
// group.wait()
// }
//
/**
Saves the transaction changes. This method should not be used after the `commit()` 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)?) {
self.swift.commit { (result) in
completion?(result: result.objc)
}
}
// /**
// 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.
//

View File

@@ -24,6 +24,7 @@
//
import Foundation
import CoreData
// MARK: - CSBaseDataTransaction

View File

@@ -255,6 +255,7 @@ public final class CSDataStack: NSObject, CoreStoreBridge {
internal init(_ swiftObject: DataStack) {
self.swift = swiftObject
super.init()
}
}

View File

@@ -107,6 +107,7 @@ public final class CSInMemoryStore: NSObject, CSStorageInterface, CoreStoreBridg
public required init(_ swiftObject: InMemoryStore) {
self.swift = swiftObject
super.init()
}
}

View File

@@ -2,11 +2,29 @@
// CSInto.swift
// CoreStore
//
// Created by John Estropia on 2016/03/24.
// 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 UIKit
import Foundation
import CoreData
// MARK: - CSInto

View File

@@ -184,6 +184,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreBridge {
public required init(_ swiftObject: SQLiteStore) {
self.swift = swiftObject
super.init()
}
}

View File

@@ -0,0 +1,76 @@
//
// CSSaveResult.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: - CSSaveResult
/**
The `CSSaveResult` serves as the Objective-C bridging type for `SaveResult`.
*/
@objc
public final class CSSaveResult: NSObject, CoreStoreBridge {
// MARK: NSObject
public override var hash: Int {
return self.swift.hashValue
}
public override func isEqual(object: AnyObject?) -> Bool {
guard let object = object as? CSSaveResult else {
return false
}
return self.swift == object.swift
}
// MARK: CoreStoreBridge
internal let swift: SaveResult
public required init(_ swiftObject: SaveResult) {
self.swift = swiftObject
super.init()
}
}
// MARK: - SaveResult
extension SaveResult: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSSaveResult
}

View File

@@ -148,32 +148,6 @@ public struct Into<T: NSManagedObject>: Hashable {
self.configuration = configuration
self.inferStoreIfPossible = inferStoreIfPossible
}
internal func upcast() -> Into<NSManagedObject> {
return Into<NSManagedObject>(
entityClass: self.entityClass,
configuration: self.configuration,
inferStoreIfPossible: self.inferStoreIfPossible
)
}
internal func downCast<U: NSManagedObject>(type: U.Type) -> Into<U>? {
let entityClass: AnyClass = self.entityClass
guard entityClass.isSubclassOfClass(U) else {
return nil
}
return Into<U>(
entityClass: entityClass,
configuration: self.configuration,
inferStoreIfPossible: self.inferStoreIfPossible
)
}
// MARK: Private
}

View File

@@ -57,7 +57,7 @@ import Foundation
}
```
*/
public enum SaveResult {
public enum SaveResult: Hashable {
/**
`SaveResult.Success` indicates that the `commit()` for the transaction succeeded, either because the save succeeded or because there were no changes to save. The associated value `hasChanges` indicates if there were saved changes or not.
@@ -70,6 +70,21 @@ public enum SaveResult {
case Failure(CoreStoreError)
// MARK: Hashable
public var hashValue: Int {
switch self {
case .Success(let hasChanges):
return self.boolValue.hashValue ^ hasChanges.hashValue
case .Failure(let error):
return self.boolValue.hashValue ^ error.hashValue
}
}
// MARK: Internal
internal init(hasChanges: Bool) {
@@ -97,3 +112,22 @@ extension SaveResult: BooleanType {
}
}
}
// MARK: - SaveResult: Equatable
@warn_unused_result
public func == (lhs: SaveResult, rhs: SaveResult) -> Bool {
switch (lhs, rhs) {
case (.Success(let hasChanges1), .Success(let hasChanges2)):
return hasChanges1 == hasChanges2
case (.Failure(let error1), .Failure(let error2)):
return error1 == error2
default:
return false
}
}