mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-10 03:13:35 +02:00
minor changes before Swift 2.0
This commit is contained in:
@@ -48,7 +48,7 @@ public extension DataStack {
|
|||||||
isDirectory: false
|
isDirectory: false
|
||||||
),
|
),
|
||||||
configuration: configuration,
|
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: 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: 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.
|
: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? {
|
public func needsMigrationForSQLiteStore(fileURL: NSURL = defaultSQLiteStoreURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as! [NSBundle]) -> MigrationType? {
|
||||||
|
|||||||
@@ -35,8 +35,14 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
|||||||
|
|
||||||
public func contains(version: String) -> Bool {
|
public func contains(version: String) -> Bool {
|
||||||
|
|
||||||
return self.latestVersion == version
|
return self.rootVersions.contains(version)
|
||||||
|| find(self.versionTree.keys, version) != nil
|
|| 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: ()) {
|
public init(nilLiteral: ()) {
|
||||||
|
|
||||||
self.latestVersion = nil
|
|
||||||
self.versionTree = [:]
|
self.versionTree = [:]
|
||||||
|
self.rootVersions = []
|
||||||
|
self.leafVersions = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -53,16 +60,18 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
|||||||
|
|
||||||
public init(stringLiteral value: String) {
|
public init(stringLiteral value: String) {
|
||||||
|
|
||||||
self.latestVersion = value
|
|
||||||
self.versionTree = [:]
|
self.versionTree = [:]
|
||||||
|
self.rootVersions = [value]
|
||||||
|
self.leafVersions = [value]
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: ExtendedGraphemeClusterLiteralConvertible
|
// MARK: ExtendedGraphemeClusterLiteralConvertible
|
||||||
|
|
||||||
public init(extendedGraphemeClusterLiteral value: String) {
|
public init(extendedGraphemeClusterLiteral value: String) {
|
||||||
|
|
||||||
self.latestVersion = value
|
|
||||||
self.versionTree = [:]
|
self.versionTree = [:]
|
||||||
|
self.rootVersions = [value]
|
||||||
|
self.leafVersions = [value]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,8 +79,9 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
|||||||
|
|
||||||
public init(unicodeScalarLiteral value: String) {
|
public init(unicodeScalarLiteral value: String) {
|
||||||
|
|
||||||
self.latestVersion = value
|
|
||||||
self.versionTree = [:]
|
self.versionTree = [:]
|
||||||
|
self.rootVersions = [value]
|
||||||
|
self.leafVersions = [value]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,15 +94,17 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
|||||||
versionTree[tuple.0] = tuple.1
|
versionTree[tuple.0] = tuple.1
|
||||||
return versionTree
|
return versionTree
|
||||||
}
|
}
|
||||||
let latestVersions = elements.filter { (tuple: (String, String)) -> Bool in
|
let leafVersions = Set(
|
||||||
|
elements.filter { (tuple: (String, String)) -> Bool in
|
||||||
return versionTree[tuple.1] == nil
|
|
||||||
}
|
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.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.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
|
// MARK: Internal
|
||||||
|
|
||||||
internal let latestVersion: String?
|
internal let rootVersions: Set<String>
|
||||||
|
internal let leafVersions: Set<String>
|
||||||
|
|
||||||
|
|
||||||
// MARK: Private
|
// MARK: Private
|
||||||
|
|||||||
@@ -57,7 +57,10 @@ public final class DataStack {
|
|||||||
modelName,
|
modelName,
|
||||||
ofType: "momd"
|
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)!)
|
let managedObjectModel: NSManagedObjectModel! = NSManagedObjectModel(contentsOfURL: NSURL(fileURLWithPath: modelFilePath)!)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<key>IDESourceControlProjectIdentifier</key>
|
<key>IDESourceControlProjectIdentifier</key>
|
||||||
<string>B6855E48-4B19-4321-B1C7-CB2706E12777</string>
|
<string>B6855E48-4B19-4321-B1C7-CB2706E12777</string>
|
||||||
<key>IDESourceControlProjectName</key>
|
<key>IDESourceControlProjectName</key>
|
||||||
<string>project</string>
|
<string>CoreStoreDemo</string>
|
||||||
<key>IDESourceControlProjectOriginsDictionary</key>
|
<key>IDESourceControlProjectOriginsDictionary</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>4B60F1BCB491FF717C56441AE7783C74F417BE48</key>
|
<key>4B60F1BCB491FF717C56441AE7783C74F417BE48</key>
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
<string>github.com:JohnEstropia/GCDKit.git</string>
|
<string>github.com:JohnEstropia/GCDKit.git</string>
|
||||||
</dict>
|
</dict>
|
||||||
<key>IDESourceControlProjectPath</key>
|
<key>IDESourceControlProjectPath</key>
|
||||||
<string>CoreStoreDemo/CoreStoreDemo.xcodeproj/project.xcworkspace</string>
|
<string>CoreStoreDemo/CoreStoreDemo.xcodeproj</string>
|
||||||
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
|
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>4B60F1BCB491FF717C56441AE7783C74F417BE48</key>
|
<key>4B60F1BCB491FF717C56441AE7783C74F417BE48</key>
|
||||||
|
|||||||
@@ -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.
|
- `fetchAll(...)` - returns an array of all objects that match the criteria.
|
||||||
- `fetchOne(...)` - returns the first object that match the criteria.
|
- `fetchOne(...)` - returns the first object that match the criteria.
|
||||||
- `fetchCount(...)` - returns the number of objects 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.
|
- `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.
|
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"))
|
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:
|
You can use the `+` and `+=` operator to append `OrderBy`s together. This is useful when sorting conditionally:
|
||||||
```swift
|
```swift
|
||||||
|
|||||||
Reference in New Issue
Block a user