From 261c3a6001424532f85c09583e940ccf0bbe0af7 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Fri, 3 Jul 2015 00:22:21 +0900 Subject: [PATCH] minor changes before Swift 2.0 --- CoreStore/Migrating/DataStack+Migration.swift | 4 +- CoreStore/Migrating/MigrationChain.swift | 44 ++++++++++++------- CoreStore/Setting Up/DataStack.swift | 5 ++- .../xcshareddata/CoreStoreDemo.xccheckout | 4 +- README.md | 4 +- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/CoreStore/Migrating/DataStack+Migration.swift b/CoreStore/Migrating/DataStack+Migration.swift index 33c4135..3f7c4d8 100644 --- a/CoreStore/Migrating/DataStack+Migration.swift +++ b/CoreStore/Migrating/DataStack+Migration.swift @@ -48,7 +48,7 @@ public extension DataStack { isDirectory: false ), configuration: configuration, - sourceBundles: sourceBundles + mappingModelBundles: mappingModelBundles ) } @@ -57,7 +57,7 @@ public extension DataStack { :param: fileURL the local file URL for the SQLite persistent store. :param: configuration an optional configuration name from the model file. If not specified, defaults to `nil` which indicates the "Default" configuration. - :param: sourceBundles an optional array of bundles to search mapping model files from. If not set, defaults to the `NSBundle.allBundles()`. + :param: mappingModelBundles an optional array of bundles to search mapping model files from. If not set, defaults to the `NSBundle.allBundles()`. :return: a `MigrationType` indicating the type of migration required for the store; or `nil` if either inspection of the store failed, or no mapping model was found/inferred. `MigrationType` acts as a `Bool` and evaluates to `false` if no migration is required, and `true` if either a lightweight or custom migration is needed. */ public func needsMigrationForSQLiteStore(fileURL: NSURL = defaultSQLiteStoreURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as! [NSBundle]) -> MigrationType? { diff --git a/CoreStore/Migrating/MigrationChain.swift b/CoreStore/Migrating/MigrationChain.swift index c6a1877..3cc563e 100644 --- a/CoreStore/Migrating/MigrationChain.swift +++ b/CoreStore/Migrating/MigrationChain.swift @@ -35,8 +35,14 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D public func contains(version: String) -> Bool { - return self.latestVersion == version - || find(self.versionTree.keys, version) != nil + return self.rootVersions.contains(version) + || self.leafVersions.contains(version) + || self.versionTree[version] != nil + } + + public func nextVersionFrom(version: String) -> String? { + + return self.versionTree[version] } @@ -44,8 +50,9 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D public init(nilLiteral: ()) { - self.latestVersion = nil self.versionTree = [:] + self.rootVersions = [] + self.leafVersions = [] } @@ -53,16 +60,18 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D public init(stringLiteral value: String) { - self.latestVersion = value self.versionTree = [:] + self.rootVersions = [value] + self.leafVersions = [value] } // MARK: ExtendedGraphemeClusterLiteralConvertible public init(extendedGraphemeClusterLiteral value: String) { - self.latestVersion = value self.versionTree = [:] + self.rootVersions = [value] + self.leafVersions = [value] } @@ -70,8 +79,9 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D public init(unicodeScalarLiteral value: String) { - self.latestVersion = value self.versionTree = [:] + self.rootVersions = [value] + self.leafVersions = [value] } @@ -84,15 +94,17 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D versionTree[tuple.0] = tuple.1 return versionTree } - let latestVersions = elements.filter { (tuple: (String, String)) -> Bool in - - return versionTree[tuple.1] == nil - } + let leafVersions = Set( + elements.filter { (tuple: (String, String)) -> Bool in + + return versionTree[tuple.1] == nil + + }.map { $1 } + ) - CoreStore.assert(latestVersions.count == 1, "\(typeName(MigrationChain))'s migration chain could not be resolved due to multiple leaf versions.") - - self.latestVersion = latestVersions.first?.1 self.versionTree = versionTree + self.rootVersions = Set(versionTree.keys).subtract(versionTree.values) + self.leafVersions = leafVersions } @@ -114,13 +126,15 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D } self.versionTree = versionTree - self.latestVersion = elements.last + self.rootVersions = Set(flatMap([elements.first]) { $0 == nil ? [] : [$0!] }) + self.leafVersions = Set(flatMap([elements.last]) { $0 == nil ? [] : [$0!] }) } // MARK: Internal - internal let latestVersion: String? + internal let rootVersions: Set + internal let leafVersions: Set // MARK: Private diff --git a/CoreStore/Setting Up/DataStack.swift b/CoreStore/Setting Up/DataStack.swift index c05d89b..e5a7597 100644 --- a/CoreStore/Setting Up/DataStack.swift +++ b/CoreStore/Setting Up/DataStack.swift @@ -57,7 +57,10 @@ public final class DataStack { modelName, ofType: "momd" ) - CoreStore.assert(modelFilePath != nil, "Could not find a \"momd\" resource from the main bundle.") + CoreStore.assert( + modelFilePath != nil, + "Could not find a \"momd\" resource from the main bundle." + ) let managedObjectModel: NSManagedObjectModel! = NSManagedObjectModel(contentsOfURL: NSURL(fileURLWithPath: modelFilePath)!) CoreStore.assert( diff --git a/CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace/xcshareddata/CoreStoreDemo.xccheckout b/CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace/xcshareddata/CoreStoreDemo.xccheckout index a385032..ca9ddfb 100644 --- a/CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace/xcshareddata/CoreStoreDemo.xccheckout +++ b/CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace/xcshareddata/CoreStoreDemo.xccheckout @@ -7,7 +7,7 @@ IDESourceControlProjectIdentifier B6855E48-4B19-4321-B1C7-CB2706E12777 IDESourceControlProjectName - project + CoreStoreDemo IDESourceControlProjectOriginsDictionary 4B60F1BCB491FF717C56441AE7783C74F417BE48 @@ -16,7 +16,7 @@ github.com:JohnEstropia/GCDKit.git IDESourceControlProjectPath - CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace + CoreStoreDemo/CoreStoreDemo.xcodeproj IDESourceControlProjectRelativeInstallPathDictionary 4B60F1BCB491FF717C56441AE7783C74F417BE48 diff --git a/README.md b/README.md index 73e933c..560af1c 100644 --- a/README.md +++ b/README.md @@ -406,7 +406,7 @@ There are currently 5 fetch methods you can call from `CoreStore`, from a `DataS - `fetchAll(...)` - returns an array of all objects that match the criteria. - `fetchOne(...)` - returns the first object that match the criteria. - `fetchCount(...)` - returns the number of objects that match the criteria. -- `fetchObjectIDs(...)`` - returns an array of `NSManagedObjectID`s for all objects that match the criteria. +- `fetchObjectIDs(...)` - returns an array of `NSManagedObjectID`s for all objects that match the criteria. - `fetchObjectID(...)` - returns the `NSManagedObjectID`s for the first objects that match the criteria. Each method's purpose is straightforward, but we need to understand how to set the clauses for the fetch. @@ -450,7 +450,7 @@ var mostValuablePeople = CoreStore.fetchAll( OrderBy(.Descending("rating"), .Ascending("surname")) ) ``` -As seen above, `OrderBy` accepts a list of `SortKey` enumeration values, which can be either `.Ascending` or `.Descending`. The associated value for the `SortKey` enumeration is the attribute key string. +As seen above, `OrderBy` accepts a list of `SortKey` enumeration values, which can be either `.Ascending` or `.Descending`. You can use the `+` and `+=` operator to append `OrderBy`s together. This is useful when sorting conditionally: ```swift