minor Swift 3 cleanup

This commit is contained in:
John Estropia
2016-12-08 18:59:41 +09:00
parent 5d2956d674
commit a27556f294
21 changed files with 453 additions and 255 deletions

View File

@@ -214,7 +214,7 @@ fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObj
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(NSFetchedResultsController<NSManagedObject>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
}
}
)
return controller.dynamicCast()
}

View File

@@ -93,17 +93,34 @@ public extension NSManagedObject {
return primitiveValue
}
/**
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
- parameter KVCKey: the KVC key
- parameter closure: a closure that receives the accessed value. This closure is executed between `willAccessValueForKey(...)` and `didAccessValueForKey(...)`
- returns: the primitive value for the KVC key
*/
@nonobjc
public func accessValueForKVCKey(_ KVCKey: KeyPath, _ closure: (Any?) -> Void) {
self.willAccessValue(forKey: KVCKey)
closure(self.primitiveValue(forKey: KVCKey))
self.didAccessValue(forKey: KVCKey)
}
/**
Provides a convenience wrapper for setting `setPrimitiveValue(...)` with proper calls to `willChangeValueForKey(...)` and `didChangeValueForKey(...)`. This is useful when implementing mutator methods for transient attributes.
- parameter value: the value to set the KVC key with
- parameter closure: a closure that receives the accessed value. This closure is executed between `willChangeValue(...)` and `didChangeValue(...)`
- parameter KVCKey: the KVC key
*/
@nonobjc
public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath, _ closure: () -> Void = {}) {
self.willChangeValue(forKey: KVCKey)
self.setPrimitiveValue(value, forKey: KVCKey)
closure()
self.didChangeValue(forKey: KVCKey)
}

View File

@@ -120,6 +120,33 @@ public enum CoreStoreError: Error, CustomNSError, Hashable {
}
// MARK: Equatable
public static func == (lhs: CoreStoreError, rhs: CoreStoreError) -> Bool {
switch (lhs, rhs) {
case (.unknown, .unknown):
return true
case (.differentStorageExistsAtURL(let url1), .differentStorageExistsAtURL(let url2)):
return url1 == url2
case (.mappingModelNotFound(let url1, let model1, let version1), .mappingModelNotFound(let url2, let model2, let version2)):
return url1 == url2 && model1 == model2 && version1 == version2
case (.progressiveMigrationRequired(let url1), .progressiveMigrationRequired(let url2)):
return url1 == url2
case (.internalError(let NSError1), .internalError(let NSError2)):
return NSError1 == NSError2
default:
return false
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -154,33 +181,6 @@ public enum CoreStoreError: Error, CustomNSError, Hashable {
}
// MARK: - CoreStoreError: Equatable
public func == (lhs: CoreStoreError, rhs: CoreStoreError) -> Bool {
switch (lhs, rhs) {
case (.unknown, .unknown):
return true
case (.differentStorageExistsAtURL(let url1), .differentStorageExistsAtURL(let url2)):
return url1 == url2
case (.mappingModelNotFound(let url1, let model1, let version1), .mappingModelNotFound(let url2, let model2, let version2)):
return url1 == url2 && model1 == model2 && version1 == version2
case (.progressiveMigrationRequired(let url1), .progressiveMigrationRequired(let url2)):
return url1 == url2
case (.internalError(let NSError1), .internalError(let NSError2)):
return NSError1 == NSError2
default:
return false
}
}
// MARK: - CoreStoreErrorDomain
/**

View File

@@ -85,6 +85,14 @@ public struct GroupBy: QueryClause, Hashable {
}
// MARK: Equatable
public static func == (lhs: GroupBy, rhs: GroupBy) -> Bool {
return lhs.keyPaths == rhs.keyPaths
}
// MARK: Hashable
public var hashValue: Int {
@@ -92,11 +100,3 @@ public struct GroupBy: QueryClause, Hashable {
return (self.keyPaths as NSArray).hashValue
}
}
// MARK: - GroupBy: Equatable
public func == (lhs: GroupBy, rhs: GroupBy) -> Bool {
return lhs.keyPaths == rhs.keyPaths
}

View File

@@ -152,6 +152,14 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
}
// MARK: Equatable
public static func == (lhs: OrderBy, rhs: OrderBy) -> Bool {
return lhs.sortDescriptors == rhs.sortDescriptors
}
// MARK: Hashable
public var hashValue: Int {
@@ -159,11 +167,3 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
return (self.sortDescriptors as NSArray).hashValue
}
}
// MARK: - OrderBy: Equatable
public func == (lhs: OrderBy, rhs: OrderBy) -> Bool {
return lhs.sortDescriptors == rhs.sortDescriptors
}

View File

@@ -241,6 +241,31 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable {
}
// MARK: Equatable
public static func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
switch (lhs, rhs) {
case (._attribute(let keyPath1), ._attribute(let keyPath2)):
return keyPath1 == keyPath2
case (._aggregate(let function1, let keyPath1, let alias1, let nativeType1),
._aggregate(let function2, let keyPath2, let alias2, let nativeType2)):
return function1 == function2
&& keyPath1 == keyPath2
&& alias1 == alias2
&& nativeType1 == nativeType2
case (._identity(let alias1, let nativeType1), ._identity(let alias2, let nativeType2)):
return alias1 == alias2 && nativeType1 == nativeType2
default:
return false
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -267,31 +292,6 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable {
}
// MARK: - SelectTerm: Equatable
public func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
switch (lhs, rhs) {
case (._attribute(let keyPath1), ._attribute(let keyPath2)):
return keyPath1 == keyPath2
case (._aggregate(let function1, let keyPath1, let alias1, let nativeType1),
._aggregate(let function2, let keyPath2, let alias2, let nativeType2)):
return function1 == function2
&& keyPath1 == keyPath2
&& alias1 == alias2
&& nativeType1 == nativeType2
case (._identity(let alias1, let nativeType1), ._identity(let alias2, let nativeType2)):
return alias1 == alias2 && nativeType1 == nativeType2
default:
return false
}
}
// MARK: - Select
/**
@@ -364,6 +364,14 @@ public struct Select<T: SelectResultType>: Hashable {
}
// MARK: Equatable
public static func == <T: SelectResultType, U: SelectResultType>(lhs: Select<T>, rhs: Select<U>) -> Bool {
return lhs.selectTerms == rhs.selectTerms
}
// MARK: Hashable
public var hashValue: Int {
@@ -386,14 +394,6 @@ public extension Select where T: NSManagedObjectID {
}
// MARK: - Select: Equatable
public func == <T: SelectResultType, U: SelectResultType>(lhs: Select<T>, rhs: Select<U>) -> Bool {
return lhs.selectTerms == rhs.selectTerms
}
// MARK: - Bool: SelectValueResultType
extension Bool: SelectValueResultType {

View File

@@ -157,6 +157,14 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
}
// MARK: Equatable
public static func == (lhs: Where, rhs: Where) -> Bool {
return lhs.predicate == rhs.predicate
}
// MARK: Hashable
public var hashValue: Int {
@@ -164,11 +172,3 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
return self.predicate.hashValue
}
}
// MARK: - Where: Equatable
public func == (lhs: Where, rhs: Where) -> Bool {
return lhs.predicate == rhs.predicate
}

View File

@@ -82,12 +82,10 @@ public extension BaseDataTransaction {
try autoreleasepool {
let entityType = type(of: object)
guard entityType.shouldInsert(from: source, in: self) else {
return
}
try object.didInsert(from: source, in: self)
}
}
@@ -114,12 +112,10 @@ public extension BaseDataTransaction {
return try sourceArray.flatMap { (source) -> T? in
let entityType = into.entityClass as! T.Type
guard entityType.shouldInsert(from: source, in: self) else {
return nil
}
return try autoreleasepool {
let object = self.create(into)
@@ -150,7 +146,6 @@ public extension BaseDataTransaction {
return try autoreleasepool {
let entityType = into.entityClass as! T.Type
let uniqueIDKeyPath = entityType.uniqueIDKeyPath
guard let uniqueIDValue = try entityType.uniqueID(from: source, in: self) else {
@@ -163,7 +158,6 @@ public extension BaseDataTransaction {
return nil
}
try object.update(from: source, in: self)
return object
}
@@ -173,7 +167,6 @@ public extension BaseDataTransaction {
return nil
}
let object = self.create(into)
object.uniqueIDValue = uniqueIDValue
try object.didInsert(from: source, in: self)
@@ -206,7 +199,6 @@ public extension BaseDataTransaction {
return try autoreleasepool {
let entityType = into.entityClass as! T.Type
var importSourceByID = Dictionary<T.UniqueIDType, T.ImportSource>()
let sortedIDs = try autoreleasepool {
@@ -242,17 +234,15 @@ public extension BaseDataTransaction {
guard entityType.shouldUpdate(from: source, in: self) else { return }
try object.update(from: source, in: self)
result.append(object)
}
else if entityType.shouldInsert(from: source, in: self) {
let object = self.create(into)
object.uniqueIDValue = objectID
try object.didInsert(from: source, in: self)
result.append(object)
}
processedObjectIDs.insert(objectID)
}
}

View File

@@ -37,7 +37,7 @@ internal final class MigrationManager: NSMigrationManager, ProgressReporting {
super.didChangeValue(forKey: key)
guard key == "migrationProgress" else {
guard key == #keyPath(NSMigrationManager.migrationProgress) else {
return
}

View File

@@ -71,7 +71,6 @@ public extension CoreStore {
)
}
internal static func abort(_ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) -> Never {
self.logger.abort(

View File

@@ -216,6 +216,17 @@ public struct MigrationChain: ExpressibleByNilLiteral, ExpressibleByStringLitera
}
// MARK: Equatable
public static func == (lhs: MigrationChain, rhs: MigrationChain) -> Bool {
return lhs.versionTree == rhs.versionTree
&& lhs.rootVersions == rhs.rootVersions
&& lhs.leafVersions == rhs.leafVersions
&& lhs.valid == rhs.valid
}
// MARK: Internal
internal let rootVersions: Set<String>
@@ -249,14 +260,3 @@ public struct MigrationChain: ExpressibleByNilLiteral, ExpressibleByStringLitera
fileprivate let versionTree: [String: String]
}
// MARK: - MigrationChain: Equatable
public func == (lhs: MigrationChain, rhs: MigrationChain) -> Bool {
return lhs.versionTree == rhs.versionTree
&& lhs.rootVersions == rhs.rootVersions
&& lhs.leafVersions == rhs.leafVersions
&& lhs.valid == rhs.valid
}

View File

@@ -83,6 +83,24 @@ public enum MigrationResult: Hashable {
}
// MARK: Equatable
public static func == (lhs: MigrationResult, rhs: MigrationResult) -> Bool {
switch (lhs, rhs) {
case (.success(let migrationTypes1), .success(let migrationTypes2)):
return migrationTypes1 == migrationTypes2
case (.failure(let error1), .failure(let error2)):
return error1 == error2
default:
return false
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -116,21 +134,3 @@ public enum MigrationResult: Hashable {
self = .failure(CoreStoreError(error))
}
}
// MARK: - SetupResult: Equatable
public func == (lhs: MigrationResult, rhs: MigrationResult) -> Bool {
switch (lhs, rhs) {
case (.success(let migrationTypes1), .success(let migrationTypes2)):
return migrationTypes1 == migrationTypes2
case (.failure(let error1), .failure(let error2)):
return error1 == error2
default:
return false
}
}

View File

@@ -122,6 +122,27 @@ public enum MigrationType: Hashable {
}
// MARK: Equatable
public static func == (lhs: MigrationType, rhs: MigrationType) -> Bool {
switch (lhs, rhs) {
case (.none(let version1), .none(let version2)):
return version1 == version2
case (.lightweight(let source1, let destination1), .lightweight(let source2, let destination2)):
return source1 == source2 && destination1 == destination2
case (.heavyweight(let source1, let destination1), .heavyweight(let source2, let destination2)):
return source1 == source2 && destination1 == destination2
default:
return false
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -140,24 +161,3 @@ public enum MigrationType: Hashable {
}
}
}
// MARK: - MigrationType: Equatable
public func == (lhs: MigrationType, rhs: MigrationType) -> Bool {
switch (lhs, rhs) {
case (.none(let version1), .none(let version2)):
return version1 == version2
case (.lightweight(let source1, let destination1), .lightweight(let source2, let destination2)):
return source1 == source2 && destination1 == destination2
case (.heavyweight(let source1, let destination1), .heavyweight(let source2, let destination2)):
return source1 == source2 && destination1 == destination2
default:
return false
}
}

View File

@@ -86,6 +86,24 @@ public enum SetupResult<T: StorageInterface>: Hashable {
}
// MARK: Equatable
public static func == <T: StorageInterface, U: StorageInterface>(lhs: SetupResult<T>, rhs: SetupResult<U>) -> Bool {
switch (lhs, rhs) {
case (.success(let storage1), .success(let storage2)):
return storage1 === storage2
case (.failure(let error1), .failure(let error2)):
return error1 == error2
default:
return false
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -118,21 +136,3 @@ public enum SetupResult<T: StorageInterface>: Hashable {
self = .failure(CoreStoreError(error))
}
}
// MARK: - SetupResult: Equatable
public func == <T: StorageInterface, U: StorageInterface>(lhs: SetupResult<T>, rhs: SetupResult<U>) -> Bool {
switch (lhs, rhs) {
case (.success(let storage1), .success(let storage2)):
return storage1 === storage2
case (.failure(let error1), .failure(let error2)):
return error1 == error2
default:
return false
}
}

View File

@@ -594,6 +594,29 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
}
// MARK: Equatable
public static func == <T: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool {
return lhs === rhs
}
public static func == <T: NSManagedObject, U: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<U>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController
}
public static func ~= <T: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool {
return lhs === rhs
}
public static func ~= <T: NSManagedObject, U: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<U>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController
}
// MARK: Hashable
public var hashValue: Int {
@@ -1123,31 +1146,6 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
}
// MARK: - ListMonitor: Equatable
public func == <T: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool {
return lhs === rhs
}
public func == <T: NSManagedObject, U: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<U>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController
}
public func ~= <T: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool {
return lhs === rhs
}
public func ~= <T: NSManagedObject, U: NSManagedObject>(lhs: ListMonitor<T>, rhs: ListMonitor<U>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController
}
extension ListMonitor: Equatable { }
// MARK: - ListMonitor: FetchedResultsControllerHandler
extension ListMonitor: FetchedResultsControllerHandler {

View File

@@ -41,7 +41,7 @@ import CoreData
Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
*/
public final class ObjectMonitor<EntityType: NSManagedObject> {
public final class ObjectMonitor<EntityType: NSManagedObject>: Equatable {
/**
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
@@ -103,6 +103,19 @@ public final class ObjectMonitor<EntityType: NSManagedObject> {
}
// MARK: Equatable
public static func == <T: NSManagedObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool {
return lhs === rhs
}
public static func ~= <T: NSManagedObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool {
return lhs === rhs
}
// MARK: Hashable
public var hashValue: Int {
@@ -291,21 +304,6 @@ public final class ObjectMonitor<EntityType: NSManagedObject> {
}
// MARK: - ObjectMonitor: Equatable
public func == <T: NSManagedObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool {
return lhs === rhs
}
public func ~= <T: NSManagedObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool {
return lhs === rhs
}
extension ObjectMonitor: Equatable { }
// MARK: - ObjectMonitor: FetchedResultsControllerHandler
extension ObjectMonitor: FetchedResultsControllerHandler {

View File

@@ -70,7 +70,7 @@ public struct SectionBy {
// MARK: Internal
internal let sectionKeyPath: KeyPath
internal let sectionIndexTransformer: (_ sectionName: KeyPath?) -> String?
internal let sectionIndexTransformer: (_ sectionName: String?) -> String?
}
#endif

View File

@@ -32,7 +32,7 @@ import CoreData
/**
The `DataStack` encapsulates the data model for the Core Data stack. Each `DataStack` can have multiple data stores, usually specified as a "Configuration" in the model editor. Behind the scenes, the DataStack manages its own `NSPersistentStoreCoordinator`, a root `NSManagedObjectContext` for disk saves, and a shared `NSManagedObjectContext` designed as a read-only model interface for `NSManagedObjects`.
*/
public final class DataStack {
public final class DataStack: Equatable {
/**
Initializes a `DataStack` from the model with the specified `modelName` in the specified `bundle`.
@@ -381,6 +381,14 @@ public final class DataStack {
}
// MARK: Equatable
public static func == (lhs: DataStack, rhs: DataStack) -> Bool {
return lhs === rhs
}
// MARK: Internal
internal static let applicationName = (Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String) ?? "CoreData"
@@ -527,13 +535,3 @@ public final class DataStack {
return self.objectID(forURIRepresentation: url)
}
}
// MARK: - DataStack: Equatable
public func == (lhs: DataStack, rhs: DataStack) -> Bool {
return lhs === rhs
}
extension DataStack: Equatable {}

View File

@@ -134,6 +134,16 @@ public struct Into<T: NSManagedObject>: Hashable {
}
// MARK: Equatable
public static func == <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<U>) -> Bool {
return lhs.entityClass == rhs.entityClass
&& lhs.configuration == rhs.configuration
&& lhs.inferStoreIfPossible == rhs.inferStoreIfPossible
}
// MARK: Hashable
public var hashValue: Int {
@@ -172,20 +182,3 @@ public struct Into<T: NSManagedObject>: Hashable {
self.inferStoreIfPossible = inferStoreIfPossible
}
}
// MARK: - Into: Equatable
public func == <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<U>) -> Bool {
return lhs.entityClass == rhs.entityClass
&& lhs.configuration == rhs.configuration
&& lhs.inferStoreIfPossible == rhs.inferStoreIfPossible
}
public func != <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<U>) -> Bool {
return lhs.entityClass == rhs.entityClass
&& lhs.configuration == rhs.configuration
&& lhs.inferStoreIfPossible == rhs.inferStoreIfPossible
}

View File

@@ -83,6 +83,24 @@ public enum SaveResult: Hashable {
}
// MARK: Equatable
public static 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
}
}
// MARK: Hashable
public var hashValue: Int {
@@ -110,21 +128,3 @@ public enum SaveResult: Hashable {
self = .failure(error)
}
}
// MARK: - SaveResult: Equatable
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
}
}