mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-06-05 14:12:54 +02:00
Merge branch 'swift3_develop' into develop
# Conflicts: # .travis.yml # CoreStore.podspec # CoreStore.xcodeproj/project.pbxproj # Sources/Info.plist
This commit is contained in:
@@ -26,28 +26,26 @@
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// MARK: - NSFetchedResultsController
|
||||
// MARK: - DataStack
|
||||
|
||||
public extension NSFetchedResultsController {
|
||||
public extension DataStack {
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter dataStack: the `DataStack` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes a `DataStack`
|
||||
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(dataStack: DataStack, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||
|
||||
return self.createFromContext(
|
||||
dataStack.mainContext,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
return createFRC(
|
||||
fromContext: self.mainContext,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
@@ -56,19 +54,18 @@ public extension NSFetchedResultsController {
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter dataStack: the `DataStack` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes a `DataStack`
|
||||
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(dataStack: DataStack, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
return self.createFromContext(
|
||||
dataStack.mainContext,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
return createFRC(
|
||||
fromContext: self.mainContext,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
@@ -76,19 +73,18 @@ public extension NSFetchedResultsController {
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter dataStack: the `DataStack` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes a `DataStack`
|
||||
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(dataStack: DataStack, _ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||
|
||||
return self.createFromContext(
|
||||
dataStack.mainContext,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
return createFRC(
|
||||
fromContext: self.mainContext,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
@@ -96,128 +92,131 @@ public extension NSFetchedResultsController {
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter dataStack: the `DataStack` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes a `DataStack`
|
||||
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(dataStack: DataStack, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
||||
public func createFetchedResultsController<T: NSManagedObject>(forDataStack dataStack: DataStack, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
return self.createFromContext(
|
||||
dataStack.mainContext,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
return createFRC(
|
||||
fromContext: self.mainContext,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
|
||||
- parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(transaction: UnsafeDataTransaction, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
||||
|
||||
return self.createFromContext(
|
||||
transaction.context,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
|
||||
- parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(transaction: UnsafeDataTransaction, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
||||
|
||||
return self.createFromContext(
|
||||
transaction.context,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
|
||||
- parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(transaction: UnsafeDataTransaction, _ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
||||
|
||||
return self.createFromContext(
|
||||
transaction.context,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
|
||||
- parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
@nonobjc
|
||||
public static func createFor<T: NSManagedObject>(transaction: UnsafeDataTransaction, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
||||
|
||||
return self.createFromContext(
|
||||
transaction.context,
|
||||
fetchRequest: CoreStoreFetchRequest(),
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal static func createFromContext<T: NSManagedObject>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
||||
|
||||
return CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: fetchRequest,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UnsafeDataTransaction
|
||||
|
||||
public extension UnsafeDataTransaction {
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||
|
||||
return createFRC(
|
||||
fromContext: self.context,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
return createFRC(
|
||||
fromContext: self.context,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||
|
||||
return createFRC(
|
||||
fromContext: self.context,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `performFetch()` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||
*/
|
||||
@nonobjc
|
||||
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
return createFRC(
|
||||
fromContext: self.context,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObjectContext, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
let controller = CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: CoreStoreFetchRequest().dynamicCast(),
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
|
||||
CoreStore.assert(
|
||||
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()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -31,6 +31,52 @@ import CoreData
|
||||
|
||||
public extension NSManagedObject {
|
||||
|
||||
/**
|
||||
Exposes a `FetchableSource` that can fetch sibling objects of this `NSManagedObject` instance. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the obejct's parent is already deallocated.
|
||||
- Warning: Future implementations may change the instance returned by this method depending on the timing or condition that `fetchSource()` was called. Do not make assumptions that the instance will be a specific instance. If the `NSManagedObjectContext` instance is desired, use the `FetchableSource.internalContext()` method to get the correct instance. Also, do not assume that the `fetchSource()` and `querySource()` return the same instance all the time.
|
||||
- returns: a `FetchableSource` that can fetch sibling objects of this `NSManagedObject` instance. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the object's parent is already deallocated.
|
||||
*/
|
||||
@nonobjc
|
||||
public func fetchSource() -> FetchableSource? {
|
||||
|
||||
guard let context = self.managedObjectContext else {
|
||||
|
||||
return nil
|
||||
}
|
||||
if context.isTransactionContext {
|
||||
|
||||
return context.parentTransaction
|
||||
}
|
||||
if context.isDataStackContext {
|
||||
|
||||
return context.parentStack
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
Exposes a `QueryableSource` that can query attributes and aggregate values. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the obejct's parent is already deallocated.
|
||||
- Warning: Future implementations may change the instance returned by this method depending on the timing or condition that `querySource()` was called. Do not make assumptions that the instance will be a specific instance. If the `NSManagedObjectContext` instance is desired, use the `QueryableSource.internalContext()` method to get the correct instance. Also, do not assume that the `fetchSource()` and `querySource()` return the same instance all the time.
|
||||
- returns: a `QueryableSource` that can query attributes and aggregate values. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the object's parent is already deallocated.
|
||||
*/
|
||||
@nonobjc
|
||||
public func querySource() -> QueryableSource? {
|
||||
|
||||
guard let context = self.managedObjectContext else {
|
||||
|
||||
return nil
|
||||
}
|
||||
if context.isTransactionContext {
|
||||
|
||||
return context.parentTransaction
|
||||
}
|
||||
if context.isDataStackContext {
|
||||
|
||||
return context.parentStack
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
|
||||
|
||||
@@ -38,14 +84,33 @@ public extension NSManagedObject {
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@nonobjc
|
||||
@warn_unused_result
|
||||
public func accessValueForKVCKey(KVCKey: KeyPath) -> AnyObject? {
|
||||
public func accessValueForKVCKey(_ KVCKey: KeyPath) -> Any? {
|
||||
|
||||
self.willAccessValueForKey(KVCKey)
|
||||
let primitiveValue: AnyObject? = self.primitiveValueForKey(KVCKey)
|
||||
self.didAccessValueForKey(KVCKey)
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
}
|
||||
return self.primitiveValue(forKey: KVCKey)
|
||||
}
|
||||
|
||||
/**
|
||||
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 didAccessPrimitiveValue: the closure to access the value. This is called between `willAccessValueForKey(...)` and `didAccessValueForKey(...)`
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func accessValueForKVCKey<T>(_ KVCKey: KeyPath, _ didAccessPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
return primitiveValue
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
}
|
||||
return try didAccessPrimitiveValue(self.primitiveValue(forKey: KVCKey))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,11 +120,34 @@ public extension NSManagedObject {
|
||||
- parameter KVCKey: the KVC key
|
||||
*/
|
||||
@nonobjc
|
||||
public func setValue(value: AnyObject?, forKVCKey KVCKey: KeyPath) {
|
||||
public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
|
||||
|
||||
self.willChangeValueForKey(KVCKey)
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
self.didChangeValueForKey(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 KVCKey: the KVC key
|
||||
- parameter didSetPrimitiveValue: the closure called between `willChangeValueForKey(...)` and `didChangeValueForKey(...)`
|
||||
*/
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func setValue<T>(_ value: Any?, forKVCKey KVCKey: KeyPath, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
return try didSetPrimitiveValue(value)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +156,7 @@ public extension NSManagedObject {
|
||||
@nonobjc
|
||||
public func refreshAsFault() {
|
||||
|
||||
self.managedObjectContext?.refreshObject(self, mergeChanges: false)
|
||||
self.managedObjectContext?.refresh(self, mergeChanges: false)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,6 +165,6 @@ public extension NSManagedObject {
|
||||
@nonobjc
|
||||
public func refreshAndMerge() {
|
||||
|
||||
self.managedObjectContext?.refreshObject(self, mergeChanges: true)
|
||||
self.managedObjectContext?.refresh(self, mergeChanges: true)
|
||||
}
|
||||
}
|
||||
|
||||
+21
-21
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// NSProgress+Convenience.swift
|
||||
// Progress+Convenience.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2015 John Rommel Estropia
|
||||
@@ -24,22 +24,19 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - NSProgress
|
||||
// MARK: - Progress
|
||||
|
||||
public extension NSProgress {
|
||||
public extension Progress {
|
||||
|
||||
/**
|
||||
Sets a closure that the `NSProgress` calls whenever its `fractionCompleted` changes. You can use this instead of setting up KVO.
|
||||
Sets a closure that the `Progress` calls whenever its `fractionCompleted` changes. You can use this instead of setting up KVO.
|
||||
|
||||
- parameter closure: the closure to execute on progress change
|
||||
*/
|
||||
@nonobjc
|
||||
public func setProgressHandler(closure: ((progress: NSProgress) -> Void)?) {
|
||||
public func setProgressHandler(_ closure: ((_ progress: Progress) -> Void)?) {
|
||||
|
||||
self.progressObserver.progressHandler = closure
|
||||
}
|
||||
@@ -81,8 +78,9 @@ public extension NSProgress {
|
||||
@objc
|
||||
private final class ProgressObserver: NSObject {
|
||||
|
||||
private unowned let progress: NSProgress
|
||||
private var progressHandler: ((progress: NSProgress) -> Void)? {
|
||||
private unowned let progress: Progress
|
||||
|
||||
fileprivate var progressHandler: ((_ progress: Progress) -> Void)? {
|
||||
|
||||
didSet {
|
||||
|
||||
@@ -96,19 +94,19 @@ private final class ProgressObserver: NSObject {
|
||||
|
||||
self.progress.addObserver(
|
||||
self,
|
||||
forKeyPath: "fractionCompleted",
|
||||
options: [.Initial, .New],
|
||||
forKeyPath: #keyPath(Progress.fractionCompleted),
|
||||
options: [.initial, .new],
|
||||
context: nil
|
||||
)
|
||||
}
|
||||
else {
|
||||
|
||||
self.progress.removeObserver(self, forKeyPath: "fractionCompleted")
|
||||
self.progress.removeObserver(self, forKeyPath: #keyPath(Progress.fractionCompleted))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private init(_ progress: NSProgress) {
|
||||
fileprivate init(_ progress: Progress) {
|
||||
|
||||
self.progress = progress
|
||||
super.init()
|
||||
@@ -119,20 +117,22 @@ private final class ProgressObserver: NSObject {
|
||||
if let _ = self.progressHandler {
|
||||
|
||||
self.progressHandler = nil
|
||||
self.progress.removeObserver(self, forKeyPath: "fractionCompleted")
|
||||
self.progress.removeObserver(self, forKeyPath: #keyPath(Progress.fractionCompleted))
|
||||
}
|
||||
}
|
||||
|
||||
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
|
||||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
|
||||
guard let progress = object as? NSProgress where progress == self.progress && keyPath == "fractionCompleted" else {
|
||||
|
||||
return
|
||||
guard let progress = object as? Progress,
|
||||
progress == self.progress,
|
||||
keyPath == #keyPath(Progress.fractionCompleted) else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
GCDQueue.Main.async { [weak self] () -> Void in
|
||||
DispatchQueue.main.async { [weak self] () -> Void in
|
||||
|
||||
self?.progressHandler?(progress: progress)
|
||||
self?.progressHandler?(progress)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,9 +24,6 @@
|
||||
//
|
||||
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - CoreStore
|
||||
@@ -45,10 +42,10 @@ public enum CoreStore {
|
||||
|
||||
get {
|
||||
|
||||
self.defaultStackBarrierQueue.barrierSync {
|
||||
|
||||
self.defaultStackBarrierQueue.sync(flags: .barrier) {
|
||||
|
||||
if self.defaultStackInstance == nil {
|
||||
|
||||
|
||||
self.defaultStackInstance = DataStack()
|
||||
}
|
||||
}
|
||||
@@ -56,7 +53,7 @@ public enum CoreStore {
|
||||
}
|
||||
set {
|
||||
|
||||
self.defaultStackBarrierQueue.barrierAsync {
|
||||
self.defaultStackBarrierQueue.async(flags: .barrier) {
|
||||
|
||||
self.defaultStackInstance = newValue
|
||||
}
|
||||
@@ -66,7 +63,7 @@ public enum CoreStore {
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private static let defaultStackBarrierQueue = GCDQueue.createConcurrent("com.coreStore.defaultStackBarrierQueue")
|
||||
private static let defaultStackBarrierQueue = DispatchQueue.concurrent("com.coreStore.defaultStackBarrierQueue")
|
||||
|
||||
private static var defaultStackInstance: DataStack?
|
||||
}
|
||||
|
||||
@@ -32,59 +32,117 @@ import CoreData
|
||||
/**
|
||||
All errors thrown from CoreStore are expressed in `CoreStoreError` enum values.
|
||||
*/
|
||||
public enum CoreStoreError: ErrorType, Hashable {
|
||||
public enum CoreStoreError: Error, CustomNSError, Hashable {
|
||||
|
||||
/**
|
||||
A failure occured because of an unknown error.
|
||||
*/
|
||||
case Unknown
|
||||
case unknown
|
||||
|
||||
/**
|
||||
The `NSPersistentStore` could not be initialized because another store existed at the specified `NSURL`.
|
||||
*/
|
||||
case DifferentStorageExistsAtURL(existingPersistentStoreURL: NSURL)
|
||||
case differentStorageExistsAtURL(existingPersistentStoreURL: URL)
|
||||
|
||||
/**
|
||||
An `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case MappingModelNotFound(localStoreURL: NSURL, targetModel: NSManagedObjectModel, targetModelVersion: String)
|
||||
case mappingModelNotFound(localStoreURL: URL, targetModel: NSManagedObjectModel, targetModelVersion: String)
|
||||
|
||||
/**
|
||||
Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case ProgressiveMigrationRequired(localStoreURL: NSURL)
|
||||
case progressiveMigrationRequired(localStoreURL: URL)
|
||||
|
||||
/**
|
||||
An internal SDK call failed with the specified `NSError`.
|
||||
*/
|
||||
case InternalError(NSError: NSError)
|
||||
case internalError(NSError: NSError)
|
||||
|
||||
|
||||
// MARK: ErrorType
|
||||
// MARK: CustomNSError
|
||||
|
||||
public var _domain: String {
|
||||
public static var errorDomain: String {
|
||||
|
||||
return CoreStoreErrorDomain
|
||||
}
|
||||
|
||||
public var _code: Int {
|
||||
public var errorCode: Int {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Unknown:
|
||||
return CoreStoreErrorCode.UnknownError.rawValue
|
||||
case .unknown:
|
||||
return CoreStoreErrorCode.unknownError.rawValue
|
||||
|
||||
case .DifferentStorageExistsAtURL:
|
||||
return CoreStoreErrorCode.DifferentPersistentStoreExistsAtURL.rawValue
|
||||
case .differentStorageExistsAtURL:
|
||||
return CoreStoreErrorCode.differentStorageExistsAtURL.rawValue
|
||||
|
||||
case .MappingModelNotFound:
|
||||
return CoreStoreErrorCode.MappingModelNotFound.rawValue
|
||||
case .mappingModelNotFound:
|
||||
return CoreStoreErrorCode.mappingModelNotFound.rawValue
|
||||
|
||||
case .ProgressiveMigrationRequired:
|
||||
return CoreStoreErrorCode.ProgressiveMigrationRequired.rawValue
|
||||
case .progressiveMigrationRequired:
|
||||
return CoreStoreErrorCode.progressiveMigrationRequired.rawValue
|
||||
|
||||
case .InternalError:
|
||||
return CoreStoreErrorCode.InternalError.rawValue
|
||||
case .internalError:
|
||||
return CoreStoreErrorCode.internalError.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
public var errorUserInfo: [String : Any] {
|
||||
|
||||
switch self {
|
||||
|
||||
case .unknown:
|
||||
return [:]
|
||||
|
||||
case .differentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
return [
|
||||
"existingPersistentStoreURL": existingPersistentStoreURL
|
||||
]
|
||||
|
||||
case .mappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
return [
|
||||
"localStoreURL": localStoreURL,
|
||||
"targetModel": targetModel,
|
||||
"targetModelVersion": targetModelVersion
|
||||
]
|
||||
|
||||
case .progressiveMigrationRequired(let localStoreURL):
|
||||
return [
|
||||
"localStoreURL": localStoreURL
|
||||
]
|
||||
|
||||
case .internalError(let NSError):
|
||||
return [
|
||||
"NSError": NSError
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,19 +154,19 @@ public enum CoreStoreError: ErrorType, Hashable {
|
||||
let code = self._code
|
||||
switch self {
|
||||
|
||||
case .Unknown:
|
||||
case .unknown:
|
||||
return code.hashValue
|
||||
|
||||
case .DifferentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
case .differentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
return code.hashValue ^ existingPersistentStoreURL.hashValue
|
||||
|
||||
case .MappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
case .mappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
return code.hashValue ^ localStoreURL.hashValue ^ targetModel.hashValue ^ targetModelVersion.hashValue
|
||||
|
||||
case .ProgressiveMigrationRequired(let localStoreURL):
|
||||
case .progressiveMigrationRequired(let localStoreURL):
|
||||
return code.hashValue ^ localStoreURL.hashValue
|
||||
|
||||
case .InternalError(let NSError):
|
||||
case .internalError(let NSError):
|
||||
return code.hashValue ^ NSError.hashValue
|
||||
}
|
||||
}
|
||||
@@ -116,37 +174,9 @@ public enum CoreStoreError: ErrorType, Hashable {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal init(_ error: ErrorType?) {
|
||||
internal init(_ error: Error?) {
|
||||
|
||||
self = error.flatMap { $0.bridgeToSwift } ?? .Unknown
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CoreStoreError: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
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
|
||||
self = error.flatMap { $0.bridgeToSwift } ?? .unknown
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,27 +200,27 @@ public enum CoreStoreErrorCode: Int {
|
||||
/**
|
||||
A failure occured because of an unknown error.
|
||||
*/
|
||||
case UnknownError
|
||||
case unknownError
|
||||
|
||||
/**
|
||||
The `NSPersistentStore` could note be initialized because another store existed at the specified `NSURL`.
|
||||
*/
|
||||
case DifferentPersistentStoreExistsAtURL
|
||||
case differentStorageExistsAtURL
|
||||
|
||||
/**
|
||||
An `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case MappingModelNotFound
|
||||
case mappingModelNotFound
|
||||
|
||||
/**
|
||||
Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case ProgressiveMigrationRequired
|
||||
case progressiveMigrationRequired
|
||||
|
||||
/**
|
||||
An internal SDK call failed with the specified "NSError" userInfo key.
|
||||
*/
|
||||
case InternalError
|
||||
case internalError
|
||||
}
|
||||
|
||||
|
||||
@@ -208,20 +238,4 @@ public extension NSError {
|
||||
|| code == NSMigrationError)
|
||||
&& self.domain == NSCocoaErrorDomain
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `CoreStoreError` enum values instead.
|
||||
|
||||
If the error's domain is equal to `CoreStoreErrorDomain`, returns the associated `CoreStoreErrorCode`. For other domains, returns `nil`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use CoreStoreError enum values instead.")
|
||||
public var coreStoreErrorCode: CoreStoreErrorCode? {
|
||||
|
||||
return (self.domain == CoreStoreErrorDomain
|
||||
? CoreStoreErrorCode(rawValue: self.code)
|
||||
: nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,46 @@ import CoreData
|
||||
|
||||
// MARK: - DataTransaction
|
||||
|
||||
public extension BaseDataTransaction {
|
||||
extension BaseDataTransaction: FetchableSource, QueryableSource {
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
@discardableResult
|
||||
public func deleteAll<T: NSManagedObject>(_ from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
@discardableResult
|
||||
public func deleteAll<T: NSManagedObject>(_ from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
|
||||
// MARK: FetchableSource
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the transaction's context from a reference created from a transaction or from a different managed object context.
|
||||
@@ -37,17 +76,9 @@ public extension BaseDataTransaction {
|
||||
- parameter object: a reference to the object created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject>(object: T) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject>(_ object: T) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
return (try self.context.existingObjectWithID(object.objectID) as! T)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.context.fetchExisting(object)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,17 +87,9 @@ public extension BaseDataTransaction {
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject>(objectID: NSManagedObjectID) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject>(_ objectID: NSManagedObjectID) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
return (try self.context.existingObjectWithID(objectID) as! T)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.context.fetchExisting(objectID)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,10 +98,9 @@ public extension BaseDataTransaction {
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
|
||||
|
||||
return objects.flatMap { (try? self.context.existingObjectWithID($0.objectID)) as? T }
|
||||
return self.context.fetchExisting(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,10 +109,9 @@ public extension BaseDataTransaction {
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
|
||||
|
||||
return objectIDs.flatMap { (try? self.context.existingObjectWithID($0)) as? T }
|
||||
return self.context.fetchExisting(objectIDs)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,10 +121,13 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
return self.fetchOne(from, fetchClauses)
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.context.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,8 +137,7 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
@@ -130,10 +153,13 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
return self.fetchAll(from, fetchClauses)
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.context.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,8 +169,7 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
@@ -160,10 +185,13 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
return self.fetchCount(from, fetchClauses)
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.context.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,14 +201,12 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -191,10 +217,13 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
return self.fetchObjectID(from, fetchClauses)
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.context.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,8 +233,7 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
@@ -221,21 +249,7 @@ public extension BaseDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
@@ -245,38 +259,23 @@ public extension BaseDataTransaction {
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
public func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.deleteAll(from, deleteClauses)
|
||||
return self.context.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
public func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
// MARK: QueryableSource
|
||||
|
||||
/**
|
||||
Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
@@ -288,14 +287,12 @@ public extension BaseDataTransaction {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to query from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -309,14 +306,12 @@ public extension BaseDataTransaction {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to query from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -330,14 +325,12 @@ public extension BaseDataTransaction {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to query from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -351,14 +344,23 @@ public extension BaseDataTransaction {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to query from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
|
||||
// MARK: FetchableSource, QueryableSource
|
||||
|
||||
/**
|
||||
The internal `NSManagedObjectContext` managed by this instance. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.
|
||||
*/
|
||||
public func internalContext() -> NSManagedObjectContext {
|
||||
|
||||
return self.context
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import CoreData
|
||||
/**
|
||||
A `From` clause specifies the source entity and source persistent store for fetch and query methods. A common usage is to just indicate the entity:
|
||||
```
|
||||
let person = transaction.fetchOne(From(MyPersonEntity))
|
||||
let person = transaction.fetchOne(From<MyPersonEntity>())
|
||||
```
|
||||
For cases where multiple `NSPersistentStore`s contain the same entity, the source configuration's name needs to be specified as well:
|
||||
```
|
||||
@@ -58,7 +58,7 @@ public struct From<T: NSManagedObject> {
|
||||
let people = transaction.fetchAll(From<MyPersonEntity>())
|
||||
```
|
||||
*/
|
||||
public init(){
|
||||
public init() {
|
||||
|
||||
self.init(entityClass: T.self, configurations: nil)
|
||||
}
|
||||
@@ -68,7 +68,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From<MyPersonEntity>())
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` type
|
||||
*/
|
||||
public init(_ entity: T.Type) {
|
||||
@@ -81,14 +80,13 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From<MyPersonEntity>())
|
||||
```
|
||||
|
||||
- parameter entityClass: the associated `NSManagedObject` entity class
|
||||
*/
|
||||
public init(_ entityClass: AnyClass) {
|
||||
|
||||
CoreStore.assert(
|
||||
entityClass is T.Type,
|
||||
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
|
||||
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
|
||||
)
|
||||
self.init(entityClass: entityClass, configurations: nil)
|
||||
}
|
||||
@@ -98,7 +96,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From<MyPersonEntity>(nil, "Configuration1"))
|
||||
```
|
||||
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
|
||||
*/
|
||||
@@ -112,7 +109,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From<MyPersonEntity>(["Configuration1", "Configuration2"]))
|
||||
```
|
||||
|
||||
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
public init(_ configurations: [String?]) {
|
||||
@@ -125,7 +121,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From(MyPersonEntity.self, nil, "Configuration1"))
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` type
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
|
||||
@@ -140,7 +135,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From(MyPersonEntity.self, ["Configuration1", "Configuration1"]))
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` type
|
||||
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
@@ -154,7 +148,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From(MyPersonEntity.self, nil, "Configuration1"))
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
|
||||
@@ -163,7 +156,7 @@ public struct From<T: NSManagedObject> {
|
||||
|
||||
CoreStore.assert(
|
||||
entityClass is T.Type,
|
||||
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
|
||||
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
|
||||
)
|
||||
self.init(entityClass: entityClass, configurations: [configuration] + otherConfigurations)
|
||||
}
|
||||
@@ -173,7 +166,6 @@ public struct From<T: NSManagedObject> {
|
||||
```
|
||||
let people = transaction.fetchAll(From(MyPersonEntity.self, ["Configuration1", "Configuration1"]))
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
@@ -181,7 +173,7 @@ public struct From<T: NSManagedObject> {
|
||||
|
||||
CoreStore.assert(
|
||||
entityClass is T.Type,
|
||||
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
|
||||
"Attempted to create generic type \(cs_typeName(From<T>.self)) with entity class \(cs_typeName(entityClass))"
|
||||
)
|
||||
self.init(entityClass: entityClass, configurations: configurations)
|
||||
}
|
||||
@@ -189,8 +181,7 @@ public struct From<T: NSManagedObject> {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@warn_unused_result
|
||||
internal func applyToFetchRequest(fetchRequest: NSFetchRequest, context: NSManagedObjectContext, applyAffectedStores: Bool = true) -> Bool {
|
||||
internal func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>, context: NSManagedObjectContext, applyAffectedStores: Bool = true) -> Bool {
|
||||
|
||||
fetchRequest.entity = context.entityDescriptionForEntityClass(self.entityClass)
|
||||
guard applyAffectedStores else {
|
||||
@@ -202,15 +193,15 @@ public struct From<T: NSManagedObject> {
|
||||
return true
|
||||
}
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
.warning,
|
||||
message: "Attempted to perform a fetch but could not find any persistent store for the entity \(cs_typeName(fetchRequest.entityName))"
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
internal func applyAffectedStoresForFetchedRequest(fetchRequest: NSFetchRequest, context: NSManagedObjectContext) -> Bool {
|
||||
internal func applyAffectedStoresForFetchedRequest<U: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<U>, context: NSManagedObjectContext) -> Bool {
|
||||
|
||||
let stores = self.findPersistentStores(context: context)
|
||||
let stores = self.findPersistentStores(context)
|
||||
fetchRequest.affectedStores = stores
|
||||
return stores?.isEmpty == false
|
||||
}
|
||||
@@ -227,7 +218,7 @@ public struct From<T: NSManagedObject> {
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?
|
||||
private let findPersistentStores: (_ context: NSManagedObjectContext) -> [NSPersistentStore]?
|
||||
|
||||
private init(entityClass: AnyClass, configurations: [String?]?) {
|
||||
|
||||
@@ -253,121 +244,10 @@ public struct From<T: NSManagedObject> {
|
||||
}
|
||||
}
|
||||
|
||||
private init(entityClass: AnyClass, configurations: [String?]?, findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?) {
|
||||
private init(entityClass: AnyClass, configurations: [String?]?, findPersistentStores: @escaping (_ context: NSManagedObjectContext) -> [NSPersistentStore]?) {
|
||||
|
||||
self.entityClass = entityClass
|
||||
self.configurations = configurations
|
||||
self.findPersistentStores = findPersistentStores
|
||||
}
|
||||
|
||||
|
||||
// MARK: Obsolete
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ storeURLs: [NSURL]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entity: T.Type, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entity: T.Type, _ storeURLs: [NSURL]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entityClass: AnyClass, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entityClass: AnyClass, _ storeURLs: [NSURL]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ persistentStores: [NSPersistentStore]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entity: T.Type, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entity: T.Type, _ persistentStores: [NSPersistentStore]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entityClass: AnyClass, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
|
||||
/**
|
||||
Obsolete. Use initializers that accept configuration names.
|
||||
*/
|
||||
@available(*, obsoleted=2.0.0, message="Use initializers that accept configuration names.")
|
||||
public init(_ entityClass: AnyClass, _ persistentStores: [NSPersistentStore]) {
|
||||
|
||||
CoreStore.abort("Use initializers that accept configuration names.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,13 +71,13 @@ public struct GroupBy: QueryClause, Hashable {
|
||||
|
||||
// MARK: QueryClause
|
||||
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>) {
|
||||
|
||||
if let keyPaths = fetchRequest.propertiesToGroupBy as? [String] where keyPaths != self.keyPaths {
|
||||
if let keyPaths = fetchRequest.propertiesToGroupBy as? [String], keyPaths != self.keyPaths {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
message: "An existing \"propertiesToGroupBy\" for the \(cs_typeName(NSFetchRequest)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
.warning,
|
||||
message: "An existing \"propertiesToGroupBy\" for the \(cs_typeName(NSFetchRequest<ResultType>.self)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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,12 +100,3 @@ public struct GroupBy: QueryClause, Hashable {
|
||||
return (self.keyPaths as NSArray).hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - GroupBy: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: GroupBy, rhs: GroupBy) -> Bool {
|
||||
|
||||
return lhs.keyPaths == rhs.keyPaths
|
||||
}
|
||||
|
||||
@@ -27,12 +27,12 @@ import Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
public func +(left: OrderBy, right: OrderBy) -> OrderBy {
|
||||
public func + (left: OrderBy, right: OrderBy) -> OrderBy {
|
||||
|
||||
return OrderBy(left.sortDescriptors + right.sortDescriptors)
|
||||
}
|
||||
|
||||
public func +=(inout left: OrderBy, right: OrderBy) {
|
||||
public func += (left: inout OrderBy, right: OrderBy) {
|
||||
|
||||
left = left + right
|
||||
}
|
||||
@@ -53,12 +53,12 @@ public enum SortKey {
|
||||
/**
|
||||
Indicates that the `KeyPath` should be sorted in ascending order
|
||||
*/
|
||||
case Ascending(KeyPath)
|
||||
case ascending(KeyPath)
|
||||
|
||||
/**
|
||||
Indicates that the `KeyPath` should be sorted in descending order
|
||||
*/
|
||||
case Descending(KeyPath)
|
||||
case descending(KeyPath)
|
||||
}
|
||||
|
||||
|
||||
@@ -114,10 +114,10 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
|
||||
switch sortKey {
|
||||
|
||||
case .Ascending(let keyPath):
|
||||
case .ascending(let keyPath):
|
||||
return NSSortDescriptor(key: keyPath, ascending: true)
|
||||
|
||||
case .Descending(let keyPath):
|
||||
case .descending(let keyPath):
|
||||
return NSSortDescriptor(key: keyPath, ascending: false)
|
||||
}
|
||||
}
|
||||
@@ -138,13 +138,13 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
|
||||
// MARK: FetchClause, QueryClause, DeleteClause
|
||||
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>) {
|
||||
|
||||
if let sortDescriptors = fetchRequest.sortDescriptors where sortDescriptors != self.sortDescriptors {
|
||||
if let sortDescriptors = fetchRequest.sortDescriptors, sortDescriptors != self.sortDescriptors {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
message: "Existing sortDescriptors for the \(cs_typeName(NSFetchRequest)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
.warning,
|
||||
message: "Existing sortDescriptors for the \(cs_typeName(fetchRequest)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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,12 +167,3 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
return (self.sortDescriptors as NSArray).hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - OrderBy: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: OrderBy, rhs: OrderBy) -> Bool {
|
||||
|
||||
return lhs.sortDescriptors == rhs.sortDescriptors
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public protocol SelectValueResultType: SelectResultType {
|
||||
|
||||
static var attributeType: NSAttributeType { get }
|
||||
|
||||
static func fromResultObject(result: AnyObject) -> Self?
|
||||
static func fromResultObject(_ result: Any) -> Self?
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public protocol SelectValueResultType: SelectResultType {
|
||||
*/
|
||||
public protocol SelectAttributesResultType: SelectResultType {
|
||||
|
||||
static func fromResultObjects(result: [AnyObject]) -> [[NSString: AnyObject]]
|
||||
static func fromResultObjects(_ result: [Any]) -> [[String: Any]]
|
||||
}
|
||||
|
||||
|
||||
@@ -64,54 +64,52 @@ public protocol SelectAttributesResultType: SelectResultType {
|
||||
/**
|
||||
The `SelectTerm` is passed to the `Select` clause to indicate the attributes/aggregate keys to be queried.
|
||||
*/
|
||||
public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
public enum SelectTerm: ExpressibleByStringLiteral, Hashable {
|
||||
|
||||
/**
|
||||
Provides a `SelectTerm` to a `Select` clause for querying an entity attribute. A shorter way to do the same is to assign from the string keypath directly:
|
||||
```
|
||||
let fullName = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<String>(.Attribute("fullName")),
|
||||
From<MyPersonEntity>(),
|
||||
Select<String>(.attribute("fullName")),
|
||||
Where("employeeID", isEqualTo: 1111)
|
||||
)
|
||||
```
|
||||
is equivalent to:
|
||||
```
|
||||
let fullName = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
From<MyPersonEntity>(),
|
||||
Select<String>("fullName"),
|
||||
Where("employeeID", isEqualTo: 1111)
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying an entity attribute
|
||||
*/
|
||||
public static func Attribute(keyPath: KeyPath) -> SelectTerm {
|
||||
public static func attribute(_ keyPath: KeyPath) -> SelectTerm {
|
||||
|
||||
return ._Attribute(keyPath)
|
||||
return ._attribute(keyPath)
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a `SelectTerm` to a `Select` clause for querying the average value of an attribute.
|
||||
```
|
||||
let averageAge = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Average("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.average("age"))
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "average(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the average value of an attribute
|
||||
*/
|
||||
public static func Average(keyPath: KeyPath, As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func average(_ keyPath: KeyPath, as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Aggregate(
|
||||
return ._aggregate(
|
||||
function: "average:",
|
||||
keyPath: keyPath,
|
||||
alias: alias ?? "average(\(keyPath))",
|
||||
nativeType: .DecimalAttributeType
|
||||
nativeType: .decimalAttributeType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -119,22 +117,21 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
Provides a `SelectTerm` to a `Select` clause for a count query.
|
||||
```
|
||||
let numberOfEmployees = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Count("employeeID"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.count("employeeID"))
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "count(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for a count query
|
||||
*/
|
||||
public static func Count(keyPath: KeyPath, As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func count(_ keyPath: KeyPath, as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Aggregate(
|
||||
return ._aggregate(
|
||||
function: "count:",
|
||||
keyPath: keyPath,
|
||||
alias: alias ?? "count(\(keyPath))",
|
||||
nativeType: .Integer64AttributeType
|
||||
nativeType: .integer64AttributeType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -142,22 +139,21 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
Provides a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute.
|
||||
```
|
||||
let maximumAge = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Maximum("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.maximum("age"))
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "max(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute
|
||||
*/
|
||||
public static func Maximum(keyPath: KeyPath, As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func maximum(_ keyPath: KeyPath, as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Aggregate(
|
||||
return ._aggregate(
|
||||
function: "max:",
|
||||
keyPath: keyPath,
|
||||
alias: alias ?? "max(\(keyPath))",
|
||||
nativeType: .UndefinedAttributeType
|
||||
nativeType: .undefinedAttributeType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -165,22 +161,21 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
Provides a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute.
|
||||
```
|
||||
let minimumAge = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Minimum("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.minimum("age"))
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "min(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute
|
||||
*/
|
||||
public static func Minimum(keyPath: KeyPath, As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func minimum(_ keyPath: KeyPath, as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Aggregate(
|
||||
return ._aggregate(
|
||||
function: "min:",
|
||||
keyPath: keyPath,
|
||||
alias: alias ?? "min(\(keyPath))",
|
||||
nativeType: .UndefinedAttributeType
|
||||
nativeType: .undefinedAttributeType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -188,22 +183,21 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
Provides a `SelectTerm` to a `Select` clause for querying the sum value for an attribute.
|
||||
```
|
||||
let totalAge = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Sum("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.sum("age"))
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "sum(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute
|
||||
*/
|
||||
public static func Sum(keyPath: KeyPath, As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func sum(_ keyPath: KeyPath, as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Aggregate(
|
||||
return ._aggregate(
|
||||
function: "sum:",
|
||||
keyPath: keyPath,
|
||||
alias: alias ?? "sum(\(keyPath))",
|
||||
nativeType: .DecimalAttributeType
|
||||
nativeType: .decimalAttributeType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -211,40 +205,64 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
Provides a `SelectTerm` to a `Select` clause for querying the `NSManagedObjectID`.
|
||||
```
|
||||
let objectID = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
From<MyPersonEntity>(),
|
||||
Select<NSManagedObjectID>(),
|
||||
Where("employeeID", isEqualTo: 1111)
|
||||
)
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "objecID" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute
|
||||
*/
|
||||
public static func ObjectID(As alias: KeyPath? = nil) -> SelectTerm {
|
||||
public static func objectID(as alias: KeyPath? = nil) -> SelectTerm {
|
||||
|
||||
return ._Identity(
|
||||
return ._identity(
|
||||
alias: alias ?? "objectID",
|
||||
nativeType: .ObjectIDAttributeType
|
||||
nativeType: .objectIDAttributeType
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// MARK: StringLiteralConvertible
|
||||
// MARK: ExpressibleByStringLiteral
|
||||
|
||||
public init(stringLiteral value: KeyPath) {
|
||||
|
||||
self = ._Attribute(value)
|
||||
self = ._attribute(value)
|
||||
}
|
||||
|
||||
public init(unicodeScalarLiteral value: KeyPath) {
|
||||
|
||||
self = ._Attribute(value)
|
||||
self = ._attribute(value)
|
||||
}
|
||||
|
||||
public init(extendedGraphemeClusterLiteral value: KeyPath) {
|
||||
|
||||
self = ._Attribute(value)
|
||||
self = ._attribute(value)
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,13 +272,13 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
|
||||
switch self {
|
||||
|
||||
case ._Attribute(let keyPath):
|
||||
case ._attribute(let keyPath):
|
||||
return 0 ^ keyPath.hashValue
|
||||
|
||||
case ._Aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
case ._aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
return 1 ^ function.hashValue ^ keyPath.hashValue ^ alias.hashValue ^ nativeType.hashValue
|
||||
|
||||
case ._Identity(let alias, let nativeType):
|
||||
case ._identity(let alias, let nativeType):
|
||||
return 3 ^ alias.hashValue ^ nativeType.hashValue
|
||||
}
|
||||
}
|
||||
@@ -268,35 +286,9 @@ public enum SelectTerm: StringLiteralConvertible, Hashable {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
case _Attribute(KeyPath)
|
||||
case _Aggregate(function: String, keyPath: KeyPath, alias: String, nativeType: NSAttributeType)
|
||||
case _Identity(alias: String, nativeType: NSAttributeType)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - SelectTerm: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
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
|
||||
}
|
||||
case _attribute(KeyPath)
|
||||
case _aggregate(function: String, keyPath: KeyPath, alias: String, nativeType: NSAttributeType)
|
||||
case _identity(alias: String, nativeType: NSAttributeType)
|
||||
}
|
||||
|
||||
|
||||
@@ -308,15 +300,15 @@ public func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
|
||||
You can bind the return type by specializing the initializer:
|
||||
```
|
||||
let maximumAge = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select<Int>(.Maximum("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select<Int>(.maximum("age"))
|
||||
)
|
||||
```
|
||||
or by casting the type of the return value:
|
||||
```
|
||||
let maximumAge: Int = CoreStore.queryValue(
|
||||
From(MyPersonEntity),
|
||||
Select(.Maximum("age"))
|
||||
From<MyPersonEntity>(),
|
||||
Select(.maximum("age"))
|
||||
)
|
||||
```
|
||||
Valid return types depend on the query:
|
||||
@@ -330,13 +322,14 @@ public func == (lhs: SelectTerm, rhs: SelectTerm) -> Bool {
|
||||
- `Double`
|
||||
- `Float`
|
||||
- `String`
|
||||
- `Date`
|
||||
- `Data`
|
||||
- `NSNumber`
|
||||
- `NSString`
|
||||
- `NSDecimalNumber`
|
||||
- `NSDate`
|
||||
- `NSData`
|
||||
- `NSManagedObjectID`
|
||||
- `NSString`
|
||||
- for `queryAttributes(...)` methods:
|
||||
- `NSDictionary`
|
||||
|
||||
@@ -371,11 +364,19 @@ 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 {
|
||||
|
||||
return self.selectTerms.map { $0.hashValue }.reduce(0, combine: ^)
|
||||
return self.selectTerms.map { $0.hashValue }.reduce(0, ^)
|
||||
}
|
||||
|
||||
|
||||
@@ -388,36 +389,27 @@ public extension Select where T: NSManagedObjectID {
|
||||
|
||||
public init() {
|
||||
|
||||
self.init(.ObjectID())
|
||||
self.init(.objectID())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Select: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == <T: SelectResultType, U: SelectResultType>(lhs: Select<T>, rhs: Select<U>) -> Bool {
|
||||
|
||||
return lhs.selectTerms == rhs.selectTerms
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Bool: SelectValueResultType
|
||||
|
||||
extension Bool: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .BooleanAttributeType
|
||||
return .booleanAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Bool? {
|
||||
public static func fromResultObject(_ result: Any) -> Bool? {
|
||||
switch result {
|
||||
|
||||
case let decimal as NSDecimalNumber:
|
||||
// iOS: NSDecimalNumber(string: "0.5").boolValue // true
|
||||
// OSX: NSDecimalNumber(string: "0.5").boolValue // false
|
||||
return NSNumber(double: decimal.doubleValue).boolValue
|
||||
return NSNumber(value: decimal.doubleValue).boolValue
|
||||
|
||||
case let number as NSNumber:
|
||||
return number.boolValue
|
||||
@@ -435,12 +427,12 @@ extension Int8: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int8? {
|
||||
public static func fromResultObject(_ result: Any) -> Int8? {
|
||||
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
guard let value = (result as? NSNumber)?.int64Value else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -455,12 +447,12 @@ extension Int16: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int16? {
|
||||
public static func fromResultObject(_ result: Any) -> Int16? {
|
||||
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
guard let value = (result as? NSNumber)?.int64Value else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -475,12 +467,12 @@ extension Int32: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int32? {
|
||||
public static func fromResultObject(_ result: Any) -> Int32? {
|
||||
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
guard let value = (result as? NSNumber)?.int64Value else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -495,12 +487,12 @@ extension Int64: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int64? {
|
||||
public static func fromResultObject(_ result: Any) -> Int64? {
|
||||
|
||||
return (result as? NSNumber)?.longLongValue
|
||||
return (result as? NSNumber)?.int64Value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,12 +503,12 @@ extension Int: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int? {
|
||||
public static func fromResultObject(_ result: Any) -> Int? {
|
||||
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
guard let value = (result as? NSNumber)?.int64Value else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -531,10 +523,10 @@ extension Double: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .DoubleAttributeType
|
||||
return .doubleAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Double? {
|
||||
public static func fromResultObject(_ result: Any) -> Double? {
|
||||
|
||||
return (result as? NSNumber)?.doubleValue
|
||||
}
|
||||
@@ -547,10 +539,10 @@ extension Float: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .FloatAttributeType
|
||||
return .floatAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Float? {
|
||||
public static func fromResultObject(_ result: Any) -> Float? {
|
||||
|
||||
return (result as? NSNumber)?.floatValue
|
||||
}
|
||||
@@ -563,12 +555,44 @@ extension String: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .StringAttributeType
|
||||
return .stringAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> String? {
|
||||
public static func fromResultObject(_ result: Any) -> String? {
|
||||
|
||||
return result as? NSString as? String
|
||||
return result as? String
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date: SelectValueResultType
|
||||
|
||||
extension Date: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .dateAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(_ result: Any) -> Date? {
|
||||
|
||||
return result as? Date
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data: SelectValueResultType
|
||||
|
||||
extension Data: SelectValueResultType {
|
||||
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .binaryDataAttributeType
|
||||
}
|
||||
|
||||
public static func fromResultObject(_ result: Any) -> Data? {
|
||||
|
||||
return result as? Data
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,12 +603,12 @@ extension NSNumber: SelectValueResultType {
|
||||
|
||||
public class var attributeType: NSAttributeType {
|
||||
|
||||
return .Integer64AttributeType
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
public class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSNumber>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSNumber>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -599,12 +623,12 @@ extension NSString: SelectValueResultType {
|
||||
|
||||
public class var attributeType: NSAttributeType {
|
||||
|
||||
return .StringAttributeType
|
||||
return .stringAttributeType
|
||||
}
|
||||
|
||||
public class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSString>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSString>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -619,12 +643,12 @@ extension NSDecimalNumber {
|
||||
|
||||
public override class var attributeType: NSAttributeType {
|
||||
|
||||
return .DecimalAttributeType
|
||||
return .decimalAttributeType
|
||||
}
|
||||
|
||||
public override class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public override class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSDecimalNumber>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSDecimalNumber>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -637,14 +661,14 @@ extension NSDecimalNumber {
|
||||
|
||||
extension NSDate: SelectValueResultType {
|
||||
|
||||
public class var attributeType: NSAttributeType {
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .DateAttributeType
|
||||
return .dateAttributeType
|
||||
}
|
||||
|
||||
public class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSDate>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSDate>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -657,14 +681,14 @@ extension NSDate: SelectValueResultType {
|
||||
|
||||
extension NSData: SelectValueResultType {
|
||||
|
||||
public class var attributeType: NSAttributeType {
|
||||
public static var attributeType: NSAttributeType {
|
||||
|
||||
return .BinaryDataAttributeType
|
||||
return .binaryDataAttributeType
|
||||
}
|
||||
|
||||
public class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSData>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSData>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -679,12 +703,12 @@ extension NSManagedObjectID: SelectValueResultType {
|
||||
|
||||
public class var attributeType: NSAttributeType {
|
||||
|
||||
return .ObjectIDAttributeType
|
||||
return .objectIDAttributeType
|
||||
}
|
||||
|
||||
public class func fromResultObject(result: AnyObject) -> Self? {
|
||||
public class func fromResultObject(_ result: Any) -> Self? {
|
||||
|
||||
func forceCast<T: NSManagedObjectID>(object: AnyObject) -> T? {
|
||||
func forceCast<T: NSManagedObjectID>(_ object: Any) -> T? {
|
||||
|
||||
return (object as? T)
|
||||
}
|
||||
@@ -699,25 +723,25 @@ extension NSDictionary: SelectAttributesResultType {
|
||||
|
||||
// MARK: SelectAttributesResultType
|
||||
|
||||
public class func fromResultObjects(result: [AnyObject]) -> [[NSString: AnyObject]] {
|
||||
public class func fromResultObjects(_ result: [Any]) -> [[String: Any]] {
|
||||
|
||||
return result as! [[NSString: AnyObject]]
|
||||
return result as! [[String: Any]]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Internal
|
||||
|
||||
internal extension CollectionType where Generator.Element == SelectTerm {
|
||||
internal extension Collection where Iterator.Element == SelectTerm {
|
||||
|
||||
internal func applyToFetchRequest<T>(fetchRequest: NSFetchRequest, owner: T) {
|
||||
internal func applyToFetchRequest<T>(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>, owner: T) {
|
||||
|
||||
fetchRequest.includesPendingChanges = false
|
||||
fetchRequest.resultType = .DictionaryResultType
|
||||
fetchRequest.resultType = .dictionaryResultType
|
||||
|
||||
func attributeDescriptionForKeyPath(keyPath: String, inEntity entity: NSEntityDescription) -> NSAttributeDescription? {
|
||||
func attributeDescription(for keyPath: String, in entity: NSEntityDescription) -> NSAttributeDescription? {
|
||||
|
||||
let components = keyPath.componentsSeparatedByString(".")
|
||||
let components = keyPath.components(separatedBy: ".")
|
||||
switch components.count {
|
||||
|
||||
case 0:
|
||||
@@ -731,39 +755,39 @@ internal extension CollectionType where Generator.Element == SelectTerm {
|
||||
|
||||
return nil
|
||||
}
|
||||
return attributeDescriptionForKeyPath(
|
||||
components.dropFirst().joinWithSeparator("."),
|
||||
inEntity: relationship.entity
|
||||
return attributeDescription(
|
||||
for: components.dropFirst().joined(separator: "."),
|
||||
in: relationship.entity
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var propertiesToFetch = [AnyObject]()
|
||||
var propertiesToFetch = [Any]()
|
||||
for term in self {
|
||||
|
||||
switch term {
|
||||
|
||||
case ._Attribute(let keyPath):
|
||||
case ._attribute(let keyPath):
|
||||
let entityDescription = fetchRequest.entity!
|
||||
if let attributeDescription = attributeDescriptionForKeyPath(keyPath, inEntity: entityDescription) {
|
||||
if let attributeDescription = attributeDescription(for: keyPath, in: entityDescription) {
|
||||
|
||||
propertiesToFetch.append(attributeDescription)
|
||||
}
|
||||
else {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
.warning,
|
||||
message: "The key path \"\(keyPath)\" could not be resolved in entity \(cs_typeName(entityDescription.managedObjectClassName)) as an attribute and will be ignored by \(cs_typeName(owner)) query clause."
|
||||
)
|
||||
}
|
||||
|
||||
case ._Aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
case ._aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
let entityDescription = fetchRequest.entity!
|
||||
if let attributeDescription = attributeDescriptionForKeyPath(keyPath, inEntity: entityDescription) {
|
||||
if let attributeDescription = attributeDescription(for: keyPath, in: entityDescription) {
|
||||
|
||||
let expressionDescription = NSExpressionDescription()
|
||||
expressionDescription.name = alias
|
||||
if nativeType == .UndefinedAttributeType {
|
||||
if nativeType == .undefinedAttributeType {
|
||||
|
||||
expressionDescription.expressionResultType = attributeDescription.attributeType
|
||||
}
|
||||
@@ -780,17 +804,17 @@ internal extension CollectionType where Generator.Element == SelectTerm {
|
||||
else {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
.warning,
|
||||
message: "The key path \"\(keyPath)\" could not be resolved in entity \(cs_typeName(entityDescription.managedObjectClassName)) as an attribute and will be ignored by \(cs_typeName(owner)) query clause."
|
||||
)
|
||||
}
|
||||
|
||||
case ._Identity(let alias, let nativeType):
|
||||
case ._identity(let alias, let nativeType):
|
||||
let expressionDescription = NSExpressionDescription()
|
||||
expressionDescription.name = alias
|
||||
if nativeType == .UndefinedAttributeType {
|
||||
if nativeType == .undefinedAttributeType {
|
||||
|
||||
expressionDescription.expressionResultType = .ObjectIDAttributeType
|
||||
expressionDescription.expressionResultType = .objectIDAttributeType
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -809,13 +833,13 @@ internal extension CollectionType where Generator.Element == SelectTerm {
|
||||
|
||||
switch self.first! {
|
||||
|
||||
case ._Attribute(let keyPath):
|
||||
case ._attribute(let keyPath):
|
||||
return keyPath
|
||||
|
||||
case ._Aggregate(_, _, let alias, _):
|
||||
case ._aggregate(_, _, let alias, _):
|
||||
return alias
|
||||
|
||||
case ._Identity(let alias, _):
|
||||
case ._identity(let alias, _):
|
||||
return alias
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ import CoreData
|
||||
Sample usage:
|
||||
```
|
||||
let employees = transaction.fetchAll(
|
||||
From(MyPersonEntity),
|
||||
From<MyPersonEntity>(),
|
||||
Tweak { (fetchRequest) -> Void in
|
||||
fetchRequest.includesPendingChanges = false
|
||||
fetchRequest.fetchLimit = 5
|
||||
@@ -47,7 +47,7 @@ public struct Tweak: FetchClause, QueryClause, DeleteClause {
|
||||
/**
|
||||
The block to customize the `NSFetchRequest`
|
||||
*/
|
||||
public let closure: (fetchRequest: NSFetchRequest) -> Void
|
||||
public let closure: (_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Void
|
||||
|
||||
/**
|
||||
Initializes a `Tweak` clause with a closure where the `NSFetchRequest` may be configured.
|
||||
@@ -55,7 +55,7 @@ public struct Tweak: FetchClause, QueryClause, DeleteClause {
|
||||
- Important: `Tweak`'s closure is executed only just before the fetch occurs, so make sure that any values captured by the closure is not prone to race conditions. Also, some utilities (such as `ListMonitor`s) may keep `FetchClause`s in memory and may thus introduce retain cycles if reference captures are not handled properly.
|
||||
- parameter closure: the block to customize the `NSFetchRequest`
|
||||
*/
|
||||
public init(_ closure: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
public init(_ closure: @escaping (_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Void) {
|
||||
|
||||
self.closure = closure
|
||||
}
|
||||
@@ -63,8 +63,8 @@ public struct Tweak: FetchClause, QueryClause, DeleteClause {
|
||||
|
||||
// MARK: FetchClause, QueryClause, DeleteClause
|
||||
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>) {
|
||||
|
||||
self.closure(fetchRequest: fetchRequest)
|
||||
self.closure(fetchRequest as! NSFetchRequest<NSFetchRequestResult>)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,19 +27,19 @@ import Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
public func &&(left: Where, right: Where) -> Where {
|
||||
public func && (left: Where, right: Where) -> Where {
|
||||
|
||||
return Where(NSCompoundPredicate(type: .AndPredicateType, subpredicates: [left.predicate, right.predicate]))
|
||||
return Where(NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate]))
|
||||
}
|
||||
|
||||
public func ||(left: Where, right: Where) -> Where {
|
||||
public func || (left: Where, right: Where) -> Where {
|
||||
|
||||
return Where(NSCompoundPredicate(type: .OrPredicateType, subpredicates: [left.predicate, right.predicate]))
|
||||
return Where(NSCompoundPredicate(type: .or, subpredicates: [left.predicate, right.predicate]))
|
||||
}
|
||||
|
||||
public prefix func !(clause: Where) -> Where {
|
||||
public prefix func ! (clause: Where) -> Where {
|
||||
|
||||
return Where(NSCompoundPredicate(type: .NotPredicateType, subpredicates: [clause.predicate]))
|
||||
return Where(NSCompoundPredicate(type: .not, subpredicates: [clause.predicate]))
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
- parameter format: the format string for the predicate
|
||||
- parameter args: the arguments for `format`
|
||||
*/
|
||||
public init(_ format: String, _ args: NSObject...) {
|
||||
public init(_ format: String, _ args: Any...) {
|
||||
|
||||
self.init(NSPredicate(format: format, argumentArray: args))
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
- parameter format: the format string for the predicate
|
||||
- parameter argumentArray: the arguments for `format`
|
||||
*/
|
||||
public init(_ format: String, argumentArray: [NSObject]?) {
|
||||
public init(_ format: String, argumentArray: [Any]?) {
|
||||
|
||||
self.init(NSPredicate(format: format, argumentArray: argumentArray))
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
- parameter keyPath: the keyPath to compare with
|
||||
- parameter value: the arguments for the `==` operator
|
||||
*/
|
||||
public init(_ keyPath: KeyPath, isEqualTo value: NSObject?) {
|
||||
public init(_ keyPath: KeyPath, isEqualTo value: Any?) {
|
||||
|
||||
self.init(value == nil
|
||||
? NSPredicate(format: "\(keyPath) == nil")
|
||||
@@ -114,7 +114,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
- parameter keyPath: the keyPath to compare with
|
||||
- parameter list: the array to check membership of
|
||||
*/
|
||||
public init(_ keyPath: KeyPath, isMemberOf list: [NSObject]) {
|
||||
public init(_ keyPath: KeyPath, isMemberOf list: [Any]) {
|
||||
|
||||
self.init(NSPredicate(format: "\(keyPath) IN %@", list))
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
- parameter keyPath: the keyPath to compare with
|
||||
- parameter list: the sequence to check membership of
|
||||
*/
|
||||
public init<S: SequenceType where S.Generator.Element: NSObject>(_ keyPath: KeyPath, isMemberOf list: S) {
|
||||
public init<S: Sequence>(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element: Any {
|
||||
|
||||
self.init(NSPredicate(format: "\(keyPath) IN %@", Array(list) as NSArray))
|
||||
}
|
||||
@@ -143,13 +143,13 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
|
||||
// MARK: FetchClause, QueryClause, DeleteClause
|
||||
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest<ResultType: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<ResultType>) {
|
||||
|
||||
if let predicate = fetchRequest.predicate where predicate != self.predicate {
|
||||
if let predicate = fetchRequest.predicate, predicate != self.predicate {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
message: "An existing predicate for the \(cs_typeName(NSFetchRequest)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
.warning,
|
||||
message: "An existing predicate for the \(cs_typeName(fetchRequest)) was overwritten by \(cs_typeName(self)) query clause."
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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,12 +172,3 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
return self.predicate.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Where: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: Where, rhs: Where) -> Bool {
|
||||
|
||||
return lhs.predicate == rhs.predicate
|
||||
}
|
||||
|
||||
@@ -37,8 +37,7 @@ public extension CoreStore {
|
||||
- parameter object: a reference to the object created/fetched outside the `DataStack`
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchExisting<T: NSManagedObject>(object: T) -> T? {
|
||||
public static func fetchExisting<T: NSManagedObject>(_ object: T) -> T? {
|
||||
|
||||
return self.defaultStack.fetchExisting(object)
|
||||
}
|
||||
@@ -49,8 +48,7 @@ public extension CoreStore {
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchExisting<T: NSManagedObject>(objectID: NSManagedObjectID) -> T? {
|
||||
public static func fetchExisting<T: NSManagedObject>(_ objectID: NSManagedObjectID) -> T? {
|
||||
|
||||
return self.defaultStack.fetchExisting(objectID)
|
||||
}
|
||||
@@ -61,8 +59,7 @@ public extension CoreStore {
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the `DataStack`
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||
public static func fetchExisting<T: NSManagedObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
|
||||
|
||||
return self.defaultStack.fetchExisting(objects)
|
||||
}
|
||||
@@ -73,8 +70,7 @@ public extension CoreStore {
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||
public static func fetchExisting<T: NSManagedObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
|
||||
|
||||
return self.defaultStack.fetchExisting(objectIDs)
|
||||
}
|
||||
@@ -86,8 +82,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
public static func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
return self.defaultStack.fetchOne(from, fetchClauses)
|
||||
}
|
||||
@@ -99,8 +94,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
public static func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
return self.defaultStack.fetchOne(from, fetchClauses)
|
||||
}
|
||||
@@ -112,8 +106,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
public static func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
return self.defaultStack.fetchAll(from, fetchClauses)
|
||||
}
|
||||
@@ -125,8 +118,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
public static func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
|
||||
return self.defaultStack.fetchAll(from, fetchClauses)
|
||||
}
|
||||
@@ -138,8 +130,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
public static func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
return self.defaultStack.fetchCount(from, fetchClauses)
|
||||
}
|
||||
@@ -151,8 +142,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
public static func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
|
||||
return self.defaultStack.fetchCount(from, fetchClauses)
|
||||
}
|
||||
@@ -164,8 +154,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
public static func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
return self.defaultStack.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
@@ -177,8 +166,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
public static func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
return self.defaultStack.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
@@ -190,8 +178,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
public static func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
@@ -203,8 +190,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
public static func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
@@ -219,8 +205,7 @@ public extension CoreStore {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
public static func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
|
||||
return self.defaultStack.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
@@ -235,8 +220,7 @@ public extension CoreStore {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
public static func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
|
||||
return self.defaultStack.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
@@ -251,8 +235,7 @@ public extension CoreStore {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[NSString: AnyObject]]? {
|
||||
public static func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
|
||||
|
||||
return self.defaultStack.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
@@ -267,8 +250,7 @@ public extension CoreStore {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public static func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
|
||||
|
||||
return self.defaultStack.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -25,14 +25,13 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - DataStack
|
||||
|
||||
public extension DataStack {
|
||||
extension DataStack: FetchableSource, QueryableSource {
|
||||
|
||||
// MARK: FetchableSource
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the `DataStack`'s context from a reference created from a transaction or from a different managed object context.
|
||||
@@ -40,17 +39,9 @@ public extension DataStack {
|
||||
- parameter object: a reference to the object created/fetched outside the `DataStack`
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject>(object: T) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject>(_ object: T) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
return (try self.mainContext.existingObjectWithID(object.objectID) as! T)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.mainContext.fetchExisting(object)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,17 +50,9 @@ public extension DataStack {
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject>(objectID: NSManagedObjectID) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject>(_ objectID: NSManagedObjectID) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
return (try self.mainContext.existingObjectWithID(objectID) as! T)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.mainContext.fetchExisting(objectID)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,10 +61,9 @@ public extension DataStack {
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the `DataStack`
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
|
||||
|
||||
return objects.flatMap { (try? self.mainContext.existingObjectWithID($0.objectID)) as? T }
|
||||
return self.mainContext.fetchExisting(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,10 +72,9 @@ public extension DataStack {
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
|
||||
|
||||
return objectIDs.flatMap { (try? self.mainContext.existingObjectWithID($0)) as? T }
|
||||
return self.mainContext.fetchExisting(objectIDs)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,11 +84,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchOne(from, fetchClauses)
|
||||
@@ -120,11 +100,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchOne(from, fetchClauses)
|
||||
@@ -137,11 +116,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchAll(from, fetchClauses)
|
||||
@@ -154,11 +132,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchAll(from, fetchClauses)
|
||||
@@ -171,11 +148,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchCount(from, fetchClauses)
|
||||
@@ -188,11 +164,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchCount(from, fetchClauses)
|
||||
@@ -205,11 +180,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchObjectID(from, fetchClauses)
|
||||
@@ -222,11 +196,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchObjectID(from, fetchClauses)
|
||||
@@ -239,11 +212,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
@@ -256,16 +228,18 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
|
||||
// MARK: QueryableSource
|
||||
|
||||
/**
|
||||
Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
|
||||
@@ -276,11 +250,10 @@ public extension DataStack {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
@@ -296,11 +269,10 @@ public extension DataStack {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
@@ -316,11 +288,10 @@ public extension DataStack {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
@@ -336,13 +307,23 @@ public extension DataStack {
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
|
||||
// MARK: FetchableSource, QueryableSource
|
||||
|
||||
/**
|
||||
The internal `NSManagedObjectContext` managed by this instance. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.
|
||||
*/
|
||||
public func internalContext() -> NSManagedObjectContext {
|
||||
|
||||
return self.mainContext
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// FetchableSource.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: - FetchableSource
|
||||
|
||||
/**
|
||||
Encapsulates containers which manages an internal `NSManagedObjectContext`, such as `DataStack`s and transactions, that can be used for fetching objects. CoreStore provides implementations for this protocol and should be used as a read-only abstraction.
|
||||
*/
|
||||
public protocol FetchableSource: class {
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the `FetchableSource`'s context from a reference created from another managed object context.
|
||||
|
||||
- parameter object: a reference to the object created/fetched outside the `FetchableSource`'s context
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `FetchableSource`'s context, or `nil` if not found.
|
||||
*/
|
||||
func fetchExisting<T: NSManagedObject>(_ object: T) -> T?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the `FetchableSource`'s context from an `NSManagedObjectID`.
|
||||
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the `FetchableSource`, or `nil` if not found.
|
||||
*/
|
||||
func fetchExisting<T: NSManagedObject>(_ objectID: NSManagedObjectID) -> T?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the `FetchableSource`'s context from references created from another managed object context.
|
||||
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the `FetchableSource`'s context
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `FetchableSource`
|
||||
*/
|
||||
func fetchExisting<T: NSManagedObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the `FetchableSource`'s context from a list of `NSManagedObjectID`.
|
||||
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the `FetchableSource`'s context
|
||||
*/
|
||||
func fetchExisting<T: NSManagedObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID
|
||||
|
||||
/**
|
||||
Fetches the first `NSManagedObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T?
|
||||
|
||||
/**
|
||||
Fetches the first `NSManagedObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T?
|
||||
|
||||
/**
|
||||
Fetches all `NSManagedObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]?
|
||||
|
||||
/**
|
||||
Fetches all `NSManagedObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]?
|
||||
|
||||
/**
|
||||
Fetches the number of `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int?
|
||||
|
||||
/**
|
||||
Fetches the number of `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `FetchClause`s
|
||||
*/
|
||||
func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]?
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `FetchClause`s
|
||||
*/
|
||||
func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]?
|
||||
|
||||
/**
|
||||
The internal `NSManagedObjectContext` managed by this `FetchableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.
|
||||
*/
|
||||
func internalContext() -> NSManagedObjectContext
|
||||
}
|
||||
@@ -34,7 +34,7 @@ import CoreData
|
||||
*/
|
||||
public protocol FetchClause {
|
||||
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest<T: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<T>)
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public protocol FetchClause {
|
||||
*/
|
||||
public protocol QueryClause {
|
||||
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest<T: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<T>)
|
||||
}
|
||||
|
||||
|
||||
@@ -56,5 +56,5 @@ public protocol QueryClause {
|
||||
*/
|
||||
public protocol DeleteClause {
|
||||
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest<T: NSFetchRequestResult>(_ fetchRequest: NSFetchRequest<T>)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// QueryableSource.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: - QueryableSource
|
||||
|
||||
/**
|
||||
Encapsulates containers which manages an internal `NSManagedObjectContext`, such as `DataStack`s and transactions, that can be used for querying values. CoreStore provides implementations for this protocol and should be used as a read-only abstraction.
|
||||
*/
|
||||
public protocol QueryableSource: class {
|
||||
|
||||
/**
|
||||
Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter selectClause: a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U?
|
||||
|
||||
/**
|
||||
Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter selectClause: a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U?
|
||||
|
||||
/**
|
||||
Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter selectClause: a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]?
|
||||
|
||||
/**
|
||||
Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
|
||||
A "query" differs from a "fetch" in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter selectClause: a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
|
||||
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
|
||||
*/
|
||||
func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]?
|
||||
|
||||
/**
|
||||
The internal `NSManagedObjectContext` managed by this `QueryableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.
|
||||
*/
|
||||
func internalContext() -> NSManagedObjectContext
|
||||
}
|
||||
@@ -36,27 +36,29 @@ public extension BaseDataTransaction {
|
||||
|
||||
- parameter into: an `Into` clause specifying the entity type
|
||||
- parameter source: the object to import values from
|
||||
- throws: an `ErrorType` thrown from any of the `ImportableObject` methods
|
||||
- throws: an `Error` thrown from any of the `ImportableObject` methods
|
||||
- returns: the created `ImportableObject` instance, or `nil` if the import was ignored
|
||||
*/
|
||||
public func importObject<T where T: NSManagedObject, T: ImportableObject>(
|
||||
into: Into<T>,
|
||||
source: T.ImportSource) throws -> T? {
|
||||
public func importObject<T>(
|
||||
_ into: Into<T>,
|
||||
source: T.ImportSource) throws -> T? where T: NSManagedObject, T: ImportableObject {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to import an object of type \(cs_typeName(into.entityClass)) outside the transaction's designated queue."
|
||||
)
|
||||
|
||||
return try cs_autoreleasepool {
|
||||
|
||||
return try autoreleasepool {
|
||||
|
||||
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||
let entityType = into.entityClass as! T.Type
|
||||
|
||||
guard entityType.shouldInsert(from: source, in: self) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
let object = self.create(into)
|
||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||
try object.didInsert(from: source, in: self)
|
||||
return object
|
||||
}
|
||||
}
|
||||
@@ -66,25 +68,25 @@ public extension BaseDataTransaction {
|
||||
|
||||
- parameter object: the `NSManagedObject` to update
|
||||
- parameter source: the object to import values from
|
||||
- throws: an `ErrorType` thrown from any of the `ImportableObject` methods
|
||||
- throws: an `Error` thrown from any of the `ImportableObject` methods
|
||||
*/
|
||||
public func importObject<T where T: NSManagedObject, T: ImportableObject>(
|
||||
object: T,
|
||||
source: T.ImportSource) throws {
|
||||
public func importObject<T>(
|
||||
_ object: T,
|
||||
source: T.ImportSource) throws where T: NSManagedObject, T: ImportableObject {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to import an object of type \(cs_typeName(object)) outside the transaction's designated queue."
|
||||
)
|
||||
|
||||
try cs_autoreleasepool {
|
||||
|
||||
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||
try autoreleasepool {
|
||||
|
||||
let entityType = type(of: object)
|
||||
guard entityType.shouldInsert(from: source, in: self) else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||
try object.didInsert(from: source, in: self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,31 +95,31 @@ public extension BaseDataTransaction {
|
||||
|
||||
- parameter into: an `Into` clause specifying the entity type
|
||||
- parameter sourceArray: the array of objects to import values from
|
||||
- throws: an `ErrorType` thrown from any of the `ImportableObject` methods
|
||||
- throws: an `Error` thrown from any of the `ImportableObject` methods
|
||||
- returns: the array of created `ImportableObject` instances
|
||||
*/
|
||||
public func importObjects<T, S: SequenceType where T: NSManagedObject, T: ImportableObject, S.Generator.Element == T.ImportSource>(
|
||||
into: Into<T>,
|
||||
sourceArray: S) throws -> [T] {
|
||||
public func importObjects<T, S: Sequence>(
|
||||
_ into: Into<T>,
|
||||
sourceArray: S) throws -> [T] where T: NSManagedObject, T: ImportableObject, S.Iterator.Element == T.ImportSource {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to import an object of type \(cs_typeName(into.entityClass)) outside the transaction's designated queue."
|
||||
)
|
||||
|
||||
return try cs_autoreleasepool {
|
||||
return try autoreleasepool {
|
||||
|
||||
return try sourceArray.flatMap { (source) -> T? in
|
||||
|
||||
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||
|
||||
let entityType = into.entityClass as! T.Type
|
||||
guard entityType.shouldInsert(from: source, in: self) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return try cs_autoreleasepool {
|
||||
return try autoreleasepool {
|
||||
|
||||
let object = self.create(into)
|
||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||
try object.didInsert(from: source, in: self)
|
||||
return object
|
||||
}
|
||||
}
|
||||
@@ -129,46 +131,45 @@ public extension BaseDataTransaction {
|
||||
|
||||
- parameter into: an `Into` clause specifying the entity type
|
||||
- parameter source: the object to import values from
|
||||
- throws: an `ErrorType` thrown from any of the `ImportableUniqueObject` methods
|
||||
- throws: an `Error` thrown from any of the `ImportableUniqueObject` methods
|
||||
- returns: the created/updated `ImportableUniqueObject` instance, or `nil` if the import was ignored
|
||||
*/
|
||||
public func importUniqueObject<T where T: NSManagedObject, T: ImportableUniqueObject>(
|
||||
into: Into<T>,
|
||||
source: T.ImportSource) throws -> T? {
|
||||
public func importUniqueObject<T>(
|
||||
_ into: Into<T>,
|
||||
source: T.ImportSource) throws -> T? where T: NSManagedObject, T: ImportableUniqueObject {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to import an object of type \(cs_typeName(into.entityClass)) outside the transaction's designated queue."
|
||||
)
|
||||
|
||||
return try cs_autoreleasepool {
|
||||
|
||||
let uniqueIDKeyPath = T.uniqueIDKeyPath
|
||||
guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else {
|
||||
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 {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if let object = self.fetchOne(From(T), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
|
||||
if let object = self.fetchOne(From(entityType), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
|
||||
|
||||
guard T.shouldUpdateFromImportSource(source, inTransaction: self) else {
|
||||
guard entityType.shouldUpdate(from: source, in: self) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
try object.updateFromImportSource(source, inTransaction: self)
|
||||
try object.update(from: source, in: self)
|
||||
return object
|
||||
}
|
||||
else {
|
||||
|
||||
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||
guard entityType.shouldInsert(from: source, in: self) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
let object = self.create(into)
|
||||
object.uniqueIDValue = uniqueIDValue
|
||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||
try object.didInsert(from: source, in: self)
|
||||
return object
|
||||
}
|
||||
}
|
||||
@@ -176,79 +177,79 @@ public extension BaseDataTransaction {
|
||||
|
||||
/**
|
||||
Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources.
|
||||
- Warning: While the array returned from `importUniqueObjects(...)` correctly maps to the order of `sourceArray`, the order of objects called with `ImportableUniqueObject` methods is arbitrary. Do not make assumptions that any particular object will be imported ahead or after another object.
|
||||
`ImportableUniqueObject` methods are called on the objects in the same order as they are in the `sourceArray`, and are returned in an array with that same order.
|
||||
- Warning: If `sourceArray` contains multiple import sources with same ID, no merging will occur and ONLY THE LAST duplicate will be imported.
|
||||
|
||||
- parameter into: an `Into` clause specifying the entity type
|
||||
- parameter sourceArray: the array of objects to import values from
|
||||
- parameter preProcess: a closure that lets the caller tweak the internal `UniqueIDType`-to-`ImportSource` mapping to be used for importing. Callers can remove from/add to/update `mapping` and return the updated array from the closure.
|
||||
- throws: an `ErrorType` thrown from any of the `ImportableUniqueObject` methods
|
||||
- throws: an `Error` thrown from any of the `ImportableUniqueObject` methods
|
||||
- returns: the array of created/updated `ImportableUniqueObject` instances
|
||||
*/
|
||||
public func importUniqueObjects<T, S: SequenceType where T: NSManagedObject, T: ImportableUniqueObject, S.Generator.Element == T.ImportSource>(
|
||||
into: Into<T>,
|
||||
public func importUniqueObjects<T, S: Sequence>(
|
||||
_ into: Into<T>,
|
||||
sourceArray: S,
|
||||
@noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws -> [T] {
|
||||
preProcess: @escaping (_ mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws -> [T] where T: NSManagedObject, T: ImportableUniqueObject, S.Iterator.Element == T.ImportSource {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to import an object of type \(cs_typeName(into.entityClass)) outside the transaction's designated queue."
|
||||
)
|
||||
|
||||
return try cs_autoreleasepool {
|
||||
|
||||
var mapping = Dictionary<T.UniqueIDType, T.ImportSource>()
|
||||
let sortedIDs = try cs_autoreleasepool {
|
||||
|
||||
return try autoreleasepool {
|
||||
|
||||
let entityType = into.entityClass as! T.Type
|
||||
var importSourceByID = Dictionary<T.UniqueIDType, T.ImportSource>()
|
||||
let sortedIDs = try autoreleasepool {
|
||||
|
||||
return try sourceArray.flatMap { (source) -> T.UniqueIDType? in
|
||||
|
||||
guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else {
|
||||
guard let uniqueIDValue = try entityType.uniqueID(from: source, in: self) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
mapping[uniqueIDValue] = source
|
||||
importSourceByID[uniqueIDValue] = source // effectively replaces duplicate with the latest
|
||||
return uniqueIDValue
|
||||
}
|
||||
}
|
||||
|
||||
mapping = try cs_autoreleasepool { try preProcess(mapping: mapping) }
|
||||
|
||||
var objects = Dictionary<T.UniqueIDType, T>()
|
||||
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: sortedIDs)) ?? [] {
|
||||
importSourceByID = try autoreleasepool { try preProcess(importSourceByID) }
|
||||
|
||||
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
|
||||
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
|
||||
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
|
||||
|
||||
var processedObjectIDs = Set<T.UniqueIDType>()
|
||||
var result = [T]()
|
||||
|
||||
for objectID in sortedIDs where !processedObjectIDs.contains(objectID) {
|
||||
|
||||
try cs_autoreleasepool {
|
||||
guard let source = importSourceByID[objectID] else {
|
||||
|
||||
let uniqueIDValue = object.uniqueIDValue
|
||||
|
||||
guard let source = mapping.removeValueForKey(uniqueIDValue)
|
||||
where T.shouldUpdateFromImportSource(source, inTransaction: self) else {
|
||||
continue
|
||||
}
|
||||
try autoreleasepool {
|
||||
|
||||
if let object = existingObjectsByID[objectID] {
|
||||
|
||||
guard entityType.shouldUpdate(from: source, in: self) else {
|
||||
|
||||
return
|
||||
}
|
||||
try object.update(from: source, in: self)
|
||||
result.append(object)
|
||||
}
|
||||
|
||||
try object.updateFromImportSource(source, inTransaction: self)
|
||||
objects[uniqueIDValue] = object
|
||||
}
|
||||
}
|
||||
|
||||
for (uniqueIDValue, source) in mapping {
|
||||
|
||||
try cs_autoreleasepool {
|
||||
|
||||
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||
else if entityType.shouldInsert(from: source, in: self) {
|
||||
|
||||
return
|
||||
let object = self.create(into)
|
||||
object.uniqueIDValue = objectID
|
||||
try object.didInsert(from: source, in: self)
|
||||
result.append(object)
|
||||
}
|
||||
|
||||
let object = self.create(into)
|
||||
object.uniqueIDValue = uniqueIDValue
|
||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||
|
||||
objects[uniqueIDValue] = object
|
||||
processedObjectIDs.insert(objectID)
|
||||
}
|
||||
}
|
||||
|
||||
return sortedIDs.flatMap { objects[$0] }
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ import CoreData
|
||||
CoreStore.beginAsynchronous { (transaction) -> Void in
|
||||
let json: NSDictionary = // ...
|
||||
let person = try! transaction.importObject(
|
||||
Into(MyPersonEntity),
|
||||
Into<MyPersonEntity>(),
|
||||
source: json
|
||||
)
|
||||
// ...
|
||||
@@ -62,15 +62,24 @@ public protocol ImportableObject: class {
|
||||
- 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.
|
||||
*/
|
||||
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> 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.
|
||||
*/
|
||||
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
*/
|
||||
func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "shouldInsert(from:in:)")
|
||||
static func shouldInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "didInsert(from:in:)")
|
||||
func didInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +87,21 @@ public protocol ImportableObject: class {
|
||||
|
||||
public extension ImportableObject {
|
||||
|
||||
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
static func shouldInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
|
||||
return Self.shouldInsert(from: source, in: transaction)
|
||||
}
|
||||
|
||||
func didInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
||||
|
||||
try self.didInsert(from: source, in: transaction)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ import CoreData
|
||||
CoreStore.beginAsynchronous { (transaction) -> Void in
|
||||
let json: NSDictionary = // ...
|
||||
let person = try! transaction.importUniqueObject(
|
||||
Into(MyPersonEntity),
|
||||
Into<MyPersonEntity>(),
|
||||
source: json
|
||||
)
|
||||
// ...
|
||||
@@ -72,13 +72,13 @@ public protocol ImportableUniqueObject: ImportableObject {
|
||||
var uniqueIDValue: UniqueIDType { get set }
|
||||
|
||||
/**
|
||||
Return `true` if an object should be created from `source`. Return `false` to ignore and skip `source`. The default implementation returns the value returned by the `shouldUpdateFromImportSource(:inTransaction:)` implementation.
|
||||
Return `true` if an object should be created from `source`. Return `false` to ignore and skip `source`. The default implementation returns the value returned by the `shouldUpdate(from:in:)` implementation.
|
||||
|
||||
- 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.
|
||||
*/
|
||||
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool
|
||||
|
||||
/**
|
||||
Return `true` if an object should be updated from `source`. Return `false` to ignore and skip `source`. The default implementation returns `true`.
|
||||
@@ -87,24 +87,24 @@ public protocol ImportableUniqueObject: ImportableObject {
|
||||
- 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 updated from `source`. Return `false` to ignore.
|
||||
*/
|
||||
static func shouldUpdateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
static func shouldUpdate(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool
|
||||
|
||||
/**
|
||||
Return the unique ID as extracted from `source`. This method is called before `shouldInsertFromImportSource(...)` or `shouldUpdateFromImportSource(...)`. Return `nil` to skip importing from `source`. Note that throwing from this method will cause subsequent imports that are part of the same `importUniqueObjects(:sourceArray:)` call to be cancelled.
|
||||
Return the unique ID as extracted from `source`. This method is called before `shouldInsert(from:in:)` or `shouldUpdate(from:in:)`. Return `nil` to skip importing from `source`. Note that throwing from this method will cause subsequent imports that are part of the same `importUniqueObjects(: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.
|
||||
- returns: the unique ID as extracted from `source`, or `nil` to skip importing from `source`.
|
||||
*/
|
||||
static func uniqueIDFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws -> UniqueIDType?
|
||||
static func uniqueID(from source: ImportSource, in transaction: BaseDataTransaction) throws -> UniqueIDType?
|
||||
|
||||
/**
|
||||
Implements the actual importing of data from `source`. This method is called just after the object is created and assigned its unique ID as returned from `uniqueIDFromImportSource(...)`. 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 `importUniqueObjects(:sourceArray:)` call to be cancelled. The default implementation simply calls `updateFromImportSource(...)`.
|
||||
Implements the actual importing of data from `source`. This method is called just after the object is created and assigned its unique ID as returned from `uniqueID(from:in:)`. 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 `importUniqueObjects(:sourceArray:)` call to be cancelled. The default implementation simply calls `update(from:in:)`.
|
||||
|
||||
- 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.
|
||||
*/
|
||||
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws
|
||||
|
||||
/**
|
||||
Implements the actual importing of data from `source`. This method is called just after the existing object is fetched using its unique ID. 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 `importUniqueObjects(:sourceArray:)` call to be cancelled.
|
||||
@@ -112,7 +112,25 @@ public protocol ImportableUniqueObject: ImportableObject {
|
||||
- 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.
|
||||
*/
|
||||
func updateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
func update(from source: ImportSource, in transaction: BaseDataTransaction) throws
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "shouldInsert(from:in:)")
|
||||
static func shouldInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "shouldUpdate(from:in:)")
|
||||
static func shouldUpdateFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "uniqueID(from:in:)")
|
||||
static func uniqueIDFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws -> UniqueIDType?
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "didInsert(from:in:)")
|
||||
func didInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
|
||||
@available(*, deprecated: 3.0.0, renamed: "update(from:in:)")
|
||||
func updateFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||
}
|
||||
|
||||
|
||||
@@ -120,18 +138,46 @@ public protocol ImportableUniqueObject: ImportableObject {
|
||||
|
||||
public extension ImportableUniqueObject {
|
||||
|
||||
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool {
|
||||
|
||||
return self.shouldUpdateFromImportSource(source, inTransaction: transaction)
|
||||
return Self.shouldUpdate(from: source, in: transaction)
|
||||
}
|
||||
|
||||
static func shouldUpdateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
static func shouldUpdate(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool{
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
||||
func didInsert(from source: Self.ImportSource, in transaction: BaseDataTransaction) throws {
|
||||
|
||||
try self.updateFromImportSource(source, inTransaction: transaction)
|
||||
try self.update(from: source, in: transaction)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
static func shouldInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
|
||||
return Self.shouldInsert(from: source, in: transaction)
|
||||
}
|
||||
|
||||
static func shouldUpdateFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||
|
||||
return Self.shouldUpdate(from: source, in: transaction)
|
||||
}
|
||||
|
||||
static func uniqueIDFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws -> UniqueIDType? {
|
||||
|
||||
return try Self.uniqueID(from: source, in: transaction)
|
||||
}
|
||||
|
||||
func didInsertFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
||||
|
||||
try self.didInsert(from: source, in: transaction)
|
||||
}
|
||||
|
||||
func updateFromImportSource(_ source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
||||
|
||||
try self.update(from: source, in: transaction)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.1.3</string>
|
||||
<string>3.0.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// CoreStoreFetchRequest+CoreStore.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: - CoreStoreFetchRequest
|
||||
|
||||
internal extension CoreStoreFetchRequest {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal func dynamicCast<U: NSFetchRequestResult>() -> NSFetchRequest<U> {
|
||||
|
||||
return unsafeBitCast(self, to: NSFetchRequest<U>.self)
|
||||
}
|
||||
}
|
||||
@@ -31,12 +31,12 @@ import CoreData
|
||||
|
||||
// MARK: - CoreStoreFetchedResultsController
|
||||
|
||||
internal final class CoreStoreFetchedResultsController: NSFetchedResultsController {
|
||||
internal final class CoreStoreFetchedResultsController: NSFetchedResultsController<NSManagedObject> {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal convenience init<T: NSManagedObject>(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
internal convenience init<T: NSManagedObject>(dataStack: DataStack, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: dataStack.mainContext,
|
||||
@@ -48,14 +48,14 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal init<T: NSManagedObject>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
internal init<T: NSManagedObject>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
|
||||
|
||||
_ = from?.applyToFetchRequest(
|
||||
fetchRequest,
|
||||
context: context,
|
||||
applyAffectedStores: false
|
||||
)
|
||||
applyFetchClauses(fetchRequest: fetchRequest)
|
||||
applyFetchClauses(fetchRequest)
|
||||
|
||||
if let from = from {
|
||||
|
||||
@@ -68,7 +68,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
|
||||
guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap(From.init) else {
|
||||
|
||||
CoreStore.abort("Attempted to create an \(cs_typeName(NSFetchedResultsController)) without a \(cs_typeName(From)) clause or an \(cs_typeName(NSEntityDescription)).")
|
||||
CoreStore.abort("Attempted to create a \(CoreStoreFetchedResultsController.self) without a \(cs_typeName(From<T>.self)) clause or an \(cs_typeName(NSEntityDescription.self)).")
|
||||
}
|
||||
|
||||
self.reapplyAffectedStores = { fetchRequest, context in
|
||||
@@ -88,16 +88,22 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
@nonobjc
|
||||
internal func performFetchFromSpecifiedStores() throws {
|
||||
|
||||
if !self.reapplyAffectedStores(fetchRequest: self.fetchRequest, context: self.managedObjectContext) {
|
||||
if !self.reapplyAffectedStores(self.fetchRequest, self.managedObjectContext) {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
message: "Attempted to perform a fetch on an \(cs_typeName(NSFetchedResultsController)) but could not find any persistent store for the entity \(cs_typeName(self.fetchRequest.entityName))"
|
||||
.warning,
|
||||
message: "Attempted to perform a fetch on an \(cs_typeName(self)) but could not find any persistent store for the entity \(cs_typeName(self.fetchRequest.entityName))"
|
||||
)
|
||||
}
|
||||
try self.performFetch()
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func dynamicCast<U: NSFetchRequestResult>() -> NSFetchedResultsController<U> {
|
||||
|
||||
return unsafeBitCast(self, to: NSFetchedResultsController<U>.self)
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
||||
self.delegate = nil
|
||||
@@ -107,7 +113,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
// MARK: Private
|
||||
|
||||
@nonobjc
|
||||
private let reapplyAffectedStores: (fetchRequest: NSFetchRequest, context: NSManagedObjectContext) -> Bool
|
||||
private let reapplyAffectedStores: (_ fetchRequest: NSFetchRequest<NSManagedObject>, _ context: NSManagedObjectContext) -> Bool
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// DispatchQueue+CoreStore.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
|
||||
|
||||
|
||||
// MARK: - DispatchQueue
|
||||
|
||||
internal extension DispatchQueue {
|
||||
|
||||
@nonobjc
|
||||
internal static func serial(_ label: String, qos: DispatchQoS = .default) -> DispatchQueue {
|
||||
|
||||
return DispatchQueue(
|
||||
label: label,
|
||||
qos: qos,
|
||||
attributes: [],
|
||||
autoreleaseFrequency: .inherit,
|
||||
target: nil
|
||||
)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal static func concurrent(_ label: String, qos: DispatchQoS = .default) -> DispatchQueue {
|
||||
|
||||
return DispatchQueue(
|
||||
label: label,
|
||||
qos: qos,
|
||||
attributes: .concurrent,
|
||||
autoreleaseFrequency: .inherit,
|
||||
target: nil
|
||||
)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func cs_isCurrentExecutionContext() -> Bool {
|
||||
|
||||
let specific = ObjectIdentifier(self)
|
||||
|
||||
self.setSpecific(key: Static.specificKey, value: specific)
|
||||
return DispatchQueue.getSpecific(key: Static.specificKey) == specific
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func cs_sync<T>(_ closure: () throws -> T) rethrows -> T {
|
||||
|
||||
return try self.sync { try autoreleasepool(invoking: closure) }
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func cs_async(_ closure: @escaping () -> Void) {
|
||||
|
||||
self.async { autoreleasepool(invoking: closure) }
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func cs_barrierSync<T>(_ closure: () throws -> T) rethrows -> T {
|
||||
|
||||
return try self.sync(flags: .barrier) { try autoreleasepool(invoking: closure) }
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func cs_barrierAsync(_ closure: @escaping () -> Void) {
|
||||
|
||||
self.async(flags: .barrier) { autoreleasepool(invoking: closure) }
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private enum Static {
|
||||
|
||||
static let specificKey = DispatchSpecificKey<ObjectIdentifier>()
|
||||
}
|
||||
}
|
||||
@@ -33,21 +33,21 @@ import CoreData
|
||||
|
||||
internal protocol FetchedResultsControllerHandler: class {
|
||||
|
||||
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
|
||||
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeObject anObject: Any, atIndexPath indexPath: IndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: IndexPath?)
|
||||
|
||||
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
|
||||
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
|
||||
|
||||
func controllerWillChangeContent(controller: NSFetchedResultsController)
|
||||
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)
|
||||
|
||||
func controllerDidChangeContent(controller: NSFetchedResultsController)
|
||||
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)
|
||||
|
||||
func controller(controller: NSFetchedResultsController, sectionIndexTitleForSectionName sectionName: String?) -> String?
|
||||
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, sectionIndexTitleForSectionName sectionName: String?) -> String?
|
||||
}
|
||||
|
||||
|
||||
// MARK: - FetchedResultsControllerDelegate
|
||||
|
||||
internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResultsControllerDelegate {
|
||||
internal final class FetchedResultsControllerDelegate<EntityType: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@@ -58,7 +58,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
internal weak var handler: FetchedResultsControllerHandler?
|
||||
|
||||
@nonobjc
|
||||
internal weak var fetchedResultsController: NSFetchedResultsController? {
|
||||
internal weak var fetchedResultsController: CoreStoreFetchedResultsController? {
|
||||
|
||||
didSet {
|
||||
|
||||
@@ -76,7 +76,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
// MARK: NSFetchedResultsControllerDelegate
|
||||
|
||||
@objc
|
||||
dynamic func controllerWillChangeContent(controller: NSFetchedResultsController) {
|
||||
dynamic func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
||||
|
||||
guard self.enabled else {
|
||||
|
||||
@@ -90,7 +90,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
}
|
||||
|
||||
@objc
|
||||
dynamic func controllerDidChangeContent(controller: NSFetchedResultsController) {
|
||||
dynamic func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
||||
|
||||
guard self.enabled else {
|
||||
|
||||
@@ -101,7 +101,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
}
|
||||
|
||||
@objc
|
||||
dynamic func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
|
||||
dynamic func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
|
||||
|
||||
guard self.enabled else {
|
||||
|
||||
@@ -120,21 +120,32 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
// http://stackoverflow.com/questions/31383760/ios-9-attempt-to-delete-and-reload-the-same-index-path/31384014#31384014
|
||||
// https://forums.developer.apple.com/message/9998#9998
|
||||
// https://forums.developer.apple.com/message/31849#31849
|
||||
|
||||
if #available(iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
|
||||
|
||||
// I don't know if iOS 10 even attempted to fix this mess...
|
||||
if case .update = actualType,
|
||||
indexPath != nil,
|
||||
newIndexPath != nil {
|
||||
|
||||
actualType = .move
|
||||
}
|
||||
}
|
||||
|
||||
if #available(iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
|
||||
|
||||
// I don't know if iOS 10 even attempted to fix this mess...
|
||||
if case .Update = actualType
|
||||
where indexPath != nil && newIndexPath != nil {
|
||||
if case .update = actualType,
|
||||
indexPath != nil && newIndexPath != nil {
|
||||
|
||||
actualType = .Move
|
||||
actualType = .move
|
||||
}
|
||||
}
|
||||
|
||||
switch actualType {
|
||||
|
||||
case .Update:
|
||||
guard let section = indexPath?.indexAtPosition(0) else {
|
||||
case .update:
|
||||
guard let section = indexPath?[0] else {
|
||||
|
||||
return
|
||||
}
|
||||
@@ -144,7 +155,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
return
|
||||
}
|
||||
|
||||
case .Move:
|
||||
case .move:
|
||||
guard let indexPath = indexPath, let newIndexPath = newIndexPath else {
|
||||
|
||||
return
|
||||
@@ -153,25 +164,25 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
|
||||
break
|
||||
}
|
||||
if self.insertedSections.contains(indexPath.indexAtPosition(0)) {
|
||||
if self.insertedSections.contains(indexPath[0]) {
|
||||
|
||||
// Observers that handle the .Move change are advised to delete then reinsert the object instead of just moving. This is especially true when indexPath and newIndexPath are equal. For example, calling tableView.moveRowAtIndexPath(_:toIndexPath) when both indexPaths are the same will crash the tableView.
|
||||
self.handler?.controller(
|
||||
controller,
|
||||
didChangeObject: anObject,
|
||||
atIndexPath: indexPath,
|
||||
forChangeType: .Move,
|
||||
forChangeType: .move,
|
||||
newIndexPath: newIndexPath
|
||||
)
|
||||
return
|
||||
}
|
||||
if self.deletedSections.contains(indexPath.indexAtPosition(0)) {
|
||||
if self.deletedSections.contains(indexPath[0]) {
|
||||
|
||||
self.handler?.controller(
|
||||
controller,
|
||||
didChangeObject: anObject,
|
||||
atIndexPath: nil,
|
||||
forChangeType: .Insert,
|
||||
forChangeType: .insert,
|
||||
newIndexPath: indexPath
|
||||
)
|
||||
return
|
||||
@@ -180,7 +191,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
controller,
|
||||
didChangeObject: anObject,
|
||||
atIndexPath: indexPath,
|
||||
forChangeType: .Update,
|
||||
forChangeType: .update,
|
||||
newIndexPath: nil
|
||||
)
|
||||
return
|
||||
@@ -199,7 +210,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
}
|
||||
|
||||
@objc
|
||||
dynamic func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
|
||||
dynamic func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
|
||||
|
||||
guard self.enabled else {
|
||||
|
||||
@@ -208,8 +219,8 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
|
||||
switch type {
|
||||
|
||||
case .Delete: self.deletedSections.insert(sectionIndex)
|
||||
case .Insert: self.insertedSections.insert(sectionIndex)
|
||||
case .delete: self.deletedSections.insert(sectionIndex)
|
||||
case .insert: self.insertedSections.insert(sectionIndex)
|
||||
default: break
|
||||
}
|
||||
|
||||
@@ -222,7 +233,7 @@ internal final class FetchedResultsControllerDelegate: NSObject, NSFetchedResult
|
||||
}
|
||||
|
||||
@objc
|
||||
dynamic func controller(controller: NSFetchedResultsController, sectionIndexTitleForSectionName sectionName: String) -> String? {
|
||||
dynamic func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, sectionIndexTitleForSectionName sectionName: String) -> String? {
|
||||
|
||||
return self.handler?.controller(
|
||||
controller,
|
||||
|
||||
@@ -25,70 +25,9 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
// MARK: Associated Objects
|
||||
|
||||
// MARK: - Custom AutoreleasePool
|
||||
|
||||
internal func cs_autoreleasepool(@noescape closure: () -> Void) {
|
||||
|
||||
autoreleasepool(closure)
|
||||
}
|
||||
|
||||
internal func cs_autoreleasepool<T>(@noescape closure: () -> T) -> T {
|
||||
|
||||
var closureValue: T!
|
||||
autoreleasepool {
|
||||
|
||||
closureValue = closure()
|
||||
}
|
||||
|
||||
return closureValue
|
||||
}
|
||||
|
||||
internal func cs_autoreleasepool<T>(@noescape closure: () throws -> T) throws -> T {
|
||||
|
||||
var closureValue: T!
|
||||
var closureError: ErrorType?
|
||||
autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
closureValue = try closure()
|
||||
}
|
||||
catch {
|
||||
|
||||
closureError = error
|
||||
}
|
||||
}
|
||||
|
||||
if let closureError = closureError {
|
||||
|
||||
throw closureError
|
||||
}
|
||||
return closureValue
|
||||
}
|
||||
|
||||
internal func cs_autoreleasepool(@noescape closure: () throws -> Void) throws {
|
||||
|
||||
var closureError: ErrorType?
|
||||
autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
try closure()
|
||||
}
|
||||
catch {
|
||||
|
||||
closureError = error
|
||||
}
|
||||
}
|
||||
|
||||
if let closureError = closureError {
|
||||
|
||||
throw closureError
|
||||
}
|
||||
}
|
||||
|
||||
internal func cs_getAssociatedObjectForKey<T: AnyObject>(key: UnsafePointer<Void>, inObject object: AnyObject) -> T? {
|
||||
internal func cs_getAssociatedObjectForKey<T: AnyObject>(_ key: UnsafeRawPointer, inObject object: Any) -> T? {
|
||||
|
||||
switch objc_getAssociatedObject(object, key) {
|
||||
|
||||
@@ -103,17 +42,17 @@ internal func cs_getAssociatedObjectForKey<T: AnyObject>(key: UnsafePointer<Void
|
||||
}
|
||||
}
|
||||
|
||||
internal func cs_setAssociatedRetainedObject<T: AnyObject>(associatedObject: T?, forKey key: UnsafePointer<Void>, inObject object: AnyObject) {
|
||||
internal func cs_setAssociatedRetainedObject<T: AnyObject>(_ associatedObject: T?, forKey key: UnsafeRawPointer, inObject object: Any) {
|
||||
|
||||
objc_setAssociatedObject(object, key, associatedObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
}
|
||||
|
||||
internal func cs_setAssociatedCopiedObject<T: AnyObject>(associatedObject: T?, forKey key: UnsafePointer<Void>, inObject object: AnyObject) {
|
||||
internal func cs_setAssociatedCopiedObject<T: AnyObject>(_ associatedObject: T?, forKey key: UnsafeRawPointer, inObject object: Any) {
|
||||
|
||||
objc_setAssociatedObject(object, key, associatedObject, .OBJC_ASSOCIATION_COPY_NONATOMIC)
|
||||
}
|
||||
|
||||
internal func cs_setAssociatedWeakObject<T: AnyObject>(associatedObject: T?, forKey key: UnsafePointer<Void>, inObject object: AnyObject) {
|
||||
internal func cs_setAssociatedWeakObject<T: AnyObject>(_ associatedObject: T?, forKey key: UnsafeRawPointer, inObject object: Any) {
|
||||
|
||||
if let associatedObject = associatedObject {
|
||||
|
||||
@@ -128,22 +67,27 @@ internal func cs_setAssociatedWeakObject<T: AnyObject>(associatedObject: T?, for
|
||||
|
||||
// MARK: Printing Utilities
|
||||
|
||||
internal func cs_typeName<T>(value: T) -> String {
|
||||
internal func cs_typeName<T>(_ value: T) -> String {
|
||||
|
||||
return "'\(String(reflecting: value.dynamicType))'"
|
||||
return "'\(String(reflecting: type(of: value)))'"
|
||||
}
|
||||
|
||||
internal func cs_typeName<T>(value: T.Type) -> String {
|
||||
internal func cs_typeName<T>(_ value: T.Type) -> String {
|
||||
|
||||
return "'\(value)'"
|
||||
}
|
||||
|
||||
internal func cs_typeName(value: AnyClass) -> String {
|
||||
internal func cs_typeName(_ value: AnyClass) -> String {
|
||||
|
||||
return "'\(value)'"
|
||||
}
|
||||
|
||||
internal func cs_typeName(name: String?) -> String {
|
||||
internal func cs_typeName(_ name: String) -> String {
|
||||
|
||||
return "<\(name)>"
|
||||
}
|
||||
|
||||
internal func cs_typeName(_ name: String?) -> String {
|
||||
|
||||
return "<\(name ?? "unknown")>"
|
||||
}
|
||||
|
||||
@@ -29,15 +29,15 @@ import CoreData
|
||||
|
||||
// MARK: - MigrationManager
|
||||
|
||||
internal final class MigrationManager: NSMigrationManager, NSProgressReporting {
|
||||
internal final class MigrationManager: NSMigrationManager, ProgressReporting {
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
override func didChangeValueForKey(key: String) {
|
||||
override func didChangeValue(forKey key: String) {
|
||||
|
||||
super.didChangeValueForKey(key)
|
||||
super.didChangeValue(forKey: key)
|
||||
|
||||
guard key == "migrationProgress" else {
|
||||
guard key == #keyPath(NSMigrationManager.migrationProgress) else {
|
||||
|
||||
return
|
||||
}
|
||||
@@ -48,7 +48,7 @@ internal final class MigrationManager: NSMigrationManager, NSProgressReporting {
|
||||
|
||||
// MARK: NSMigrationManager
|
||||
|
||||
init(sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, progress: NSProgress) {
|
||||
init(sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, progress: Progress) {
|
||||
|
||||
self.progress = progress
|
||||
|
||||
@@ -56,7 +56,7 @@ internal final class MigrationManager: NSMigrationManager, NSProgressReporting {
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSProgressReporting
|
||||
// MARK: ProgressReporting
|
||||
|
||||
let progress: NSProgress
|
||||
let progress: Progress
|
||||
}
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - NSManagedObjectContext
|
||||
@@ -50,7 +47,7 @@ internal extension NSManagedObjectContext {
|
||||
set {
|
||||
|
||||
cs_setAssociatedCopiedObject(
|
||||
NSNumber(bool: newValue),
|
||||
NSNumber(value: newValue),
|
||||
forKey: &PropertyKeys.shouldCascadeSavesToParent,
|
||||
inObject: self
|
||||
)
|
||||
@@ -58,40 +55,30 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityDescriptionForEntityType(entity: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
internal func entityDescriptionForEntityType(_ entity: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return self.entityDescriptionForEntityClass(entity)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityDescriptionForEntityClass(entity: AnyClass) -> NSEntityDescription? {
|
||||
internal func entityDescriptionForEntityClass(_ entity: AnyClass) -> NSEntityDescription? {
|
||||
|
||||
guard let entityName = self.parentStack?.entityNameForEntityClass(entity) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return NSEntityDescription.entityForName(
|
||||
entityName,
|
||||
inManagedObjectContext: self
|
||||
return NSEntityDescription.entity(
|
||||
forEntityName: entityName,
|
||||
in: self
|
||||
)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func setupForCoreStoreWithContextName(contextName: String) {
|
||||
|
||||
#if USE_FRAMEWORKS
|
||||
|
||||
self.name = contextName
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
|
||||
self.name = contextName
|
||||
}
|
||||
#endif
|
||||
internal func setupForCoreStoreWithContextName(_ contextName: String) {
|
||||
|
||||
self.name = contextName
|
||||
self.observerForWillSaveNotification = NotificationObserver(
|
||||
notificationName: NSManagedObjectContextWillSaveNotification,
|
||||
notificationName: NSNotification.Name.NSManagedObjectContextWillSave,
|
||||
object: self,
|
||||
closure: { (note) -> Void in
|
||||
|
||||
@@ -105,7 +92,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
do {
|
||||
|
||||
try context.obtainPermanentIDsForObjects(Array(insertedObjects))
|
||||
try context.obtainPermanentIDs(for: Array(insertedObjects))
|
||||
}
|
||||
catch {
|
||||
|
||||
|
||||
@@ -29,20 +29,20 @@ import CoreData
|
||||
|
||||
// MARK: - NSManagedObjectContext
|
||||
|
||||
internal extension NSManagedObjectContext {
|
||||
extension NSManagedObjectContext: FetchableSource, QueryableSource {
|
||||
|
||||
// MARK: Internal: Fetch Existing
|
||||
// MARK: FetchableSource
|
||||
|
||||
@nonobjc
|
||||
internal func fetchExisting<T: NSManagedObject>(object: T) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject>(_ object: T) -> T? {
|
||||
|
||||
if object.objectID.temporaryID {
|
||||
if object.objectID.isTemporaryID {
|
||||
|
||||
do {
|
||||
|
||||
try withExtendedLifetime(self) { (context: NSManagedObjectContext) -> Void in
|
||||
|
||||
try context.obtainPermanentIDsForObjects([object])
|
||||
try context.obtainPermanentIDs(for: [object])
|
||||
}
|
||||
}
|
||||
catch {
|
||||
@@ -54,10 +54,9 @@ internal extension NSManagedObjectContext {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
let existingObject = try self.existingObjectWithID(object.objectID)
|
||||
let existingObject = try self.existingObject(with: object.objectID)
|
||||
return (existingObject as! T)
|
||||
}
|
||||
catch {
|
||||
@@ -70,125 +69,86 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Fetch One
|
||||
@nonobjc
|
||||
public func fetchExisting<T: NSManagedObject>(_ objectID: NSManagedObjectID) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
return (try self.existingObject(with: objectID) as! T)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
|
||||
|
||||
return objects.flatMap { (try? self.existingObject(with: $0.objectID)) as? T }
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
public func fetchExisting<T: NSManagedObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
|
||||
|
||||
return objectIDs.flatMap { (try? self.existingObject(with: $0)) as? T }
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
|
||||
return self.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchOne(fetchRequest)
|
||||
return self.fetchOne(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(fetchRequest: NSFetchRequest) -> T? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest) as? [T]
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Fetch All
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
return self.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
public func fetchAll<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchAll(fetchRequest)
|
||||
return self.fetchAll(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(fetchRequest: NSFetchRequest) -> [T]? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest) as? [T]
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fetchResults
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Count
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
return self.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
public func fetchCount<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -198,127 +158,65 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchCount(fetchRequest)
|
||||
return self.fetchCount(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount(fetchRequest: NSFetchRequest) -> Int? {
|
||||
|
||||
var count = 0
|
||||
var countError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
count = try self.countForFetchRequest(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
countError = error
|
||||
}
|
||||
}
|
||||
if count == NSNotFound {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(countError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Object ID
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
return self.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
public func fetchObjectID<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchRequest.resultType = .managedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchObjectID(fetchRequest)
|
||||
return self.fetchObjectID(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID(fetchRequest: NSFetchRequest) -> NSManagedObjectID? {
|
||||
|
||||
var fetchResults: [NSManagedObjectID]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest) as? [NSManagedObjectID]
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Object IDs
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDs<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchRequest.resultType = .managedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchObjectIDs(fetchRequest)
|
||||
return self.fetchObjectIDs(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs(fetchRequest: NSFetchRequest) -> [NSManagedObjectID]? {
|
||||
internal func fetchObjectIDs(_ fetchRequest: NSFetchRequest<NSManagedObjectID>) -> [NSManagedObjectID]? {
|
||||
|
||||
var fetchResults: [NSManagedObjectID]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest) as? [NSManagedObjectID]
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
@@ -333,85 +231,20 @@ internal extension NSManagedObjectContext {
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fetchResults
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Delete All
|
||||
// MARK: QueryableSource
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
|
||||
|
||||
return self.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.returnsObjectsAsFaults = true
|
||||
fetchRequest.includesPropertyValues = false
|
||||
deleteClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.deleteAll(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll(fetchRequest: NSFetchRequest) -> Int? {
|
||||
|
||||
var numberOfDeletedObjects: Int?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
cs_autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
let fetchResults = try self.executeFetchRequest(fetchRequest) as? [NSManagedObject] ?? []
|
||||
for object in fetchResults {
|
||||
|
||||
self.deleteObject(object)
|
||||
}
|
||||
numberOfDeletedObjects = fetchResults.count
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
}
|
||||
if numberOfDeletedObjects == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return numberOfDeletedObjects
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Value
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
|
||||
return self.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
public func queryValue<T: NSManagedObject, U: SelectValueResultType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -430,82 +263,13 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<U: SelectValueResultType>(selectTerms: [SelectTerm], fetchRequest: NSFetchRequest) -> U? {
|
||||
|
||||
var fetchResults: [AnyObject]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if let fetchResults = fetchResults {
|
||||
|
||||
if let rawResult = fetchResults.first as? NSDictionary,
|
||||
let rawObject: AnyObject = rawResult[selectTerms.keyPathForFirstSelectTerm()] {
|
||||
|
||||
return Select<U>.ReturnType.fromResultObject(rawObject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue(selectTerms: [SelectTerm], fetchRequest: NSFetchRequest) -> AnyObject? {
|
||||
|
||||
var fetchResults: [AnyObject]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if let fetchResults = fetchResults {
|
||||
|
||||
if let rawResult = fetchResults.first as? NSDictionary,
|
||||
let rawObject: AnyObject = rawResult[selectTerms.keyPathForFirstSelectTerm()] {
|
||||
|
||||
return rawObject
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Attributes
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
|
||||
|
||||
return self.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributes<T: NSManagedObject>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -522,16 +286,210 @@ internal extension NSManagedObjectContext {
|
||||
return self.queryAttributes(fetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: FetchableSource, QueryableSource
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes(fetchRequest: NSFetchRequest) -> [[NSString: AnyObject]]? {
|
||||
public func internalContext() -> NSManagedObjectContext {
|
||||
|
||||
var fetchResults: [AnyObject]?
|
||||
var fetchError: ErrorType?
|
||||
self.performBlockAndWait {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSManagedObjectContext (Internal)
|
||||
|
||||
internal extension NSManagedObjectContext {
|
||||
|
||||
// MARK: Fetching
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> T? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.executeFetchRequest(fetchRequest)
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> [T]? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return fetchResults
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Int? {
|
||||
|
||||
var count = 0
|
||||
var countError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
count = try self.count(for: fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
countError = error
|
||||
}
|
||||
}
|
||||
if count == NSNotFound {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(countError),
|
||||
"Failed executing count request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID(_ fetchRequest: NSFetchRequest<NSManagedObjectID>) -> NSManagedObjectID? {
|
||||
|
||||
var fetchResults: [NSManagedObjectID]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if fetchResults == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
|
||||
// MARK: Querying
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<U: SelectValueResultType>(_ selectTerms: [SelectTerm], fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> U? {
|
||||
|
||||
var fetchResults: [Any]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if let fetchResults = fetchResults {
|
||||
|
||||
if let rawResult = fetchResults.first as? NSDictionary,
|
||||
let rawObject = rawResult[selectTerms.keyPathForFirstSelectTerm()] {
|
||||
|
||||
return Select<U>.ReturnType.fromResultObject(rawObject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue(_ selectTerms: [SelectTerm], fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Any? {
|
||||
|
||||
var fetchResults: [Any]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
if let fetchResults = fetchResults {
|
||||
|
||||
if let rawResult = fetchResults.first as? NSDictionary,
|
||||
let rawObject = rawResult[selectTerms.keyPathForFirstSelectTerm()] {
|
||||
|
||||
return rawObject
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> [[String: Any]]? {
|
||||
|
||||
var fetchResults: [Any]?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
do {
|
||||
|
||||
fetchResults = try self.fetch(fetchRequest)
|
||||
}
|
||||
catch {
|
||||
|
||||
@@ -549,4 +507,67 @@ internal extension NSManagedObjectContext {
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deleting
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(_ from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
|
||||
|
||||
return self.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(_ from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchRequest.returnsObjectsAsFaults = true
|
||||
fetchRequest.includesPropertyValues = false
|
||||
deleteClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.deleteAll(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> Int? {
|
||||
|
||||
var numberOfDeletedObjects: Int?
|
||||
var fetchError: Error?
|
||||
self.performAndWait {
|
||||
|
||||
autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
let fetchResults = try self.fetch(fetchRequest)
|
||||
for object in fetchResults {
|
||||
|
||||
self.delete(object)
|
||||
}
|
||||
numberOfDeletedObjects = fetchResults.count
|
||||
}
|
||||
catch {
|
||||
|
||||
fetchError = error
|
||||
}
|
||||
}
|
||||
}
|
||||
if numberOfDeletedObjects == nil {
|
||||
|
||||
CoreStore.log(
|
||||
CoreStoreError(fetchError),
|
||||
"Failed executing fetch request."
|
||||
)
|
||||
return nil
|
||||
}
|
||||
return numberOfDeletedObjects
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
get {
|
||||
|
||||
if let parentContext = self.parentContext {
|
||||
if let parentContext = self.parent {
|
||||
|
||||
return parentContext.parentStack
|
||||
}
|
||||
@@ -47,7 +47,7 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
set {
|
||||
|
||||
guard self.parentContext == nil else {
|
||||
guard self.parent == nil else {
|
||||
|
||||
return
|
||||
}
|
||||
@@ -61,9 +61,9 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal static func rootSavingContextForCoordinator(coordinator: NSPersistentStoreCoordinator) -> NSManagedObjectContext {
|
||||
internal static func rootSavingContextForCoordinator(_ coordinator: NSPersistentStoreCoordinator) -> NSManagedObjectContext {
|
||||
|
||||
let context = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
|
||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||
context.persistentStoreCoordinator = coordinator
|
||||
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
||||
context.undoManager = nil
|
||||
@@ -72,18 +72,20 @@ internal extension NSManagedObjectContext {
|
||||
#if os(iOS) || os(OSX)
|
||||
|
||||
context.observerForDidImportUbiquitousContentChangesNotification = NotificationObserver(
|
||||
notificationName: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
|
||||
notificationName: NSNotification.Name.NSPersistentStoreDidImportUbiquitousContentChanges,
|
||||
object: coordinator,
|
||||
closure: { [weak context] (note) -> Void in
|
||||
|
||||
context?.performBlock { () -> Void in
|
||||
context?.perform { () -> Void in
|
||||
|
||||
let updatedObjectIDs = (note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObjectID>) ?? []
|
||||
for objectID in updatedObjectIDs {
|
||||
if let updatedObjectIDs = (note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObjectID>) {
|
||||
|
||||
context?.objectWithID(objectID).willAccessValueForKey(nil)
|
||||
for objectID in updatedObjectIDs {
|
||||
|
||||
context?.object(with: objectID).willAccessValue(forKey: nil)
|
||||
}
|
||||
}
|
||||
context?.mergeChangesFromContextDidSaveNotification(note)
|
||||
context?.mergeChanges(fromContextDidSave: note)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -94,15 +96,15 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal static func mainContextForRootContext(rootContext: NSManagedObjectContext) -> NSManagedObjectContext {
|
||||
internal static func mainContextForRootContext(_ rootContext: NSManagedObjectContext) -> NSManagedObjectContext {
|
||||
|
||||
let context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
|
||||
context.parentContext = rootContext
|
||||
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
|
||||
context.parent = rootContext
|
||||
context.mergePolicy = NSRollbackMergePolicy
|
||||
context.undoManager = nil
|
||||
context.setupForCoreStoreWithContextName("com.corestore.maincontext")
|
||||
context.observerForDidSaveNotification = NotificationObserver(
|
||||
notificationName: NSManagedObjectContextDidSaveNotification,
|
||||
notificationName: NSNotification.Name.NSManagedObjectContextDidSave,
|
||||
object: rootContext,
|
||||
closure: { [weak context] (note) -> Void in
|
||||
|
||||
@@ -113,21 +115,22 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
let mergeChanges = { () -> Void in
|
||||
|
||||
let updatedObjects = (note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject>) ?? []
|
||||
for object in updatedObjects {
|
||||
if let updatedObjects = (note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject>) {
|
||||
|
||||
context.objectWithID(object.objectID).willAccessValueForKey(nil)
|
||||
for object in updatedObjects {
|
||||
|
||||
context.object(with: object.objectID).willAccessValue(forKey: nil)
|
||||
}
|
||||
}
|
||||
context.mergeChangesFromContextDidSaveNotification(note)
|
||||
context.mergeChanges(fromContextDidSave: note)
|
||||
}
|
||||
|
||||
if rootContext.isSavingSynchronously == true {
|
||||
|
||||
context.performBlockAndWait(mergeChanges)
|
||||
context.performAndWait(mergeChanges)
|
||||
}
|
||||
else {
|
||||
|
||||
context.performBlock(mergeChanges)
|
||||
context.perform(mergeChanges)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - NSManagedObjectContext
|
||||
@@ -70,13 +67,55 @@ internal extension NSManagedObjectContext {
|
||||
set {
|
||||
|
||||
cs_setAssociatedWeakObject(
|
||||
newValue.flatMap { NSNumber(bool: $0) },
|
||||
newValue.flatMap { NSNumber(value: $0) },
|
||||
forKey: &PropertyKeys.isSavingSynchronously,
|
||||
inObject: self
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal var isTransactionContext: Bool {
|
||||
|
||||
get {
|
||||
|
||||
let value: NSNumber? = cs_getAssociatedObjectForKey(
|
||||
&PropertyKeys.isTransactionContext,
|
||||
inObject: self
|
||||
)
|
||||
return value?.boolValue == true
|
||||
}
|
||||
set {
|
||||
|
||||
cs_setAssociatedCopiedObject(
|
||||
NSNumber(value: newValue),
|
||||
forKey: &PropertyKeys.isTransactionContext,
|
||||
inObject: self
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal var isDataStackContext: Bool {
|
||||
|
||||
get {
|
||||
|
||||
let value: NSNumber? = cs_getAssociatedObjectForKey(
|
||||
&PropertyKeys.isDataStackContext,
|
||||
inObject: self
|
||||
)
|
||||
return value?.boolValue == true
|
||||
}
|
||||
set {
|
||||
|
||||
cs_setAssociatedCopiedObject(
|
||||
NSNumber(value: newValue),
|
||||
forKey: &PropertyKeys.isDataStackContext,
|
||||
inObject: self
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func isRunningInAllowedQueue() -> Bool {
|
||||
|
||||
@@ -88,10 +127,10 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func temporaryContextInTransactionWithConcurrencyType(concurrencyType: NSManagedObjectContextConcurrencyType) -> NSManagedObjectContext {
|
||||
internal func temporaryContextInTransactionWithConcurrencyType(_ concurrencyType: NSManagedObjectContextConcurrencyType) -> NSManagedObjectContext {
|
||||
|
||||
let context = NSManagedObjectContext(concurrencyType: concurrencyType)
|
||||
context.parentContext = self
|
||||
context.parent = self
|
||||
context.parentStack = self.parentStack
|
||||
context.setupForCoreStoreWithContextName("com.corestore.temporarycontext")
|
||||
context.shouldCascadeSavesToParent = (self.parentStack?.rootSavingContext == self)
|
||||
@@ -105,7 +144,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
var result = SaveResult(hasChanges: false)
|
||||
|
||||
self.performBlockAndWait {
|
||||
self.performAndWait {
|
||||
|
||||
guard self.hasChanges else {
|
||||
|
||||
@@ -123,20 +162,20 @@ internal extension NSManagedObjectContext {
|
||||
let saveError = CoreStoreError(error)
|
||||
CoreStore.log(
|
||||
saveError,
|
||||
"Failed to save \(cs_typeName(NSManagedObjectContext))."
|
||||
"Failed to save \(cs_typeName(NSManagedObjectContext.self))."
|
||||
)
|
||||
result = SaveResult(saveError)
|
||||
return
|
||||
}
|
||||
|
||||
if let parentContext = self.parentContext where self.shouldCascadeSavesToParent {
|
||||
if let parentContext = self.parent, self.shouldCascadeSavesToParent {
|
||||
|
||||
switch parentContext.saveSynchronously(waitForMerge: waitForMerge) {
|
||||
|
||||
case .Success:
|
||||
case .success:
|
||||
result = SaveResult(hasChanges: true)
|
||||
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
result = SaveResult(error)
|
||||
}
|
||||
}
|
||||
@@ -150,15 +189,15 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func saveAsynchronouslyWithCompletion(completion: ((result: SaveResult) -> Void) = { _ in }) {
|
||||
internal func saveAsynchronouslyWithCompletion(_ completion: @escaping ((_ result: SaveResult) -> Void) = { _ in }) {
|
||||
|
||||
self.performBlock {
|
||||
self.perform {
|
||||
|
||||
guard self.hasChanges else {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(result: SaveResult(hasChanges: false))
|
||||
completion(SaveResult(hasChanges: false))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -174,24 +213,24 @@ internal extension NSManagedObjectContext {
|
||||
let saveError = CoreStoreError(error)
|
||||
CoreStore.log(
|
||||
saveError,
|
||||
"Failed to save \(cs_typeName(NSManagedObjectContext))."
|
||||
"Failed to save \(cs_typeName(NSManagedObjectContext.self))."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(result: SaveResult(saveError))
|
||||
completion(SaveResult(saveError))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if let parentContext = self.parentContext where self.shouldCascadeSavesToParent {
|
||||
if self.shouldCascadeSavesToParent, let parentContext = self.parent {
|
||||
|
||||
parentContext.saveAsynchronouslyWithCompletion(completion)
|
||||
}
|
||||
else {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(result: SaveResult(hasChanges: true))
|
||||
completion(SaveResult(hasChanges: true))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,7 +245,7 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
else {
|
||||
|
||||
self.registeredObjects.forEach { self.refreshObject($0, mergeChanges: true) }
|
||||
self.registeredObjects.forEach { self.refresh($0, mergeChanges: true) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,5 +256,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
static var parentTransaction: Void?
|
||||
static var isSavingSynchronously: Void?
|
||||
static var isTransactionContext: Void?
|
||||
static var isDataStackContext: Void?
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,32 +34,34 @@ internal extension NSManagedObjectModel {
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal static func fromBundle(bundle: NSBundle, modelName: String, modelVersionHints: Set<String> = []) -> NSManagedObjectModel {
|
||||
internal static func fromBundle(_ bundle: Bundle, modelName: String, modelVersionHints: Set<String> = []) -> NSManagedObjectModel {
|
||||
|
||||
guard let modelFilePath = bundle.pathForResource(modelName, ofType: "momd") else {
|
||||
guard let modelFilePath = bundle.path(forResource: modelName, ofType: "momd") else {
|
||||
|
||||
// For users migrating from very old Xcode versions: Old xcdatamodel files are not contained inside xcdatamodeld (with a "d"), and will thus fail this check. If that was the case, create a new xcdatamodeld file and copy all contents into the new model.
|
||||
CoreStore.abort("Could not find \"\(modelName).momd\" from the bundle. \(bundle)")
|
||||
}
|
||||
|
||||
let modelFileURL = NSURL(fileURLWithPath: modelFilePath)
|
||||
let versionInfoPlistURL = modelFileURL.URLByAppendingPathComponent("VersionInfo.plist", isDirectory: false)!
|
||||
let modelFileURL = URL(fileURLWithPath: modelFilePath)
|
||||
let versionInfoPlistURL = modelFileURL.appendingPathComponent("VersionInfo.plist", isDirectory: false)
|
||||
|
||||
guard let versionInfo = NSDictionary(contentsOfURL: versionInfoPlistURL),
|
||||
guard let versionInfo = NSDictionary(contentsOf: versionInfoPlistURL),
|
||||
let versionHashes = versionInfo["NSManagedObjectModel_VersionHashes"] as? [String: AnyObject] else {
|
||||
|
||||
CoreStore.abort("Could not load \(cs_typeName(NSManagedObjectModel)) metadata from path \"\(versionInfoPlistURL)\".")
|
||||
CoreStore.abort("Could not load \(cs_typeName(NSManagedObjectModel.self)) metadata from path \"\(versionInfoPlistURL)\".")
|
||||
}
|
||||
|
||||
let modelVersions = Set(versionHashes.keys)
|
||||
let currentModelVersion: String
|
||||
if let plistModelVersion = versionInfo["NSManagedObjectModel_CurrentVersionName"] as? String where modelVersionHints.isEmpty || modelVersionHints.contains(plistModelVersion) {
|
||||
if let plistModelVersion = versionInfo["NSManagedObjectModel_CurrentVersionName"] as? String,
|
||||
modelVersionHints.isEmpty || modelVersionHints.contains(plistModelVersion) {
|
||||
|
||||
currentModelVersion = plistModelVersion
|
||||
}
|
||||
else if let resolvedVersion = modelVersions.intersect(modelVersionHints).first {
|
||||
else if let resolvedVersion = modelVersions.intersection(modelVersionHints).first {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
.warning,
|
||||
message: "The MigrationChain leaf versions do not include the model file's current version. Resolving to version \"\(resolvedVersion)\"."
|
||||
)
|
||||
currentModelVersion = resolvedVersion
|
||||
@@ -69,7 +71,7 @@ internal extension NSManagedObjectModel {
|
||||
if !modelVersionHints.isEmpty {
|
||||
|
||||
CoreStore.log(
|
||||
.Warning,
|
||||
.warning,
|
||||
message: "The MigrationChain leaf versions do not include any of the model file's embedded versions. Resolving to version \"\(resolvedVersion)\"."
|
||||
)
|
||||
}
|
||||
@@ -80,10 +82,10 @@ internal extension NSManagedObjectModel {
|
||||
CoreStore.abort("No model files were found in URL \"\(modelFileURL)\".")
|
||||
}
|
||||
|
||||
var modelVersionFileURL: NSURL?
|
||||
var modelVersionFileURL: URL?
|
||||
for modelVersion in modelVersions {
|
||||
|
||||
let fileURL = modelFileURL.URLByAppendingPathComponent("\(modelVersion).mom", isDirectory: false)!
|
||||
let fileURL = modelFileURL.appendingPathComponent("\(modelVersion).mom", isDirectory: false)
|
||||
|
||||
if modelVersion == currentModelVersion {
|
||||
|
||||
@@ -92,13 +94,13 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
precondition(
|
||||
NSManagedObjectModel(contentsOfURL: fileURL) != nil,
|
||||
NSManagedObjectModel(contentsOf: fileURL) != nil,
|
||||
"Could not find the \"\(modelVersion).mom\" version file for the model at URL \"\(modelFileURL)\"."
|
||||
)
|
||||
}
|
||||
|
||||
if let modelVersionFileURL = modelVersionFileURL,
|
||||
let rootModel = NSManagedObjectModel(contentsOfURL: modelVersionFileURL) {
|
||||
let rootModel = NSManagedObjectModel(contentsOf: modelVersionFileURL) {
|
||||
|
||||
rootModel.modelVersionFileURL = modelVersionFileURL
|
||||
rootModel.modelVersions = modelVersions
|
||||
@@ -106,7 +108,7 @@ internal extension NSManagedObjectModel {
|
||||
return rootModel
|
||||
}
|
||||
|
||||
CoreStore.abort("Could not create an \(cs_typeName(NSManagedObjectModel)) from the model at URL \"\(modelFileURL)\".")
|
||||
CoreStore.abort("Could not create an \(cs_typeName(NSManagedObjectModel.self)) from the model at URL \"\(modelFileURL)\".")
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
@@ -152,7 +154,7 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityNameForClass(entityClass: AnyClass) -> String {
|
||||
internal func entityNameForClass(_ entityClass: AnyClass) -> String {
|
||||
|
||||
return self.entityNameMapping[NSStringFromClass(entityClass)]!
|
||||
}
|
||||
@@ -183,14 +185,14 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
guard let modelFileURL = self.modelFileURL,
|
||||
let modelVersions = self.modelVersions
|
||||
where modelVersions.contains(modelVersion) else {
|
||||
let modelVersions = self.modelVersions,
|
||||
modelVersions.contains(modelVersion) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
let versionModelFileURL = modelFileURL.URLByAppendingPathComponent("\(modelVersion).mom", isDirectory: false)
|
||||
guard let model = NSManagedObjectModel(contentsOfURL: versionModelFileURL!) else {
|
||||
let versionModelFileURL = modelFileURL.appendingPathComponent("\(modelVersion).mom", isDirectory: false)
|
||||
guard let model = NSManagedObjectModel(contentsOf: versionModelFileURL) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -202,15 +204,15 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal subscript(metadata: [String: AnyObject]) -> NSManagedObjectModel? {
|
||||
internal subscript(metadata: [String: Any]) -> NSManagedObjectModel? {
|
||||
|
||||
guard let modelHashes = metadata[NSStoreModelVersionHashesKey] as? [String : NSData] else {
|
||||
guard let modelHashes = metadata[NSStoreModelVersionHashesKey] as? [String : Data] else {
|
||||
|
||||
return nil
|
||||
}
|
||||
for modelVersion in self.modelVersions ?? [] {
|
||||
|
||||
if let versionModel = self[modelVersion] where modelHashes == versionModel.entityVersionHashesByName {
|
||||
if let versionModel = self[modelVersion], modelHashes == versionModel.entityVersionHashesByName {
|
||||
|
||||
return versionModel
|
||||
}
|
||||
@@ -222,16 +224,16 @@ internal extension NSManagedObjectModel {
|
||||
// MARK: Private
|
||||
|
||||
@nonobjc
|
||||
private var modelFileURL: NSURL? {
|
||||
private var modelFileURL: URL? {
|
||||
|
||||
get {
|
||||
|
||||
return self.modelVersionFileURL?.URLByDeletingLastPathComponent
|
||||
return self.modelVersionFileURL?.deletingLastPathComponent()
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
private var modelVersionFileURL: NSURL? {
|
||||
private var modelVersionFileURL: URL? {
|
||||
|
||||
get {
|
||||
|
||||
@@ -239,12 +241,12 @@ internal extension NSManagedObjectModel {
|
||||
&PropertyKeys.modelVersionFileURL,
|
||||
inObject: self
|
||||
)
|
||||
return value
|
||||
return value as URL?
|
||||
}
|
||||
set {
|
||||
|
||||
cs_setAssociatedCopiedObject(
|
||||
newValue,
|
||||
newValue as NSURL?,
|
||||
forKey: &PropertyKeys.modelVersionFileURL,
|
||||
inObject: self
|
||||
)
|
||||
@@ -270,7 +272,7 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
let className = $0.managedObjectClassName
|
||||
mapping[className] = entityName
|
||||
mapping[className!] = entityName
|
||||
}
|
||||
cs_setAssociatedCopiedObject(
|
||||
mapping as NSDictionary,
|
||||
|
||||
@@ -66,15 +66,15 @@ internal extension NSPersistentStore {
|
||||
|
||||
// MARK: - StorageObject
|
||||
|
||||
private class StorageObject: NSObject {
|
||||
fileprivate class StorageObject: NSObject {
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@nonobjc
|
||||
private let storageInterface: StorageInterface?
|
||||
fileprivate let storageInterface: StorageInterface?
|
||||
|
||||
@nonobjc
|
||||
private init(_ storage: StorageInterface?) {
|
||||
fileprivate init(_ storage: StorageInterface?) {
|
||||
|
||||
self.storageInterface = storage
|
||||
}
|
||||
|
||||
@@ -26,134 +26,53 @@
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - NSPersistentStoreCoordinator
|
||||
|
||||
internal extension NSPersistentStoreCoordinator {
|
||||
|
||||
@nonobjc
|
||||
internal func performAsynchronously(closure: () -> Void) {
|
||||
internal func performAsynchronously(_ closure: @escaping () -> Void) {
|
||||
|
||||
#if USE_FRAMEWORKS
|
||||
|
||||
self.performBlock(closure)
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
|
||||
self.performBlock(closure)
|
||||
}
|
||||
else {
|
||||
|
||||
self.lock()
|
||||
GCDQueue.Default.async {
|
||||
|
||||
closure()
|
||||
self.unlock()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
self.perform(closure)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func performSynchronously<T>(closure: () -> T) -> T {
|
||||
internal func performSynchronously<T>(_ closure: @escaping () -> T) -> T {
|
||||
|
||||
var result: T?
|
||||
#if USE_FRAMEWORKS
|
||||
self.performAndWait {
|
||||
|
||||
self.performBlockAndWait {
|
||||
|
||||
result = closure()
|
||||
}
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
|
||||
self.performBlockAndWait {
|
||||
|
||||
result = closure()
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
self.lock()
|
||||
cs_autoreleasepool {
|
||||
|
||||
result = closure()
|
||||
}
|
||||
self.unlock()
|
||||
}
|
||||
#endif
|
||||
|
||||
result = closure()
|
||||
}
|
||||
return result!
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func performSynchronously<T>(closure: () throws -> T) throws -> T {
|
||||
internal func performSynchronously<T>(_ closure: @escaping () throws -> T) throws -> T {
|
||||
|
||||
var closureError: ErrorType?
|
||||
var closureError: Error?
|
||||
var result: T?
|
||||
#if USE_FRAMEWORKS
|
||||
self.performAndWait {
|
||||
|
||||
self.performBlockAndWait {
|
||||
do {
|
||||
|
||||
do {
|
||||
|
||||
result = try closure()
|
||||
}
|
||||
catch {
|
||||
|
||||
closureError = error
|
||||
}
|
||||
result = try closure()
|
||||
}
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
catch {
|
||||
|
||||
self.performBlockAndWait {
|
||||
|
||||
do {
|
||||
|
||||
result = try closure()
|
||||
}
|
||||
catch {
|
||||
|
||||
closureError = error
|
||||
}
|
||||
}
|
||||
closureError = error
|
||||
}
|
||||
else {
|
||||
|
||||
self.lock()
|
||||
cs_autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
result = try closure()
|
||||
}
|
||||
catch {
|
||||
|
||||
closureError = error
|
||||
}
|
||||
}
|
||||
self.unlock()
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
if let closureError = closureError {
|
||||
|
||||
throw closureError
|
||||
}
|
||||
|
||||
return result!
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func addPersistentStoreSynchronously(storeType: String, configuration: String?, URL storeURL: NSURL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore {
|
||||
internal func addPersistentStoreSynchronously(_ storeType: String, configuration: String?, URL storeURL: URL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore {
|
||||
|
||||
var store: NSPersistentStore?
|
||||
var storeError: NSError?
|
||||
@@ -161,10 +80,10 @@ internal extension NSPersistentStoreCoordinator {
|
||||
|
||||
do {
|
||||
|
||||
store = try self.addPersistentStoreWithType(
|
||||
storeType,
|
||||
configuration: configuration,
|
||||
URL: storeURL,
|
||||
store = try self.addPersistentStore(
|
||||
ofType: storeType,
|
||||
configurationName: configuration,
|
||||
at: storeURL,
|
||||
options: options
|
||||
)
|
||||
}
|
||||
@@ -173,12 +92,10 @@ internal extension NSPersistentStoreCoordinator {
|
||||
storeError = error as NSError
|
||||
}
|
||||
}
|
||||
|
||||
if let store = store {
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
throw CoreStoreError(storeError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,18 +34,18 @@ internal final class NotificationObserver {
|
||||
|
||||
let observer: NSObjectProtocol
|
||||
|
||||
init(notificationName: String, object: AnyObject?, queue: NSOperationQueue? = nil, closure: (note: NSNotification) -> Void) {
|
||||
init(notificationName: Notification.Name, object: Any?, queue: OperationQueue? = nil, closure: @escaping (_ note: Notification) -> Void) {
|
||||
|
||||
self.observer = NSNotificationCenter.defaultCenter().addObserverForName(
|
||||
notificationName,
|
||||
self.observer = NotificationCenter.default.addObserver(
|
||||
forName: notificationName,
|
||||
object: object,
|
||||
queue: queue,
|
||||
usingBlock: closure
|
||||
using: closure
|
||||
)
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
||||
NSNotificationCenter.defaultCenter().removeObserver(self.observer)
|
||||
NotificationCenter.default.removeObserver(self.observer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ extension AsynchronousDataTransaction: CustomDebugStringConvertible, CoreStoreDe
|
||||
("supportsUndo", self.supportsUndo),
|
||||
("bypassesQueueing", self.bypassesQueueing),
|
||||
("isCommitted", self.isCommitted),
|
||||
("result", self.result)
|
||||
("result", self.result as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -72,27 +72,27 @@ extension CloudStorageOptions: CustomDebugStringConvertible, CoreStoreDebugStrin
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
var flags = [String]()
|
||||
if self.contains(.RecreateLocalStoreOnModelMismatch) {
|
||||
if self.contains(.recreateLocalStoreOnModelMismatch) {
|
||||
|
||||
flags.append(".RecreateLocalStoreOnModelMismatch")
|
||||
flags.append(".recreateLocalStoreOnModelMismatch")
|
||||
}
|
||||
if self.contains(.AllowSynchronousLightweightMigration) {
|
||||
if self.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
flags.append(".AllowSynchronousLightweightMigration")
|
||||
flags.append(".allowSynchronousLightweightMigration")
|
||||
}
|
||||
switch flags.count {
|
||||
|
||||
case 0:
|
||||
return "[.None]"
|
||||
return "[.none]"
|
||||
|
||||
case 1:
|
||||
return "[.\(flags[0])]"
|
||||
|
||||
default:
|
||||
var string = "[\n"
|
||||
string.appendContentsOf(flags.joinWithSeparator(",\n"))
|
||||
string.append(flags.joined(separator: ",\n"))
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n]")
|
||||
string.append("\n]")
|
||||
return string
|
||||
}
|
||||
}
|
||||
@@ -117,30 +117,30 @@ extension CoreStoreError: CustomDebugStringConvertible, CoreStoreDebugStringConv
|
||||
|
||||
let firstLine: String
|
||||
var info: DumpInfo = [
|
||||
("_domain", self._domain),
|
||||
("_code", self._code),
|
||||
("errorDomain", type(of: self).errorDomain),
|
||||
("errorCode", self.errorCode),
|
||||
]
|
||||
switch self {
|
||||
|
||||
case .Unknown:
|
||||
firstLine = ".Unknown"
|
||||
case .unknown:
|
||||
firstLine = ".unknown"
|
||||
|
||||
case .DifferentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
firstLine = ".DifferentStorageExistsAtURL"
|
||||
case .differentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
firstLine = ".differentStorageExistsAtURL"
|
||||
info.append(("existingPersistentStoreURL", existingPersistentStoreURL))
|
||||
|
||||
case .MappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
firstLine = ".MappingModelNotFound"
|
||||
case .mappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
firstLine = ".mappingModelNotFound"
|
||||
info.append(("localStoreURL", localStoreURL))
|
||||
info.append(("targetModel", targetModel))
|
||||
info.append(("targetModelVersion", targetModelVersion))
|
||||
|
||||
case .ProgressiveMigrationRequired(let localStoreURL):
|
||||
firstLine = ".ProgressiveMigrationRequired"
|
||||
case .progressiveMigrationRequired(let localStoreURL):
|
||||
firstLine = ".progressiveMigrationRequired"
|
||||
info.append(("localStoreURL", localStoreURL))
|
||||
|
||||
case .InternalError(let NSError):
|
||||
firstLine = ".InternalError"
|
||||
case .internalError(let NSError):
|
||||
firstLine = ".internalError"
|
||||
info.append(("NSError", NSError))
|
||||
}
|
||||
|
||||
@@ -254,8 +254,8 @@ extension ICloudStore: CustomDebugStringConvertible, CoreStoreDebugStringConvert
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("configuration", self.configuration),
|
||||
("storeOptions", self.storeOptions),
|
||||
("configuration", self.configuration as Any),
|
||||
("storeOptions", self.storeOptions as Any),
|
||||
("cacheFileURL", self.cacheFileURL),
|
||||
("cloudStorageOptions", self.cloudStorageOptions)
|
||||
)
|
||||
@@ -283,8 +283,8 @@ extension InMemoryStore: CustomDebugStringConvertible, CoreStoreDebugStringConve
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("configuration", self.configuration),
|
||||
("storeOptions", self.storeOptions)
|
||||
("configuration", self.configuration as Any),
|
||||
("storeOptions", self.storeOptions as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -309,7 +309,7 @@ extension Into: CustomDebugStringConvertible, CoreStoreDebugStringConvertible {
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("entityClass", self.entityClass),
|
||||
("configuration", self.configuration),
|
||||
("configuration", self.configuration as Any),
|
||||
("inferStoreIfPossible", self.inferStoreIfPossible)
|
||||
)
|
||||
}
|
||||
@@ -334,8 +334,8 @@ extension LegacySQLiteStore: CustomDebugStringConvertible, CoreStoreDebugStringC
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("configuration", self.configuration),
|
||||
("storeOptions", self.storeOptions),
|
||||
("configuration", self.configuration as Any),
|
||||
("storeOptions", self.storeOptions as Any),
|
||||
("fileURL", self.fileURL),
|
||||
("mappingModelBundles", self.mappingModelBundles),
|
||||
("localStorageOptions", self.localStorageOptions)
|
||||
@@ -357,7 +357,7 @@ private struct CoreStoreFetchedSectionInfoWrapper: CoreStoreDebugStringConvertib
|
||||
return createFormattedString(
|
||||
"\"\(self.sectionInfo.name)\" (", ")",
|
||||
("numberOfObjects", self.sectionInfo.numberOfObjects),
|
||||
("indexTitle", self.sectionInfo.indexTitle)
|
||||
("indexTitle", self.sectionInfo.indexTitle as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -404,31 +404,31 @@ extension LocalStorageOptions: CustomDebugStringConvertible, CoreStoreDebugStrin
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
var flags = [String]()
|
||||
if self.contains(.RecreateStoreOnModelMismatch) {
|
||||
if self.contains(.recreateStoreOnModelMismatch) {
|
||||
|
||||
flags.append(".RecreateStoreOnModelMismatch")
|
||||
flags.append(".recreateStoreOnModelMismatch")
|
||||
}
|
||||
if self.contains(.PreventProgressiveMigration) {
|
||||
if self.contains(.preventProgressiveMigration) {
|
||||
|
||||
flags.append(".PreventProgressiveMigration")
|
||||
flags.append(".preventProgressiveMigration")
|
||||
}
|
||||
if self.contains(.AllowSynchronousLightweightMigration) {
|
||||
if self.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
flags.append(".AllowSynchronousLightweightMigration")
|
||||
flags.append(".allowSynchronousLightweightMigration")
|
||||
}
|
||||
switch flags.count {
|
||||
|
||||
case 0:
|
||||
return "[.None]"
|
||||
return "[.none]"
|
||||
|
||||
case 1:
|
||||
return "[.\(flags[0])]"
|
||||
|
||||
default:
|
||||
var string = "[\n"
|
||||
string.appendContentsOf(flags.joinWithSeparator(",\n"))
|
||||
string.append(flags.joined(separator: ",\n"))
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n]")
|
||||
string.append("\n]")
|
||||
return string
|
||||
}
|
||||
}
|
||||
@@ -465,7 +465,7 @@ extension MigrationChain: CustomDebugStringConvertible, CoreStoreDebugStringConv
|
||||
steps.append(nextVersion)
|
||||
version = nextVersion
|
||||
}
|
||||
paths.append(steps.joinWithSeparator(" → "))
|
||||
paths.append(steps.joined(separator: " → "))
|
||||
}
|
||||
switch paths.count {
|
||||
|
||||
@@ -479,10 +479,10 @@ extension MigrationChain: CustomDebugStringConvertible, CoreStoreDebugStringConv
|
||||
var string = "["
|
||||
paths.forEach {
|
||||
|
||||
string.appendContentsOf("\n\($0);")
|
||||
string.append("\n\($0);")
|
||||
}
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n]")
|
||||
string.append("\n]")
|
||||
return string
|
||||
}
|
||||
}
|
||||
@@ -507,15 +507,15 @@ extension MigrationResult: CustomDebugStringConvertible, CoreStoreDebugStringCon
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success(let migrationTypes):
|
||||
case .success(let migrationTypes):
|
||||
return createFormattedString(
|
||||
".Success (", ")",
|
||||
".success (", ")",
|
||||
("migrationTypes", migrationTypes)
|
||||
)
|
||||
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
return createFormattedString(
|
||||
".Failure (", ")",
|
||||
".failure (", ")",
|
||||
("error", error)
|
||||
)
|
||||
}
|
||||
@@ -541,14 +541,14 @@ extension MigrationType: CustomDebugStringConvertible, CoreStoreDebugStringConve
|
||||
|
||||
switch self {
|
||||
|
||||
case .None(let version):
|
||||
return ".None (\"\(version)\")"
|
||||
case .none(let version):
|
||||
return ".none (\"\(version)\")"
|
||||
|
||||
case .Lightweight(let sourceVersion, let destinationVersion):
|
||||
return ".Lightweight (\"\(sourceVersion)\" → \"\(destinationVersion)\")"
|
||||
case .lightweight(let sourceVersion, let destinationVersion):
|
||||
return ".lightweight (\"\(sourceVersion)\" → \"\(destinationVersion)\")"
|
||||
|
||||
case .Heavyweight(let sourceVersion, let destinationVersion):
|
||||
return ".Heavyweight (\"\(sourceVersion)\" → \"\(destinationVersion)\")"
|
||||
case .heavyweight(let sourceVersion, let destinationVersion):
|
||||
return ".heavyweight (\"\(sourceVersion)\" → \"\(destinationVersion)\")"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -575,7 +575,7 @@ extension ObjectMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConve
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("isObjectDeleted", self.isObjectDeleted),
|
||||
("object", self.object)
|
||||
("object", self.object as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -623,15 +623,15 @@ extension SaveResult: CustomDebugStringConvertible, CoreStoreDebugStringConverti
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success(let hasChanges):
|
||||
case .success(let hasChanges):
|
||||
return createFormattedString(
|
||||
".Success (", ")",
|
||||
".success (", ")",
|
||||
("hasChanges", hasChanges)
|
||||
)
|
||||
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
return createFormattedString(
|
||||
".Failure (", ")",
|
||||
".failure (", ")",
|
||||
("error", error)
|
||||
)
|
||||
}
|
||||
@@ -708,24 +708,24 @@ extension SelectTerm: CustomDebugStringConvertible, CoreStoreDebugStringConverti
|
||||
|
||||
switch self {
|
||||
|
||||
case ._Attribute(let keyPath):
|
||||
case ._attribute(let keyPath):
|
||||
return createFormattedString(
|
||||
".Attribute (", ")",
|
||||
".attribute (", ")",
|
||||
("keyPath", keyPath)
|
||||
)
|
||||
|
||||
case ._Aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
case ._aggregate(let function, let keyPath, let alias, let nativeType):
|
||||
return createFormattedString(
|
||||
".Aggregate (", ")",
|
||||
".aggregate (", ")",
|
||||
("function", function),
|
||||
("keyPath", keyPath),
|
||||
("alias", alias),
|
||||
("nativeType", nativeType)
|
||||
)
|
||||
|
||||
case ._Identity(let alias, let nativeType):
|
||||
case ._identity(let alias, let nativeType):
|
||||
return createFormattedString(
|
||||
".Identity (", ")",
|
||||
".identity (", ")",
|
||||
("alias", alias),
|
||||
("nativeType", nativeType)
|
||||
)
|
||||
@@ -752,15 +752,15 @@ extension SetupResult: CustomDebugStringConvertible, CoreStoreDebugStringConvert
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success(let storage):
|
||||
case .success(let storage):
|
||||
return createFormattedString(
|
||||
".Success (", ")",
|
||||
".success (", ")",
|
||||
("storage", storage)
|
||||
)
|
||||
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
return createFormattedString(
|
||||
".Failure (", ")",
|
||||
".failure (", ")",
|
||||
("error", error)
|
||||
)
|
||||
}
|
||||
@@ -786,8 +786,8 @@ extension SQLiteStore: CustomDebugStringConvertible, CoreStoreDebugStringConvert
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("configuration", self.configuration),
|
||||
("storeOptions", self.storeOptions),
|
||||
("configuration", self.configuration as Any),
|
||||
("storeOptions", self.storeOptions as Any),
|
||||
("fileURL", self.fileURL),
|
||||
("mappingModelBundles", self.mappingModelBundles),
|
||||
("localStorageOptions", self.localStorageOptions)
|
||||
@@ -818,7 +818,7 @@ extension SynchronousDataTransaction: CustomDebugStringConvertible, CoreStoreDeb
|
||||
("supportsUndo", self.supportsUndo),
|
||||
("bypassesQueueing", self.bypassesQueueing),
|
||||
("isCommitted", self.isCommitted),
|
||||
("result", self.result)
|
||||
("result", self.result as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -898,7 +898,7 @@ extension Where: CustomDebugStringConvertible, CoreStoreDebugStringConvertible {
|
||||
|
||||
private typealias DumpInfo = [(key: String, value: Any)]
|
||||
|
||||
private func formattedValue(any: Any) -> String {
|
||||
private func formattedValue(_ any: Any) -> String {
|
||||
|
||||
switch any {
|
||||
|
||||
@@ -910,19 +910,19 @@ private func formattedValue(any: Any) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
private func formattedDebugDescription(any: Any) -> String {
|
||||
private func formattedDebugDescription(_ any: Any) -> String {
|
||||
|
||||
var string = "(\(String(reflecting: any.dynamicType))) "
|
||||
string.appendContentsOf(formattedValue(any))
|
||||
var string = "(\(String(reflecting: type(of: any)))) "
|
||||
string.append(formattedValue(any))
|
||||
return string
|
||||
}
|
||||
|
||||
private func createFormattedString(firstLine: String, _ lastLine: String, _ info: (key: String, value: Any)...) -> String {
|
||||
private func createFormattedString(_ firstLine: String, _ lastLine: String, _ info: (key: String, value: Any)...) -> String {
|
||||
|
||||
return createFormattedString(firstLine, lastLine, info)
|
||||
}
|
||||
|
||||
private func createFormattedString(firstLine: String, _ lastLine: String, _ info: [(key: String, value: Any)]) -> String {
|
||||
private func createFormattedString(_ firstLine: String, _ lastLine: String, _ info: [(key: String, value: Any)]) -> String {
|
||||
|
||||
var string = firstLine
|
||||
for (key, value) in info {
|
||||
@@ -930,34 +930,34 @@ private func createFormattedString(firstLine: String, _ lastLine: String, _ info
|
||||
string.appendDumpInfo(key, value)
|
||||
}
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n\(lastLine)")
|
||||
string.append("\n\(lastLine)")
|
||||
return string
|
||||
}
|
||||
|
||||
private extension String {
|
||||
fileprivate extension String {
|
||||
|
||||
private static func indention(level: Int = 1) -> String {
|
||||
fileprivate static func indention(_ level: Int = 1) -> String {
|
||||
|
||||
return String(count: level * 4, repeatedValue: Character(" "))
|
||||
return String(repeating: " ", count: level * 4)
|
||||
}
|
||||
|
||||
private func trimSwiftModuleName() -> String {
|
||||
fileprivate func trimSwiftModuleName() -> String {
|
||||
|
||||
if self.hasPrefix("Swift.") {
|
||||
|
||||
return self.substringFromIndex("Swift.".endIndex)
|
||||
return self.substring(from: "Swift.".endIndex)
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
private mutating func indent(level: Int) {
|
||||
fileprivate mutating func indent(_ level: Int) {
|
||||
|
||||
self = self.stringByReplacingOccurrencesOfString("\n", withString: "\n\(String.indention(level))")
|
||||
self = self.replacingOccurrences(of: "\n", with: "\n\(String.indention(level))")
|
||||
}
|
||||
|
||||
private mutating func appendDumpInfo(key: String, _ value: Any) {
|
||||
fileprivate mutating func appendDumpInfo(_ key: String, _ value: Any) {
|
||||
|
||||
self.appendContentsOf("\n.\(key) = \(formattedValue(value));")
|
||||
self.append("\n.\(key) = \(formattedValue(value));")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -979,17 +979,17 @@ extension Array: CoreStoreDebugStringConvertible {
|
||||
var string = "\(self.count) item(s) ["
|
||||
if self.isEmpty {
|
||||
|
||||
string.appendContentsOf("]")
|
||||
string.append("]")
|
||||
return string
|
||||
}
|
||||
else {
|
||||
|
||||
for (index, item) in self.enumerate() {
|
||||
for (index, item) in self.enumerated() {
|
||||
|
||||
string.appendContentsOf("\n\(index) = \(formattedValue(item));")
|
||||
string.append("\n\(index) = \(formattedValue(item));")
|
||||
}
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n]")
|
||||
string.append("\n]")
|
||||
return string
|
||||
}
|
||||
}
|
||||
@@ -1002,17 +1002,17 @@ extension Dictionary: CoreStoreDebugStringConvertible {
|
||||
var string = "\(self.count) key-value(s) ["
|
||||
if self.isEmpty {
|
||||
|
||||
string.appendContentsOf("]")
|
||||
string.append("]")
|
||||
return string
|
||||
}
|
||||
else {
|
||||
|
||||
for (key, value) in self {
|
||||
|
||||
string.appendContentsOf("\n\(formattedValue(key)) = \(formattedValue(value));")
|
||||
string.append("\n\(formattedValue(key)) = \(formattedValue(value));")
|
||||
}
|
||||
string.indent(1)
|
||||
string.appendContentsOf("\n]")
|
||||
string.append("\n]")
|
||||
return string
|
||||
}
|
||||
}
|
||||
@@ -1025,21 +1025,21 @@ extension NSAttributeDescription: CoreStoreDebugStringConvertible {
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("attributeType", self.attributeType),
|
||||
("attributeValueClassName", self.attributeValueClassName),
|
||||
("defaultValue", self.defaultValue),
|
||||
("valueTransformerName", self.valueTransformerName),
|
||||
("attributeValueClassName", self.attributeValueClassName as Any),
|
||||
("defaultValue", self.defaultValue as Any),
|
||||
("valueTransformerName", self.valueTransformerName as Any),
|
||||
("allowsExternalBinaryDataStorage", self.allowsExternalBinaryDataStorage),
|
||||
("entity.name", self.entity.name),
|
||||
("entity.name", self.entity.name as Any),
|
||||
("name", self.name),
|
||||
("optional", self.optional),
|
||||
("transient", self.transient),
|
||||
("userInfo", self.userInfo),
|
||||
("indexed", self.indexed),
|
||||
("isOptional", self.isOptional),
|
||||
("isTransient", self.isTransient),
|
||||
("userInfo", self.userInfo as Any),
|
||||
("isIndexed", self.isIndexed),
|
||||
("versionHash", self.versionHash),
|
||||
("versionHashModifier", self.versionHashModifier),
|
||||
("indexedBySpotlight", self.indexedBySpotlight),
|
||||
("storedInExternalRecord", self.storedInExternalRecord),
|
||||
("renamingIdentifier", self.renamingIdentifier)
|
||||
("versionHashModifier", self.versionHashModifier as Any),
|
||||
("isIndexedBySpotlight", self.isIndexedBySpotlight),
|
||||
("isStoredInExternalRecord", self.isStoredInExternalRecord),
|
||||
("renamingIdentifier", self.renamingIdentifier as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1050,28 +1050,28 @@ extension NSAttributeType: CoreStoreDebugStringConvertible {
|
||||
|
||||
switch self {
|
||||
|
||||
case .UndefinedAttributeType: return ".UndefinedAttributeType"
|
||||
case .Integer16AttributeType: return ".Integer16AttributeType"
|
||||
case .Integer32AttributeType: return ".Integer32AttributeType"
|
||||
case .Integer64AttributeType: return ".Integer64AttributeType"
|
||||
case .DecimalAttributeType: return ".DecimalAttributeType"
|
||||
case .DoubleAttributeType: return ".DoubleAttributeType"
|
||||
case .FloatAttributeType: return ".FloatAttributeType"
|
||||
case .StringAttributeType: return ".StringAttributeType"
|
||||
case .BooleanAttributeType: return ".BooleanAttributeType"
|
||||
case .DateAttributeType: return ".DateAttributeType"
|
||||
case .BinaryDataAttributeType: return ".BinaryDataAttributeType"
|
||||
case .TransformableAttributeType: return ".TransformableAttributeType"
|
||||
case .ObjectIDAttributeType: return ".ObjectIDAttributeType"
|
||||
case .undefinedAttributeType: return ".undefinedAttributeType"
|
||||
case .integer16AttributeType: return ".integer16AttributeType"
|
||||
case .integer32AttributeType: return ".integer32AttributeType"
|
||||
case .integer64AttributeType: return ".integer64AttributeType"
|
||||
case .decimalAttributeType: return ".decimalAttributeType"
|
||||
case .doubleAttributeType: return ".doubleAttributeType"
|
||||
case .floatAttributeType: return ".floatAttributeType"
|
||||
case .stringAttributeType: return ".stringAttributeType"
|
||||
case .booleanAttributeType: return ".booleanAttributeType"
|
||||
case .dateAttributeType: return ".dateAttributeType"
|
||||
case .binaryDataAttributeType: return ".binaryDataAttributeType"
|
||||
case .transformableAttributeType: return ".transformableAttributeType"
|
||||
case .objectIDAttributeType: return ".objectIDAttributeType"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension NSBundle: CoreStoreDebugStringConvertible {
|
||||
extension Bundle: CoreStoreDebugStringConvertible {
|
||||
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
return "\(self.bundleIdentifier.flatMap({ "\"\($0)\"" }) ?? "<unknown bundle identifier>") (\(self.bundleURL.lastPathComponent ?? "<unknown bundle URL>"))"
|
||||
return "\(self.bundleIdentifier.flatMap({ "\"\($0)\"" }) ?? "<unknown bundle identifier>") (\(self.bundleURL.lastPathComponent))"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1081,10 +1081,10 @@ extension NSDeleteRule: CoreStoreDebugStringConvertible {
|
||||
|
||||
switch self {
|
||||
|
||||
case .NoActionDeleteRule: return ".NoActionDeleteRule"
|
||||
case .NullifyDeleteRule: return ".NullifyDeleteRule"
|
||||
case .CascadeDeleteRule: return ".CascadeDeleteRule"
|
||||
case .DenyDeleteRule: return ".DenyDeleteRule"
|
||||
case .noActionDeleteRule: return ".noActionDeleteRule"
|
||||
case .nullifyDeleteRule: return ".nullifyDeleteRule"
|
||||
case .cascadeDeleteRule: return ".cascadeDeleteRule"
|
||||
case .denyDeleteRule: return ".denyDeleteRule"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1095,15 +1095,15 @@ extension NSEntityDescription: CoreStoreDebugStringConvertible {
|
||||
|
||||
var info: DumpInfo = [
|
||||
("managedObjectClassName", self.managedObjectClassName!),
|
||||
("name", self.name),
|
||||
("abstract", self.abstract),
|
||||
("superentity?.name", self.superentity?.name),
|
||||
("name", self.name as Any),
|
||||
("isAbstract", self.isAbstract),
|
||||
("superentity?.name", self.superentity?.name as Any),
|
||||
("subentities", self.subentities.map({ $0.name })),
|
||||
("properties", self.properties),
|
||||
("userInfo", self.userInfo),
|
||||
("userInfo", self.userInfo as Any),
|
||||
("versionHash", self.versionHash),
|
||||
("versionHashModifier", self.versionHashModifier),
|
||||
("renamingIdentifier", self.renamingIdentifier),
|
||||
("versionHashModifier", self.versionHashModifier as Any),
|
||||
("renamingIdentifier", self.renamingIdentifier as Any),
|
||||
("compoundIndexes", self.compoundIndexes)
|
||||
]
|
||||
if #available(iOS 9.0, OSXApplicationExtension 10.11, OSX 10.11, *) {
|
||||
@@ -1147,10 +1147,10 @@ extension NSManagedObjectID: CoreStoreDebugStringConvertible {
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
return createFormattedString(
|
||||
"\(self.URIRepresentation().coreStoreDumpString) (", ")",
|
||||
("entity.name", self.entity.name),
|
||||
("temporaryID", self.temporaryID),
|
||||
("persistentStore?.URL", self.persistentStore?.URL)
|
||||
"\(self.uriRepresentation().coreStoreDumpString) (", ")",
|
||||
("entity.name", self.entity.name as Any),
|
||||
("isTemporaryID", self.isTemporaryID as Any),
|
||||
("persistentStore?.url", self.persistentStore?.url as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1177,24 +1177,24 @@ extension NSRelationshipDescription: CoreStoreDebugStringConvertible {
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("destinationEntity?.name", self.destinationEntity?.name),
|
||||
("inverseRelationship?.name", self.inverseRelationship?.name),
|
||||
("destinationEntity?.name", self.destinationEntity?.name as Any),
|
||||
("inverseRelationship?.name", self.inverseRelationship?.name as Any),
|
||||
("minCount", self.minCount),
|
||||
("maxCount", self.maxCount),
|
||||
("deleteRule", self.deleteRule),
|
||||
("toMany", self.toMany),
|
||||
("ordered", self.ordered),
|
||||
("entity.name", self.entity.name),
|
||||
("isToMany", self.isToMany),
|
||||
("isOrdered", self.isOrdered),
|
||||
("entity.name", self.entity.name as Any),
|
||||
("name", self.name),
|
||||
("optional", self.optional),
|
||||
("transient", self.transient),
|
||||
("userInfo", self.userInfo),
|
||||
("indexed", self.indexed),
|
||||
("isOptional", self.isOptional),
|
||||
("isTransient", self.isTransient),
|
||||
("userInfo", self.userInfo as Any),
|
||||
("isIndexed", self.isIndexed),
|
||||
("versionHash", self.versionHash),
|
||||
("versionHashModifier", self.versionHashModifier),
|
||||
("indexedBySpotlight", self.indexedBySpotlight),
|
||||
("storedInExternalRecord", self.storedInExternalRecord),
|
||||
("renamingIdentifier", self.renamingIdentifier)
|
||||
("versionHashModifier", self.versionHashModifier as Any),
|
||||
("isIndexedBySpotlight", self.isIndexedBySpotlight),
|
||||
("isStoredInExternalRecord", self.isStoredInExternalRecord),
|
||||
("renamingIdentifier", self.renamingIdentifier as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1205,14 +1205,14 @@ extension NSSortDescriptor: CoreStoreDebugStringConvertible {
|
||||
|
||||
return createFormattedString(
|
||||
"(", ")",
|
||||
("key", self.key),
|
||||
("key", self.key as Any),
|
||||
("ascending", self.ascending),
|
||||
("selector", self.selector)
|
||||
("selector", self.selector as Any)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension NSURL: CoreStoreDebugStringConvertible {
|
||||
extension URL: CoreStoreDebugStringConvertible {
|
||||
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
@@ -1236,7 +1236,7 @@ extension Selector: CoreStoreDebugStringConvertible {
|
||||
|
||||
public var coreStoreDumpString: String {
|
||||
|
||||
return self == nil ? "nil" : "\"\(self)\""
|
||||
return "\"\(self)\""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public extension CoreStore {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static func log(level: LogLevel, message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
internal static func log(_ level: LogLevel, message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
|
||||
self.logger.log(
|
||||
level: level,
|
||||
@@ -49,7 +49,7 @@ public extension CoreStore {
|
||||
)
|
||||
}
|
||||
|
||||
internal static func log(error: CoreStoreError, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
internal static func log(_ error: CoreStoreError, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
|
||||
self.logger.log(
|
||||
error: error,
|
||||
@@ -60,7 +60,7 @@ public extension CoreStore {
|
||||
)
|
||||
}
|
||||
|
||||
internal static func assert(@autoclosure condition: () -> Bool, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
internal static func assert( _ condition: @autoclosure () -> Bool, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
|
||||
self.logger.assert(
|
||||
condition,
|
||||
@@ -71,8 +71,7 @@ public extension CoreStore {
|
||||
)
|
||||
}
|
||||
|
||||
@noreturn
|
||||
internal static func abort(message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
|
||||
internal static func abort(_ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) -> Never {
|
||||
|
||||
self.logger.abort(
|
||||
message,
|
||||
|
||||
@@ -33,17 +33,17 @@ import Foundation
|
||||
*/
|
||||
public enum LogLevel {
|
||||
|
||||
case Trace
|
||||
case Notice
|
||||
case Warning
|
||||
case Fatal
|
||||
case trace
|
||||
case notice
|
||||
case warning
|
||||
case fatal
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CoreStoreLogger
|
||||
|
||||
/**
|
||||
Custom loggers should implement the `CoreStoreLogger` protocol and pass its instance to `CoreStore.logger`. Calls to `log(...)`, `handleError(...)`, and `assert(...)` are not tied to a specific queue/thread, so it is the implementer's job to handle thread-safety.
|
||||
Custom loggers should implement the `CoreStoreLogger` protocol and pass its instance to `CoreStore.logger`. Calls to `log(...)`, `assert(...)`, and `abort(...)` are not tied to a specific queue/thread, so it is the implementer's job to handle thread-safety.
|
||||
*/
|
||||
public protocol CoreStoreLogger {
|
||||
|
||||
@@ -56,7 +56,7 @@ public protocol CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
func log(level level: LogLevel, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
func log(level: LogLevel, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
|
||||
/**
|
||||
Handles errors sent by the `CoreStore` framework.
|
||||
@@ -67,7 +67,7 @@ public protocol CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
func log(error error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
func log(error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
|
||||
/**
|
||||
Handles assertions made throughout the `CoreStore` framework.
|
||||
@@ -78,41 +78,23 @@ public protocol CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
func assert(@autoclosure condition: () -> Bool, @autoclosure message: () -> String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
func assert(_ condition: @autoclosure () -> Bool, message: @autoclosure () -> String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
|
||||
/**
|
||||
Handles fatal errors made throughout the `CoreStore` framework. The app wil terminate after this method is called.
|
||||
- Important: Implementers may guarantee that the function doesn't return, either by calling another `@noreturn` function such as `fatalError()` or `abort()`, or by raising an exception. If the implementation does not terminate the app, CoreStore will call an internal `fatalError()` to do so.
|
||||
- Important: Implementers may guarantee that the function doesn't return, either by calling another `Never` function such as `fatalError()` or `abort()`, or by raising an exception. If the implementation does not terminate the app, CoreStore will call an internal `fatalError()` to do so.
|
||||
|
||||
- parameter message: the fatal error message
|
||||
- parameter fileName: the source file name
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
func abort(message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `log(error:message:fileName:lineNumber:functionName:)` instead.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use log(error:message:fileName:lineNumber:functionName:) instead.")
|
||||
func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
func abort(_ message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
|
||||
}
|
||||
|
||||
extension CoreStoreLogger {
|
||||
|
||||
/**
|
||||
Deprecated. Use `log(error:message:fileName:lineNumber:functionName:)` instead.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use log(error:message:fileName:lineNumber:functionName:) instead.")
|
||||
public func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
self.log(error: error.bridgeToSwift, message: message, fileName: fileName, lineNumber: lineNumber, functionName: functionName)
|
||||
}
|
||||
|
||||
public func abort(message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
public func abort(_ message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
Swift.fatalError(message, file: fileName, line: UInt(lineNumber))
|
||||
}
|
||||
|
||||
@@ -30,10 +30,6 @@ import Foundation
|
||||
|
||||
/**
|
||||
The `DefaultLogger` is a basic implementation of the `CoreStoreLogger` protocol.
|
||||
|
||||
- The `log(...)` method calls `print(...)` to print the level, source file name, line number, function name, and the log message.
|
||||
- The `handleError(...)` method calls `print(...)` to print the source file name, line number, function name, and the error message.
|
||||
- The `assert(...)` method calls `assert(...)` on the arguments.
|
||||
*/
|
||||
public final class DefaultLogger: CoreStoreLogger {
|
||||
|
||||
@@ -51,30 +47,30 @@ public final class DefaultLogger: CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
public func log(level level: LogLevel, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
public func log(level: LogLevel, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
#if DEBUG
|
||||
let icon: String
|
||||
let levelString: String
|
||||
switch level {
|
||||
|
||||
case .Trace:
|
||||
case .trace:
|
||||
icon = "🔹"
|
||||
levelString = "Trace"
|
||||
|
||||
case .Notice:
|
||||
case .notice:
|
||||
icon = "🔸"
|
||||
levelString = "Notice"
|
||||
|
||||
case .Warning:
|
||||
case .warning:
|
||||
icon = "⚠️"
|
||||
levelString = "Warning"
|
||||
|
||||
case .Fatal:
|
||||
case .fatal:
|
||||
icon = "❗"
|
||||
levelString = "Fatal"
|
||||
}
|
||||
Swift.print("\(icon) [CoreStore: \(levelString)] \((fileName.stringValue as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n")
|
||||
Swift.print("\(icon) [CoreStore: \(levelString)] \((String(describing: fileName) as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n")
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -87,10 +83,10 @@ public final class DefaultLogger: CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
public func log(error error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
public func log(error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
#if DEBUG
|
||||
Swift.print("⚠️ [CoreStore: Error] \((fileName.stringValue as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n \(error)\n")
|
||||
Swift.print("⚠️ [CoreStore: Error] \((String(describing: fileName) as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n \(error)\n")
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -103,30 +99,30 @@ public final class DefaultLogger: CoreStoreLogger {
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
public func assert(@autoclosure condition: () -> Bool, @autoclosure message: () -> String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
public func assert(_ condition: @autoclosure () -> Bool, message: @autoclosure () -> String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
#if DEBUG
|
||||
if condition() {
|
||||
|
||||
return
|
||||
}
|
||||
Swift.print("❗ [CoreStore: Assertion Failure] \((fileName.stringValue as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message())\n")
|
||||
Swift.print("❗ [CoreStore: Assertion Failure] \((String(describing: fileName) as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message())\n")
|
||||
Swift.fatalError(file: fileName, line: UInt(lineNumber))
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
Handles fatal errors made throughout the `CoreStore` framework.
|
||||
- Important: This method should be marked `@noreturn` and implementers should guarantee that the function doesn't, either by calling another `@noreturn` function such as `fatalError()` or `abort()`, or by raising an exception.
|
||||
- Important: Implementers should guarantee that this function doesn't return, either by calling another `Never` function such as `fatalError()` or `abort()`, or by raising an exception.
|
||||
|
||||
- parameter message: the fatal error message
|
||||
- parameter fileName: the source file name
|
||||
- parameter lineNumber: the source line number
|
||||
- parameter functionName: the source function name
|
||||
*/
|
||||
public func abort(message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
public func abort(_ message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
|
||||
|
||||
Swift.print("❗ [CoreStore: Fatal Error] \((fileName.stringValue as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n")
|
||||
Swift.print("❗ [CoreStore: Fatal Error] \((String(describing: fileName) as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n")
|
||||
Swift.fatalError(file: fileName, line: UInt(lineNumber))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - CoreStore
|
||||
@@ -41,17 +38,16 @@ public extension CoreStore {
|
||||
InMemoryStore.self,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storeType: the storage type
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorage<T: StorageInterface where T: DefaultInitializableStore>(storeType: T.Type, completion: (SetupResult<T>) -> Void) {
|
||||
public static func addStorage<T: StorageInterface>(_ storeType: T.Type, completion: @escaping (SetupResult<T>) -> Void) where T: DefaultInitializableStore {
|
||||
|
||||
self.defaultStack.addStorage(storeType.init(), completion: completion)
|
||||
}
|
||||
@@ -63,17 +59,16 @@ public extension CoreStore {
|
||||
InMemoryStore(configuration: "Config1"),
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorage<T: StorageInterface>(storage: T, completion: (SetupResult<T>) -> Void) {
|
||||
public static func addStorage<T: StorageInterface>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) {
|
||||
|
||||
self.defaultStack.addStorage(storage, completion: completion)
|
||||
}
|
||||
@@ -85,18 +80,17 @@ public extension CoreStore {
|
||||
SQLiteStore.self,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storeType: the local storage type
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
*/
|
||||
public static func addStorage<T: LocalStorage where T: DefaultInitializableStore>(storeType: T.Type, completion: (SetupResult<T>) -> Void) -> NSProgress? {
|
||||
public static func addStorage<T: LocalStorage>(_ storeType: T.Type, completion: @escaping (SetupResult<T>) -> Void) -> Progress? where T: DefaultInitializableStore {
|
||||
|
||||
return self.defaultStack.addStorage(storeType.init(), completion: completion)
|
||||
}
|
||||
@@ -108,18 +102,17 @@ public extension CoreStore {
|
||||
SQLiteStore(fileName: "core_data.sqlite", configuration: "Config1"),
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
*/
|
||||
public static func addStorage<T: LocalStorage>(storage: T, completion: (SetupResult<T>) -> Void) -> NSProgress? {
|
||||
public static func addStorage<T: LocalStorage>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) -> Progress? {
|
||||
|
||||
return self.defaultStack.addStorage(storage, completion: completion)
|
||||
}
|
||||
@@ -133,7 +126,7 @@ public extension CoreStore {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
@@ -142,17 +135,16 @@ public extension CoreStore {
|
||||
storage,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the cloud storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `CloudStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `CloudStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorage<T: CloudStorage>(storage: T, completion: (SetupResult<T>) -> Void) {
|
||||
public static func addStorage<T: CloudStorage>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) {
|
||||
|
||||
self.defaultStack.addStorage(storage, completion: completion)
|
||||
}
|
||||
@@ -161,11 +153,11 @@ public extension CoreStore {
|
||||
Migrates a local storage to match the `defaultStack`'s managed object model version. This method does NOT add the migrated store to the data stack.
|
||||
|
||||
- parameter storage: the local storage
|
||||
- parameter completion: the closure to be executed on the main queue when the migration completes, either due to success or failure. The closure's `MigrationResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a `.Failure` result if an error occurs asynchronously.
|
||||
- parameter completion: the closure to be executed on the main queue when the migration completes, either due to success or failure. The closure's `MigrationResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a `.failure` result if an error occurs asynchronously.
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` is no migrations are required
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` is no migrations are required
|
||||
*/
|
||||
public static func upgradeStorageIfNeeded<T: LocalStorage>(storage: T, completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
public static func upgradeStorageIfNeeded<T: LocalStorage>(_ storage: T, completion: @escaping (MigrationResult) -> Void) throws -> Progress? {
|
||||
|
||||
return try self.defaultStack.upgradeStorageIfNeeded(storage, completion: completion)
|
||||
}
|
||||
@@ -177,110 +169,8 @@ public extension CoreStore {
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: a `MigrationType` array indicating the migration steps required for the store, or an empty array if the file does not exist yet. Otherwise, an error is thrown if either inspection of the store failed, or if no mapping model was found/inferred.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func requiredMigrationsForStorage<T: LocalStorage>(storage: T) throws -> [MigrationType] {
|
||||
public static func requiredMigrationsForStorage<T: LocalStorage>(_ storage: T) throws -> [MigrationType] {
|
||||
|
||||
return try self.defaultStack.requiredMigrationsForStorage(storage)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `addSQLiteStore(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addSQLiteStore(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func addSQLiteStore(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle]? = nil, resetStoreOnModelMismatch: Bool = false, completion: (PersistentStoreResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.defaultStack.addSQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles,
|
||||
resetStoreOnModelMismatch: resetStoreOnModelMismatch,
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addSQLiteStore(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addSQLiteStore(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func addSQLiteStore(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle]? = NSBundle.allBundles(), resetStoreOnModelMismatch: Bool = false, completion: (PersistentStoreResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.defaultStack.addSQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles,
|
||||
resetStoreOnModelMismatch: resetStoreOnModelMismatch,
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `upgradeStorageIfNeeded(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use upgradeStorageIfNeeded(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func upgradeSQLiteStoreIfNeeded(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle]? = nil, completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.defaultStack.upgradeSQLiteStoreIfNeeded(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `upgradeStorageIfNeeded(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use upgradeStorageIfNeeded(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func upgradeSQLiteStoreIfNeeded(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle]? = nil, completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.defaultStack.upgradeSQLiteStoreIfNeeded(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `requiredMigrationsForStorage(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use requiredMigrationsForStorage(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
@warn_unused_result
|
||||
public static func requiredMigrationsForSQLiteStore(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as [NSBundle]) throws -> [MigrationType] {
|
||||
|
||||
return try self.defaultStack.requiredMigrationsForSQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `requiredMigrationsForStorage(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use requiredMigrationsForStorage(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
@warn_unused_result
|
||||
public static func requiredMigrationsForSQLiteStore(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as [NSBundle]) throws -> [MigrationType] {
|
||||
|
||||
return try self.defaultStack.requiredMigrationsForSQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - DataStack
|
||||
@@ -41,17 +38,16 @@ public extension DataStack {
|
||||
InMemoryStore.self,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storeType: the storage type
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorage<T: StorageInterface where T: DefaultInitializableStore>(storeType: T.Type, completion: (SetupResult<T>) -> Void) {
|
||||
public func addStorage<T: StorageInterface>(_ storeType: T.Type, completion: @escaping (SetupResult<T>) -> Void) where T: DefaultInitializableStore {
|
||||
|
||||
self.addStorage(storeType.init(), completion: completion)
|
||||
}
|
||||
@@ -63,23 +59,22 @@ public extension DataStack {
|
||||
InMemoryStore(configuration: "Config1"),
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `StorageInterface` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorage<T: StorageInterface>(storage: T, completion: (SetupResult<T>) -> Void) {
|
||||
public func addStorage<T: StorageInterface>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) {
|
||||
|
||||
self.coordinator.performAsynchronously {
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
@@ -88,13 +83,13 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: nil,
|
||||
finalStoreOptions: storage.storeOptions
|
||||
)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
@@ -106,7 +101,7 @@ public extension DataStack {
|
||||
storeError,
|
||||
"Failed to add \(cs_typeName(storage)) to the stack."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storeError))
|
||||
}
|
||||
@@ -121,20 +116,19 @@ public extension DataStack {
|
||||
SQLiteStore.self,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storeType: the local storage type
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
*/
|
||||
public func addStorage<T: LocalStorage where T: DefaultInitializableStore>(storeType: T.Type, completion: (SetupResult<T>) -> Void) -> NSProgress? {
|
||||
public func addStorage<T: LocalStorage>(_ storeType: T.Type, completion: @escaping (SetupResult<T>) -> Void) -> Progress? where T: DefaultInitializableStore {
|
||||
|
||||
return self.addStorage(storeType.init(), completion: completion)
|
||||
return self.addStorage(storeType.init() as! T.Type, completion: completion)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,22 +138,21 @@ public extension DataStack {
|
||||
SQLiteStore(fileName: "core_data.sqlite", configuration: "Config1"),
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `LocalStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` if either no migrations are required or if a failure occured.
|
||||
*/
|
||||
public func addStorage<T: LocalStorage>(storage: T, completion: (SetupResult<T>) -> Void) -> NSProgress? {
|
||||
public func addStorage<T: LocalStorage>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) -> Progress? {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
CoreStore.assert(
|
||||
fileURL.fileURL,
|
||||
fileURL.isFileURL,
|
||||
"The specified URL for the \(cs_typeName(storage)) is invalid: \"\(fileURL)\""
|
||||
)
|
||||
|
||||
@@ -167,31 +160,31 @@ public extension DataStack {
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(fileURL) {
|
||||
if let persistentStore = self.coordinator.persistentStore(for: fileURL as URL) {
|
||||
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
if let existingStorage = persistentStore.storageInterface as? T,
|
||||
storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(existingStorage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
|
||||
let error = CoreStoreError.differentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(fileURL)\" because a different \(cs_typeName(NSPersistentStore)) at that URL already exists."
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(fileURL)\" because a different \(cs_typeName(NSPersistentStore.self)) at that URL already exists."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(error))
|
||||
}
|
||||
@@ -200,15 +193,15 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
try FileManager.default.createDirectory(
|
||||
at: fileURL.deletingLastPathComponent(),
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: fileURL as URL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
|
||||
@@ -217,16 +210,19 @@ public extension DataStack {
|
||||
metadata: metadata,
|
||||
completion: { (result) -> Void in
|
||||
|
||||
if case .Failure(.InternalError(let error)) = result {
|
||||
if case .failure(.internalError(let error)) = result {
|
||||
|
||||
if storage.localStorageOptions.contains(.RecreateStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
if storage.localStorageOptions.contains(.recreateStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
|
||||
do {
|
||||
|
||||
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
|
||||
try self.addStorageAndWait(storage)
|
||||
try storage.eraseStorageAndWait(
|
||||
metadata: metadata,
|
||||
soureModelHint: self.model[metadata]
|
||||
)
|
||||
_ = try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
@@ -244,9 +240,12 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
_ = try self.addStorageAndWait(storage)
|
||||
|
||||
completion(SetupResult(storage))
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
@@ -260,16 +259,16 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
_ = try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(error))
|
||||
}
|
||||
@@ -281,9 +280,9 @@ public extension DataStack {
|
||||
let storeError = CoreStoreError(error)
|
||||
CoreStore.log(
|
||||
storeError,
|
||||
"Failed to load SQLite \(cs_typeName(NSPersistentStore)) metadata."
|
||||
"Failed to load SQLite \(cs_typeName(NSPersistentStore.self)) metadata."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storeError))
|
||||
}
|
||||
@@ -301,7 +300,7 @@ public extension DataStack {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
@@ -310,48 +309,47 @@ public extension DataStack {
|
||||
storage,
|
||||
completion: { result in
|
||||
switch result {
|
||||
case .Success(let storage): // ...
|
||||
case .Failure(let error): // ...
|
||||
case .success(let storage): // ...
|
||||
case .failure(let error): // ...
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter storage: the cloud storage
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `CloudStorage` associated to the `SetupResult.Success` may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `SetupResult` argument indicates the result. Note that the `CloudStorage` associated to the `SetupResult.success` may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorage<T: CloudStorage>(storage: T, completion: (SetupResult<T>) -> Void) {
|
||||
public func addStorage<T: CloudStorage>(_ storage: T, completion: @escaping (SetupResult<T>) -> Void) {
|
||||
|
||||
let cacheFileURL = storage.cacheFileURL
|
||||
self.coordinator.performSynchronously {
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(cacheFileURL) {
|
||||
if let persistentStore = self.coordinator.persistentStore(for: cacheFileURL as URL) {
|
||||
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
if let existingStorage = persistentStore.storageInterface as? T,
|
||||
storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(existingStorage))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: cacheFileURL)
|
||||
let error = CoreStoreError.differentStorageExistsAtURL(existingPersistentStoreURL: cacheFileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(cacheFileURL)\" because a different \(cs_typeName(NSPersistentStore)) at that URL already exists."
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(cacheFileURL)\" because a different \(cs_typeName(NSPersistentStore.self)) at that URL already exists."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(error))
|
||||
}
|
||||
@@ -361,39 +359,34 @@ public extension DataStack {
|
||||
do {
|
||||
|
||||
var cloudStorageOptions = storage.cloudStorageOptions
|
||||
cloudStorageOptions.remove(.RecreateLocalStoreOnModelMismatch)
|
||||
cloudStorageOptions.remove(.recreateLocalStoreOnModelMismatch)
|
||||
|
||||
let storeOptions = storage.storeOptionsForOptions(cloudStorageOptions)
|
||||
let storeOptions = storage.dictionary(forOptions: cloudStorageOptions)
|
||||
do {
|
||||
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
cacheFileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: cacheFileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch let error as NSError where storage.cloudStorageOptions.contains(.RecreateLocalStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
catch let error as NSError where storage.cloudStorageOptions.contains(.recreateLocalStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: cacheFileURL,
|
||||
let finalStoreOptions = storage.dictionary(forOptions: storage.cloudStorageOptions)
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: cacheFileURL,
|
||||
options: storeOptions
|
||||
)
|
||||
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
|
||||
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.model[metadata].flatMap(storage.eraseStorageAndWait)
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: cacheFileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
finalStoreOptions: finalStoreOptions
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -402,16 +395,16 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
_ = try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(error))
|
||||
}
|
||||
@@ -422,9 +415,9 @@ public extension DataStack {
|
||||
let storeError = CoreStoreError(error)
|
||||
CoreStore.log(
|
||||
storeError,
|
||||
"Failed to load \(cs_typeName(NSPersistentStore)) metadata."
|
||||
"Failed to load \(cs_typeName(NSPersistentStore.self)) metadata."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(SetupResult(storeError))
|
||||
}
|
||||
@@ -438,9 +431,9 @@ public extension DataStack {
|
||||
- parameter storage: the local storage
|
||||
- parameter completion: the closure to be executed on the main queue when the migration completes, either due to success or failure. The closure's `MigrationResult` argument indicates the result.
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: an `NSProgress` instance if a migration has started, or `nil` is no migrations are required
|
||||
- returns: a `Progress` instance if a migration has started, or `nil` is no migrations are required
|
||||
*/
|
||||
public func upgradeStorageIfNeeded<T: LocalStorage>(storage: T, completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
public func upgradeStorageIfNeeded<T: LocalStorage>(_ storage: T, completion: @escaping (MigrationResult) -> Void) throws -> Progress? {
|
||||
|
||||
return try self.coordinator.performSynchronously {
|
||||
|
||||
@@ -452,9 +445,9 @@ public extension DataStack {
|
||||
"Attempted to migrate an already added \(cs_typeName(storage)) at URL \"\(fileURL)\""
|
||||
)
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: fileURL as URL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
return self.upgradeStorageIfNeeded(
|
||||
@@ -482,8 +475,7 @@ public extension DataStack {
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: a `MigrationType` array indicating the migration steps required for the store, or an empty array if the file does not exist yet. Otherwise, an error is thrown if either inspection of the store failed, or if no mapping model was found/inferred.
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func requiredMigrationsForStorage<T: LocalStorage>(storage: T) throws -> [MigrationType] {
|
||||
public func requiredMigrationsForStorage<T: LocalStorage>(_ storage: T) throws -> [MigrationType] {
|
||||
|
||||
return try self.coordinator.performSynchronously {
|
||||
|
||||
@@ -495,15 +487,15 @@ public extension DataStack {
|
||||
)
|
||||
do {
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: fileURL as URL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
|
||||
guard let migrationSteps = self.computeMigrationFromStorage(storage, metadata: metadata) else {
|
||||
|
||||
let error = CoreStoreError.MappingModelNotFound(
|
||||
let error = CoreStoreError.mappingModelNotFound(
|
||||
localStoreURL: fileURL,
|
||||
targetModel: self.model,
|
||||
targetModelVersion: self.modelVersion
|
||||
@@ -515,9 +507,9 @@ public extension DataStack {
|
||||
throw error
|
||||
}
|
||||
|
||||
if migrationSteps.count > 1 && storage.localStorageOptions.contains(.PreventProgressiveMigration) {
|
||||
if migrationSteps.count > 1 && storage.localStorageOptions.contains(.preventProgressiveMigration) {
|
||||
|
||||
let error = CoreStoreError.ProgressiveMigrationRequired(localStoreURL: fileURL)
|
||||
let error = CoreStoreError.progressiveMigrationRequired(localStoreURL: fileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to find migration mapping from the \(cs_typeName(storage)) at URL \"\(fileURL)\" to version model \"\(self.modelVersion)\" without requiring progessive migrations."
|
||||
@@ -547,11 +539,11 @@ public extension DataStack {
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private func upgradeStorageIfNeeded<T: LocalStorage>(storage: T, metadata: [String: AnyObject], completion: (MigrationResult) -> Void) -> NSProgress? {
|
||||
private func upgradeStorageIfNeeded<T: LocalStorage>(_ storage: T, metadata: [String: Any], completion: @escaping (MigrationResult) -> Void) -> Progress? {
|
||||
|
||||
guard let migrationSteps = self.computeMigrationFromStorage(storage, metadata: metadata) else {
|
||||
|
||||
let error = CoreStoreError.MappingModelNotFound(
|
||||
let error = CoreStoreError.mappingModelNotFound(
|
||||
localStoreURL: storage.fileURL,
|
||||
targetModel: self.model,
|
||||
targetModelVersion: self.modelVersion
|
||||
@@ -561,7 +553,7 @@ public extension DataStack {
|
||||
"Failed to find migration steps from \(cs_typeName(storage)) at URL \"\(storage.fileURL)\" to version model \"\(self.model)\"."
|
||||
)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(MigrationResult(error))
|
||||
}
|
||||
@@ -571,21 +563,21 @@ public extension DataStack {
|
||||
let numberOfMigrations: Int64 = Int64(migrationSteps.count)
|
||||
if numberOfMigrations == 0 {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(MigrationResult([]))
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
else if numberOfMigrations > 1 && storage.localStorageOptions.contains(.PreventProgressiveMigration) {
|
||||
else if numberOfMigrations > 1 && storage.localStorageOptions.contains(.preventProgressiveMigration) {
|
||||
|
||||
let error = CoreStoreError.ProgressiveMigrationRequired(localStoreURL: storage.fileURL)
|
||||
let error = CoreStoreError.progressiveMigrationRequired(localStoreURL: storage.fileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to find migration mapping from the \(cs_typeName(storage)) at URL \"\(storage.fileURL)\" to version model \"\(self.modelVersion)\" without requiring progessive migrations."
|
||||
)
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
completion(MigrationResult(error))
|
||||
}
|
||||
@@ -594,28 +586,28 @@ public extension DataStack {
|
||||
|
||||
let migrationTypes = migrationSteps.map { $0.migrationType }
|
||||
var migrationResult: MigrationResult?
|
||||
var operations = [NSOperation]()
|
||||
var operations = [Operation]()
|
||||
var cancelled = false
|
||||
|
||||
let progress = NSProgress(parent: nil, userInfo: nil)
|
||||
let progress = Progress(parent: nil, userInfo: nil)
|
||||
progress.totalUnitCount = numberOfMigrations
|
||||
|
||||
for (sourceModel, destinationModel, mappingModel, _) in migrationSteps {
|
||||
|
||||
progress.becomeCurrentWithPendingUnitCount(1)
|
||||
progress.becomeCurrent(withPendingUnitCount: 1)
|
||||
|
||||
let childProgress = NSProgress(parent: progress, userInfo: nil)
|
||||
let childProgress = Progress(parent: progress, userInfo: nil)
|
||||
childProgress.totalUnitCount = 100
|
||||
|
||||
operations.append(
|
||||
NSBlockOperation { [weak self] in
|
||||
BlockOperation { [weak self] in
|
||||
|
||||
guard let `self` = self where !cancelled else {
|
||||
guard let `self` = self, !cancelled else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cs_autoreleasepool {
|
||||
autoreleasepool {
|
||||
|
||||
do {
|
||||
|
||||
@@ -634,9 +626,9 @@ public extension DataStack {
|
||||
}
|
||||
}
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
_ = withExtendedLifetime(childProgress) { (_: NSProgress) -> Void in }
|
||||
_ = withExtendedLifetime(childProgress) { (_: Progress) -> Void in }
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -644,21 +636,12 @@ public extension DataStack {
|
||||
progress.resignCurrent()
|
||||
}
|
||||
|
||||
let migrationOperation = NSBlockOperation()
|
||||
#if USE_FRAMEWORKS
|
||||
|
||||
migrationOperation.qualityOfService = .Utility
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
|
||||
migrationOperation.qualityOfService = .Utility
|
||||
}
|
||||
#endif
|
||||
let migrationOperation = BlockOperation()
|
||||
migrationOperation.qualityOfService = .utility
|
||||
operations.forEach { migrationOperation.addDependency($0) }
|
||||
migrationOperation.addExecutionBlock { () -> Void in
|
||||
|
||||
GCDQueue.Main.async {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
progress.setProgressHandler(nil)
|
||||
completion(migrationResult ?? MigrationResult(migrationTypes))
|
||||
@@ -673,10 +656,10 @@ public extension DataStack {
|
||||
return progress
|
||||
}
|
||||
|
||||
private func computeMigrationFromStorage<T: LocalStorage>(storage: T, metadata: [String: AnyObject]) -> [(sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, migrationType: MigrationType)]? {
|
||||
private func computeMigrationFromStorage<T: LocalStorage>(_ storage: T, metadata: [String: Any]) -> [(sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, migrationType: MigrationType)]? {
|
||||
|
||||
let model = self.model
|
||||
if model.isConfiguration(storage.configuration, compatibleWithStoreMetadata: metadata) {
|
||||
if model.isConfiguration(withName: storage.configuration, compatibleWithStoreMetadata: metadata) {
|
||||
|
||||
return []
|
||||
}
|
||||
@@ -695,10 +678,11 @@ public extension DataStack {
|
||||
|
||||
while let nextVersion = migrationChain.nextVersionFrom(currentVersion),
|
||||
let sourceModel = model[currentVersion],
|
||||
let destinationModel = model[nextVersion] where sourceModel != model {
|
||||
sourceModel != model,
|
||||
let destinationModel = model[nextVersion] {
|
||||
|
||||
if let mappingModel = NSMappingModel(
|
||||
fromBundles: storage.mappingModelBundles,
|
||||
from: storage.mappingModelBundles,
|
||||
forSourceModel: sourceModel,
|
||||
destinationModel: destinationModel) {
|
||||
|
||||
@@ -707,7 +691,7 @@ public extension DataStack {
|
||||
sourceModel: sourceModel,
|
||||
destinationModel: destinationModel,
|
||||
mappingModel: mappingModel,
|
||||
migrationType: .Heavyweight(
|
||||
migrationType: .heavyweight(
|
||||
sourceVersion: currentVersion,
|
||||
destinationVersion: nextVersion
|
||||
)
|
||||
@@ -718,8 +702,8 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
let mappingModel = try NSMappingModel.inferredMappingModelForSourceModel(
|
||||
sourceModel,
|
||||
let mappingModel = try NSMappingModel.inferredMappingModel(
|
||||
forSourceModel: sourceModel,
|
||||
destinationModel: destinationModel
|
||||
)
|
||||
|
||||
@@ -728,7 +712,7 @@ public extension DataStack {
|
||||
sourceModel: sourceModel,
|
||||
destinationModel: destinationModel,
|
||||
mappingModel: mappingModel,
|
||||
migrationType: .Lightweight(
|
||||
migrationType: .lightweight(
|
||||
sourceVersion: currentVersion,
|
||||
destinationVersion: nextVersion
|
||||
)
|
||||
@@ -751,24 +735,25 @@ public extension DataStack {
|
||||
return nil
|
||||
}
|
||||
|
||||
private func startMigrationForStorage<T: LocalStorage>(storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, progress: NSProgress) throws {
|
||||
private func startMigrationForStorage<T: LocalStorage>(_ storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, progress: Progress) throws {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
|
||||
let temporaryDirectoryURL = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier ?? "com.CoreStore.DataStack")!
|
||||
.URLByAppendingPathComponent(NSProcessInfo().globallyUniqueString)
|
||||
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
.appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack")
|
||||
.appendingPathComponent(ProcessInfo().globallyUniqueString)
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
try! fileManager.createDirectoryAtURL(
|
||||
temporaryDirectoryURL!,
|
||||
let fileManager = FileManager.default
|
||||
try! fileManager.createDirectory(
|
||||
at: temporaryDirectoryURL,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let temporaryFileURL = temporaryDirectoryURL!.URLByAppendingPathComponent(
|
||||
fileURL.lastPathComponent!,
|
||||
isDirectory: false)!
|
||||
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(
|
||||
fileURL.lastPathComponent,
|
||||
isDirectory: false
|
||||
)
|
||||
|
||||
let migrationManager = MigrationManager(
|
||||
sourceModel: sourceModel,
|
||||
@@ -778,13 +763,13 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try migrationManager.migrateStoreFromURL(
|
||||
fileURL,
|
||||
type: storage.dynamicType.storeType,
|
||||
try migrationManager.migrateStore(
|
||||
from: fileURL,
|
||||
sourceType: type(of: storage).storeType,
|
||||
options: nil,
|
||||
withMappingModel: mappingModel,
|
||||
with: mappingModel,
|
||||
toDestinationURL: temporaryFileURL,
|
||||
destinationType: storage.dynamicType.storeType,
|
||||
destinationType: type(of: storage).storeType,
|
||||
destinationOptions: nil
|
||||
)
|
||||
}
|
||||
@@ -792,7 +777,7 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.removeItemAtURL(temporaryFileURL)
|
||||
try fileManager.removeItem(at: temporaryFileURL)
|
||||
}
|
||||
catch _ { }
|
||||
|
||||
@@ -809,9 +794,9 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.replaceItemAtURL(
|
||||
fileURL,
|
||||
withItemAtURL: temporaryFileURL,
|
||||
try fileManager.replaceItem(
|
||||
at: fileURL as URL,
|
||||
withItemAt: temporaryFileURL,
|
||||
backupItemName: nil,
|
||||
options: [],
|
||||
resultingItemURL: nil
|
||||
@@ -823,7 +808,7 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.removeItemAtURL(temporaryFileURL)
|
||||
try fileManager.removeItem(at: temporaryFileURL)
|
||||
}
|
||||
catch _ { }
|
||||
|
||||
@@ -838,159 +823,4 @@ public extension DataStack {
|
||||
throw fileError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorage(_:completion:)` by passing a `InMemoryStore` instance.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorage(_:completion:) by passing a InMemoryStore instance.")
|
||||
public func addInMemoryStore(configuration configuration: String? = nil, completion: (PersistentStoreResult) -> Void) {
|
||||
|
||||
self.addStorage(
|
||||
InMemoryStore(configuration: configuration),
|
||||
completion: { result in
|
||||
|
||||
switch result {
|
||||
|
||||
case .Success(let storage):
|
||||
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
|
||||
|
||||
case .Failure(let error):
|
||||
completion(PersistentStoreResult(error as NSError))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorage(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorage(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func addSQLiteStore(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle]? = nil, resetStoreOnModelMismatch: Bool = false, completion: (PersistentStoreResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return self.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
localStorageOptions: resetStoreOnModelMismatch ? .RecreateStoreOnModelMismatch : .None
|
||||
),
|
||||
completion: { result in
|
||||
|
||||
switch result {
|
||||
|
||||
case .Success(let storage):
|
||||
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
|
||||
|
||||
case .Failure(let error):
|
||||
completion(PersistentStoreResult(error as NSError))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addSQLiteStore(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addSQLiteStore(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func addSQLiteStore(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle]? = NSBundle.allBundles(), resetStoreOnModelMismatch: Bool = false, completion: (PersistentStoreResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return self.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
localStorageOptions: resetStoreOnModelMismatch ? .RecreateStoreOnModelMismatch : .None
|
||||
),
|
||||
completion: { result in
|
||||
|
||||
switch result {
|
||||
|
||||
case .Success(let storage):
|
||||
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
|
||||
|
||||
case .Failure(let error):
|
||||
completion(PersistentStoreResult(error as NSError))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `upgradeStorageIfNeeded(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use upgradeStorageIfNeeded(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func upgradeSQLiteStoreIfNeeded(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.upgradeStorageIfNeeded(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
),
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `upgradeStorageIfNeeded(_:completion:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use upgradeStorageIfNeeded(_:completion:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func upgradeSQLiteStoreIfNeeded(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
return try self.upgradeStorageIfNeeded(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
),
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `requiredMigrationsForStorage(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use requiredMigrationsForStorage(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
@warn_unused_result
|
||||
public func requiredMigrationsForSQLiteStore(fileName fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as [NSBundle]) throws -> [MigrationType] {
|
||||
|
||||
return try self.requiredMigrationsForStorage(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `requiredMigrationsForStorage(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use requiredMigrationsForStorage(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
@warn_unused_result
|
||||
public func requiredMigrationsForSQLiteStore(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles() as [NSBundle]) throws -> [MigrationType] {
|
||||
|
||||
return try self.requiredMigrationsForStorage(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ import CoreData
|
||||
- a version appears twice as a key in a dictionary literal
|
||||
- a loop is found in any of the paths
|
||||
*/
|
||||
public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, DictionaryLiteralConvertible, ArrayLiteralConvertible, Equatable {
|
||||
public struct MigrationChain: ExpressibleByNilLiteral, ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral, ExpressibleByArrayLiteral, Equatable {
|
||||
|
||||
/**
|
||||
Initializes the `MigrationChain` with empty values, which instructs the `DataStack` to use the .xcdatamodel's current version as the final version, and to disable progressive migrations.
|
||||
@@ -87,9 +87,9 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
/**
|
||||
Initializes the `MigrationChain` with a linear order of versions, which becomes the order of the `DataStack`'s progressive migrations.
|
||||
*/
|
||||
public init<T: CollectionType where T.Generator.Element == String, T.Index: BidirectionalIndexType>(_ elements: T) {
|
||||
public init<T: Collection>(_ elements: T) where T.Iterator.Element == String, T.SubSequence.Iterator.Element == String, T.Index: Comparable {
|
||||
|
||||
CoreStore.assert(Set(elements).count == Array(elements).count, "\(cs_typeName(MigrationChain))'s migration chain could not be created due to duplicate version strings.")
|
||||
CoreStore.assert(Set(elements).count == Array(elements).count, "\(cs_typeName(MigrationChain.self))'s migration chain could not be created due to duplicate version strings.")
|
||||
|
||||
var lastVersion: String?
|
||||
var versionTree = [String: String]()
|
||||
@@ -105,8 +105,8 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
self.versionTree = versionTree
|
||||
self.rootVersions = Set([elements.first].flatMap { $0 == nil ? [] : [$0!] })
|
||||
self.leafVersions = Set([elements.last].flatMap { $0 == nil ? [] : [$0!] })
|
||||
self.rootVersions = Set(elements.prefix(1))
|
||||
self.leafVersions = Set(elements.suffix(1))
|
||||
self.valid = valid
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
return
|
||||
}
|
||||
|
||||
CoreStore.assert(false, "\(cs_typeName(MigrationChain))'s migration chain could not be created due to ambiguous version paths.")
|
||||
CoreStore.assert(false, "\(cs_typeName(MigrationChain.self))'s migration chain could not be created due to ambiguous version paths.")
|
||||
|
||||
valid = false
|
||||
}
|
||||
@@ -138,11 +138,11 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
|
||||
var checklist: Set<String> = [start]
|
||||
var version = start
|
||||
while let nextVersion = versionTree[version] where nextVersion != version {
|
||||
while let nextVersion = versionTree[version], nextVersion != version {
|
||||
|
||||
if checklist.contains(nextVersion) {
|
||||
|
||||
CoreStore.assert(false, "\(cs_typeName(MigrationChain))'s migration chain could not be created due to looping version paths.")
|
||||
CoreStore.assert(false, "\(cs_typeName(MigrationChain.self))'s migration chain could not be created due to looping version paths.")
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -154,7 +154,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
self.versionTree = versionTree
|
||||
self.rootVersions = Set(versionTree.keys).subtract(versionTree.values)
|
||||
self.rootVersions = Set(versionTree.keys).subtracting(versionTree.values)
|
||||
self.leafVersions = leafVersions
|
||||
self.valid = valid && Set(versionTree.keys).union(versionTree.values).filter { isVersionAmbiguous($0) }.count <= 0
|
||||
}
|
||||
@@ -168,7 +168,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
|
||||
// MARK: NilLiteralConvertible
|
||||
// MARK: ExpressibleByNilLiteral
|
||||
|
||||
public init(nilLiteral: ()) {
|
||||
|
||||
@@ -176,7 +176,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
|
||||
// MARK: StringLiteralConvertible
|
||||
// MARK: ExpressibleByStringLiteral
|
||||
|
||||
public init(stringLiteral value: String) {
|
||||
|
||||
@@ -200,7 +200,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
|
||||
// MARK: DictionaryLiteralConvertible
|
||||
// MARK: ExpressibleByDictionaryLiteral
|
||||
|
||||
public init(dictionaryLiteral elements: (String, String)...) {
|
||||
|
||||
@@ -208,7 +208,7 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
|
||||
// MARK: ArrayLiteralConvertible
|
||||
// MARK: ExpressibleByArrayLiteral
|
||||
|
||||
public init(arrayLiteral elements: String...) {
|
||||
|
||||
@@ -216,6 +216,17 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
}
|
||||
|
||||
|
||||
// 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>
|
||||
@@ -227,16 +238,16 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
return self.versionTree.count <= 0
|
||||
}
|
||||
|
||||
internal func contains(version: String) -> Bool {
|
||||
internal func contains(_ version: String) -> Bool {
|
||||
|
||||
return self.rootVersions.contains(version)
|
||||
|| self.leafVersions.contains(version)
|
||||
|| self.versionTree[version] != nil
|
||||
}
|
||||
|
||||
internal func nextVersionFrom(version: String) -> String? {
|
||||
internal func nextVersionFrom(_ version: String) -> String? {
|
||||
|
||||
guard let nextVersion = self.versionTree[version] where nextVersion != version else {
|
||||
guard let nextVersion = self.versionTree[version], nextVersion != version else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -246,18 +257,6 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let versionTree: [String: String]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - MigrationChain: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: MigrationChain, rhs: MigrationChain) -> Bool {
|
||||
|
||||
return lhs.versionTree == rhs.versionTree
|
||||
&& lhs.rootVersions == rhs.rootVersions
|
||||
&& lhs.leafVersions == rhs.leafVersions
|
||||
&& lhs.valid == rhs.valid
|
||||
fileprivate let versionTree: [String: String]
|
||||
}
|
||||
|
||||
|
||||
@@ -49,35 +49,54 @@ import Foundation
|
||||
// ...
|
||||
let result = transaction.commit()
|
||||
switch result {
|
||||
case .Success(let hasChanges):
|
||||
case .success(let hasChanges):
|
||||
// hasChanges indicates if there were changes or not
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
// error is a CoreStoreError enum value
|
||||
}
|
||||
}
|
||||
```
|
||||
*/
|
||||
public enum MigrationResult: BooleanType, Hashable {
|
||||
public enum MigrationResult: Hashable {
|
||||
|
||||
/**
|
||||
`MigrationResult.Success` indicates either the migration succeeded, or there were no migrations needed. The associated value is an array of `MigrationType`s reflecting the migration steps completed.
|
||||
`MigrationResult.success` indicates either the migration succeeded, or there were no migrations needed. The associated value is an array of `MigrationType`s reflecting the migration steps completed.
|
||||
*/
|
||||
case Success([MigrationType])
|
||||
case success([MigrationType])
|
||||
|
||||
/**
|
||||
`SaveResult.Failure` indicates that the migration failed. The associated object for this value is the a `CoreStoreError` enum value.
|
||||
`SaveResult.failure` indicates that the migration failed. The associated object for this value is the a `CoreStoreError` enum value.
|
||||
*/
|
||||
case Failure(CoreStoreError)
|
||||
case failure(CoreStoreError)
|
||||
|
||||
|
||||
// MARK: BooleanType
|
||||
|
||||
public var boolValue: Bool {
|
||||
/**
|
||||
Returns `true` if the result indicates `.success`, `false` if the result is `.failure`.
|
||||
*/
|
||||
public var isSuccess: Bool {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success: return true
|
||||
case .Failure: return false
|
||||
case .success: return true
|
||||
case .failure: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,12 +107,12 @@ public enum MigrationResult: BooleanType, Hashable {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success(let migrationTypes):
|
||||
return self.boolValue.hashValue
|
||||
^ migrationTypes.map { $0.hashValue }.reduce(0, combine: ^).hashValue
|
||||
case .success(let migrationTypes):
|
||||
return true.hashValue
|
||||
^ migrationTypes.map { $0.hashValue }.reduce(0, ^).hashValue
|
||||
|
||||
case .Failure(let error):
|
||||
return self.boolValue.hashValue ^ error.hashValue
|
||||
case .failure(let error):
|
||||
return false.hashValue ^ error.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,35 +121,16 @@ public enum MigrationResult: BooleanType, Hashable {
|
||||
|
||||
internal init(_ migrationTypes: [MigrationType]) {
|
||||
|
||||
self = .Success(migrationTypes)
|
||||
self = .success(migrationTypes)
|
||||
}
|
||||
|
||||
internal init(_ error: CoreStoreError) {
|
||||
|
||||
self = .Failure(error)
|
||||
self = .failure(error)
|
||||
}
|
||||
|
||||
internal init(_ error: ErrorType) {
|
||||
internal init(_ error: Error) {
|
||||
|
||||
self = .Failure(CoreStoreError(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - SetupResult: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
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
|
||||
self = .failure(CoreStoreError(error))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,22 +31,22 @@ import Foundation
|
||||
/**
|
||||
The `MigrationType` specifies the type of migration required for a store.
|
||||
*/
|
||||
public enum MigrationType: BooleanType, Hashable {
|
||||
public enum MigrationType: Hashable {
|
||||
|
||||
/**
|
||||
Indicates that the persistent store matches the latest model version and no migration is needed
|
||||
*/
|
||||
case None(version: String)
|
||||
case none(version: String)
|
||||
|
||||
/**
|
||||
Indicates that the persistent store does not match the latest model version but Core Data can infer the mapping model, so a lightweight migration is needed
|
||||
*/
|
||||
case Lightweight(sourceVersion: String, destinationVersion: String)
|
||||
case lightweight(sourceVersion: String, destinationVersion: String)
|
||||
|
||||
/**
|
||||
Indicates that the persistent store does not match the latest model version and Core Data could not infer a mapping model, so a custom migration is needed
|
||||
*/
|
||||
case Heavyweight(sourceVersion: String, destinationVersion: String)
|
||||
case heavyweight(sourceVersion: String, destinationVersion: String)
|
||||
|
||||
/**
|
||||
Returns the source model version for the migration type. If no migration is required, `sourceVersion` will be equal to the `destinationVersion`.
|
||||
@@ -55,13 +55,13 @@ public enum MigrationType: BooleanType, Hashable {
|
||||
|
||||
switch self {
|
||||
|
||||
case .None(let version):
|
||||
case .none(let version):
|
||||
return version
|
||||
|
||||
case .Lightweight(let sourceVersion, _):
|
||||
case .lightweight(let sourceVersion, _):
|
||||
return sourceVersion
|
||||
|
||||
case .Heavyweight(let sourceVersion, _):
|
||||
case .heavyweight(let sourceVersion, _):
|
||||
return sourceVersion
|
||||
}
|
||||
}
|
||||
@@ -73,13 +73,13 @@ public enum MigrationType: BooleanType, Hashable {
|
||||
|
||||
switch self {
|
||||
|
||||
case .None(let version):
|
||||
case .none(let version):
|
||||
return version
|
||||
|
||||
case .Lightweight(_, let destinationVersion):
|
||||
case .lightweight(_, let destinationVersion):
|
||||
return destinationVersion
|
||||
|
||||
case .Heavyweight(_, let destinationVersion):
|
||||
case .heavyweight(_, let destinationVersion):
|
||||
return destinationVersion
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public enum MigrationType: BooleanType, Hashable {
|
||||
*/
|
||||
public var isLightweightMigration: Bool {
|
||||
|
||||
if case .Lightweight = self {
|
||||
if case .lightweight = self {
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -101,23 +101,44 @@ public enum MigrationType: BooleanType, Hashable {
|
||||
*/
|
||||
public var isHeavyweightMigration: Bool {
|
||||
|
||||
if case .Heavyweight = self {
|
||||
if case .heavyweight = self {
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// MARK: BooleanType
|
||||
|
||||
public var boolValue: Bool {
|
||||
/**
|
||||
Returns `true` if the `MigrationType` is either a lightweight or a heavyweight migration. Returns `false` if no migrations specified.
|
||||
*/
|
||||
public var hasMigration: Bool {
|
||||
|
||||
switch self {
|
||||
|
||||
case .None: return false
|
||||
case .Lightweight: return true
|
||||
case .Heavyweight: return true
|
||||
case .none: return false
|
||||
case .lightweight: return true
|
||||
case .heavyweight: return true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,39 +147,17 @@ public enum MigrationType: BooleanType, Hashable {
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
let preHash = self.boolValue.hashValue ^ self.isHeavyweightMigration.hashValue
|
||||
let preHash = self.hasMigration.hashValue ^ self.isHeavyweightMigration.hashValue
|
||||
switch self {
|
||||
|
||||
case .None(let version):
|
||||
case .none(let version):
|
||||
return preHash ^ version.hashValue
|
||||
|
||||
case .Lightweight(let sourceVersion, let destinationVersion):
|
||||
case .lightweight(let sourceVersion, let destinationVersion):
|
||||
return preHash ^ sourceVersion.hashValue ^ destinationVersion.hashValue
|
||||
|
||||
case .Heavyweight(let sourceVersion, let destinationVersion):
|
||||
case .heavyweight(let sourceVersion, let destinationVersion):
|
||||
return preHash ^ sourceVersion.hashValue ^ destinationVersion.hashValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - MigrationType: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,36 +51,55 @@ import CoreData
|
||||
SQLiteStore(),
|
||||
completion: { (result: SetupResult) -> Void in
|
||||
switch result {
|
||||
case .Success(let storage):
|
||||
case .success(let storage):
|
||||
// storage is the related StorageInterface instance
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
// error is the CoreStoreError enum value for the failure
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
*/
|
||||
public enum SetupResult<T: StorageInterface>: BooleanType, Hashable {
|
||||
public enum SetupResult<T: StorageInterface>: Hashable {
|
||||
|
||||
/**
|
||||
`SetupResult.Success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance.
|
||||
`SetupResult.success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance.
|
||||
*/
|
||||
case Success(T)
|
||||
case success(T)
|
||||
|
||||
/**
|
||||
`SetupResult.Failure` indicates that the storage setup failed. The associated object for this value is the related `CoreStoreError` enum value.
|
||||
`SetupResult.failure` indicates that the storage setup failed. The associated object for this value is the related `CoreStoreError` enum value.
|
||||
*/
|
||||
case Failure(CoreStoreError)
|
||||
case failure(CoreStoreError)
|
||||
|
||||
|
||||
// MARK: BooleanType
|
||||
|
||||
public var boolValue: Bool {
|
||||
/**
|
||||
Returns `true` if the result indicates `.success`, `false` if the result is `.failure`.
|
||||
*/
|
||||
public var isSuccess: Bool {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success: return true
|
||||
case .Failure: return false
|
||||
case .success: return true
|
||||
case .failure: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,11 +110,11 @@ public enum SetupResult<T: StorageInterface>: BooleanType, Hashable {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success(let storage):
|
||||
return self.boolValue.hashValue ^ ObjectIdentifier(storage).hashValue
|
||||
case .success(let storage):
|
||||
return true.hashValue ^ ObjectIdentifier(storage).hashValue
|
||||
|
||||
case .Failure(let error):
|
||||
return self.boolValue.hashValue ^ error.hashValue
|
||||
case .failure(let error):
|
||||
return false.hashValue ^ error.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,80 +123,16 @@ public enum SetupResult<T: StorageInterface>: BooleanType, Hashable {
|
||||
|
||||
internal init(_ storage: T) {
|
||||
|
||||
self = .Success(storage)
|
||||
self = .success(storage)
|
||||
}
|
||||
|
||||
internal init(_ error: CoreStoreError) {
|
||||
|
||||
self = .Failure(error)
|
||||
self = .failure(error)
|
||||
}
|
||||
|
||||
internal init(_ error: ErrorType) {
|
||||
internal init(_ error: Error) {
|
||||
|
||||
self = .Failure(CoreStoreError(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - SetupResult: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult<T>` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Replaced by SetupResult by using the new addStorage(_:completion:) method variants.")
|
||||
public enum PersistentStoreResult: BooleanType {
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult.Success` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
case Success(NSPersistentStore)
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult.Failure` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
case Failure(NSError)
|
||||
|
||||
|
||||
// MARK: BooleanType
|
||||
|
||||
public var boolValue: Bool {
|
||||
|
||||
switch self {
|
||||
|
||||
case .Success: return true
|
||||
case .Failure: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal init(_ store: NSPersistentStore) {
|
||||
|
||||
self = .Success(store)
|
||||
}
|
||||
|
||||
internal init(_ error: NSError) {
|
||||
|
||||
self = .Failure(error)
|
||||
self = .failure(CoreStoreError(error))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +43,11 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- parameter completion: the block executed after the save completes. Success or failure is reported by the `CSSaveResult` argument of the block.
|
||||
*/
|
||||
@objc
|
||||
public func commitWithCompletion(completion: ((result: CSSaveResult) -> Void)?) {
|
||||
public func commitWithCompletion(_ completion: ((_ result: CSSaveResult) -> Void)?) {
|
||||
|
||||
self.bridgeToSwift.commit { (result) in
|
||||
|
||||
completion?(result: result.bridgeToObjectiveC)
|
||||
completion?(result.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,13 +58,14 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- 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? {
|
||||
@discardableResult
|
||||
public func beginSynchronous(_ closure: @escaping (_ transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.bridgeToSwift.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +75,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +88,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
@objc
|
||||
public override func createInto(into: CSInto) -> AnyObject {
|
||||
public override func createInto(_ into: CSInto) -> Any {
|
||||
|
||||
return self.bridgeToSwift.create(into.bridgeToSwift)
|
||||
}
|
||||
@@ -99,8 +100,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public override func editObject(object: NSManagedObject?) -> AnyObject? {
|
||||
public override func editObject(_ object: NSManagedObject?) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(object)
|
||||
}
|
||||
@@ -113,8 +113,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public override func editInto(into: CSInto, objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public override func editInto(_ into: CSInto, objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(into.bridgeToSwift, objectID)
|
||||
}
|
||||
@@ -125,7 +124,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- parameter object: the `NSManagedObject` type to be deleted
|
||||
*/
|
||||
@objc
|
||||
public override func deleteObject(object: NSManagedObject?) {
|
||||
public override func deleteObject(_ object: NSManagedObject?) {
|
||||
|
||||
self.bridgeToSwift.delete(object)
|
||||
}
|
||||
@@ -136,7 +135,7 @@ public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- parameter objects: the `NSManagedObject`s type to be deleted
|
||||
*/
|
||||
@objc
|
||||
public override func deleteObjects(objects: [NSManagedObject]) {
|
||||
public override func deleteObjects(_ objects: [NSManagedObject]) {
|
||||
|
||||
self.bridgeToSwift.delete(objects)
|
||||
}
|
||||
|
||||
@@ -38,17 +38,9 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObject(object: NSManagedObject) -> AnyObject? {
|
||||
public func fetchExistingObject(_ object: NSManagedObject) -> Any? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.bridgeToSwift.context.existingObjectWithID(object.objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.bridgeToSwift.context.fetchExisting(object)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,17 +50,9 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public func fetchExistingObjectWithID(_ objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.bridgeToSwift.context.existingObjectWithID(objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.bridgeToSwift.context.fetchExisting(objectID)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,10 +62,9 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjects(objects: [NSManagedObject]) -> [AnyObject] {
|
||||
public func fetchExistingObjects(_ objects: [NSManagedObject]) -> [Any] {
|
||||
|
||||
return objects.flatMap { try? self.bridgeToSwift.context.existingObjectWithID($0.objectID) }
|
||||
return self.bridgeToSwift.context.fetchExisting(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,10 +74,9 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [AnyObject] {
|
||||
public func fetchExistingObjectsWithIDs(_ objectIDs: [NSManagedObjectID]) -> [Any] {
|
||||
|
||||
return objectIDs.flatMap { try? self.bridgeToSwift.context.existingObjectWithID($0) }
|
||||
return self.bridgeToSwift.context.fetchExisting(objectIDs)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,8 +87,7 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> AnyObject? {
|
||||
public func fetchOneFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> Any? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
@@ -123,8 +104,7 @@ public extension CSBaseDataTransaction {
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [AnyObject]? {
|
||||
public func fetchAllFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [Any]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
@@ -141,14 +121,15 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
public func fetchCountFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.bridgeToSwift.context.fetchCount(from, fetchClauses)
|
||||
return self.bridgeToSwift.context
|
||||
.fetchCount(from, fetchClauses)
|
||||
.flatMap { NSNumber(value: $0) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,8 +140,7 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
public func fetchObjectIDFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
@@ -180,8 +160,7 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
public func queryValueFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> Any? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
@@ -201,8 +180,7 @@ public extension CSBaseDataTransaction {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributesFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.bridgeToSwift.isRunningInAllowedQueue(),
|
||||
|
||||
@@ -55,7 +55,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
@objc
|
||||
public func createInto(into: CSInto) -> AnyObject {
|
||||
public func createInto(_ into: CSInto) -> Any {
|
||||
|
||||
return self.bridgeToSwift.create(into.bridgeToSwift)
|
||||
}
|
||||
@@ -67,8 +67,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func editObject(object: NSManagedObject?) -> AnyObject? {
|
||||
public func editObject(_ object: NSManagedObject?) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(object)
|
||||
}
|
||||
@@ -81,8 +80,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func editInto(into: CSInto, objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public func editInto(_ into: CSInto, objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(into.bridgeToSwift, objectID)
|
||||
}
|
||||
@@ -93,7 +91,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter object: the `NSManagedObject` to be deleted
|
||||
*/
|
||||
@objc
|
||||
public func deleteObject(object: NSManagedObject?) {
|
||||
public func deleteObject(_ object: NSManagedObject?) {
|
||||
|
||||
self.bridgeToSwift.delete(object)
|
||||
}
|
||||
@@ -104,7 +102,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter objects: the `NSManagedObject`s to be deleted
|
||||
*/
|
||||
@objc
|
||||
public func deleteObjects(objects: [NSManagedObject]) {
|
||||
public func deleteObjects(_ objects: [NSManagedObject]) {
|
||||
|
||||
self.bridgeToSwift.delete(objects)
|
||||
}
|
||||
@@ -127,7 +125,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s that were inserted to the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func insertedObjects() -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.insertedObjects()
|
||||
@@ -140,8 +137,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were inserted to the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func insertedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
public func insertedObjectsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.insertedObjects(entity)
|
||||
}
|
||||
@@ -152,7 +148,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObjectID`s that were inserted to the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func insertedObjectIDs() -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.insertedObjectIDs()
|
||||
@@ -165,8 +160,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were inserted to the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func insertedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
public func insertedObjectIDsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.insertedObjectIDs(entity)
|
||||
}
|
||||
@@ -177,7 +171,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s that were updated to the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func updatedObjects() -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.updatedObjects()
|
||||
@@ -190,8 +183,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were updated in the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func updatedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
public func updatedObjectsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.updatedObjects(entity)
|
||||
}
|
||||
@@ -202,7 +194,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObjectID`s that were updated in the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func updatedObjectIDs() -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.updatedObjectIDs()
|
||||
@@ -215,8 +206,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were updated in the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func updatedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
public func updatedObjectIDsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.updatedObjectIDs(entity)
|
||||
}
|
||||
@@ -227,7 +217,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s that were deleted from the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func deletedObjects() -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.deletedObjects()
|
||||
@@ -240,8 +229,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were deleted from the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func deletedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
public func deletedObjectsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObject> {
|
||||
|
||||
return self.bridgeToSwift.deletedObjects(entity)
|
||||
}
|
||||
@@ -253,7 +241,6 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func deletedObjectIDs() -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.deletedObjectIDs()
|
||||
@@ -266,8 +253,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func deletedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
public func deletedObjectIDsOfType(_ entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
|
||||
|
||||
return self.bridgeToSwift.deletedObjectIDs(entity)
|
||||
}
|
||||
@@ -280,7 +266,7 @@ public class CSBaseDataTransaction: NSObject, CoreStoreObjectiveCType {
|
||||
return ObjectIdentifier(self.bridgeToSwift).hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSBaseDataTransaction else {
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ import CoreData
|
||||
public protocol CSFetchClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>)
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public protocol CSFetchClause {
|
||||
public protocol CSQueryClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>)
|
||||
}
|
||||
|
||||
|
||||
@@ -68,5 +68,5 @@ public protocol CSQueryClause {
|
||||
public protocol CSDeleteClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>)
|
||||
}
|
||||
|
||||
@@ -44,11 +44,10 @@ public extension CSCoreStore {
|
||||
}
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSInMemoryStore` instance
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `CSSetupResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failure `CSSetupResult` result if an error occurs asynchronously.
|
||||
*/
|
||||
public static func addInMemoryStorage(storage: CSInMemoryStore, completion: (CSSetupResult) -> Void) {
|
||||
public static func addInMemoryStorage(_ storage: CSInMemoryStore, completion: @escaping (CSSetupResult) -> Void) {
|
||||
|
||||
self.defaultStack.addInMemoryStorage(storage, completion: completion)
|
||||
}
|
||||
@@ -68,13 +67,12 @@ public extension CSCoreStore {
|
||||
}
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSSQLiteStore` instance
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `CSSetupResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failure `CSSetupResult` result if an error occurs asynchronously. Note that the `CSLocalStorage` associated to the `-[CSSetupResult storage]` may not always be the same instance as the parameter argument if a previous `CSLocalStorage` was already added at the same URL and with the same configuration.
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: an `NSProgress` instance if a migration has started. `nil` if no migrations are required or if `error` was set.
|
||||
*/
|
||||
public static func addSQLiteStorage(storage: CSSQLiteStore, completion: (CSSetupResult) -> Void, error: NSErrorPointer) -> NSProgress? {
|
||||
public static func addSQLiteStorage(_ storage: CSSQLiteStore, completion: @escaping (CSSetupResult) -> Void, error: NSErrorPointer) -> Progress? {
|
||||
|
||||
return self.defaultStack.addSQLiteStorage(storage, completion: completion, error: error)
|
||||
}
|
||||
@@ -88,7 +86,7 @@ public extension CSCoreStore {
|
||||
- returns: an `NSProgress` instance if a migration has started. `nil` if no migrations are required or if `error` was set.
|
||||
*/
|
||||
@objc
|
||||
public static func upgradeStorageIfNeeded(storage: CSSQLiteStore, completion: (CSMigrationResult) -> Void, error: NSErrorPointer) -> NSProgress? {
|
||||
public static func upgradeStorageIfNeeded(_ storage: CSSQLiteStore, completion: @escaping (CSMigrationResult) -> Void, error: NSErrorPointer) -> Progress? {
|
||||
|
||||
return self.defaultStack.upgradeStorageIfNeeded(storage, completion: completion, error: error)
|
||||
}
|
||||
@@ -101,8 +99,7 @@ public extension CSCoreStore {
|
||||
- returns: a `CSMigrationType` array indicating the migration steps required for the store, or an empty array if the file does not exist yet. Otherwise, `nil` is returned and the `error` argument is set if either inspection of the store failed, or if no mapping model was found/inferred.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func requiredMigrationsForSQLiteStore(storage: CSSQLiteStore, error: NSErrorPointer) -> [CSMigrationType]? {
|
||||
public static func requiredMigrationsForSQLiteStore(_ storage: CSSQLiteStore, error: NSErrorPointer) -> [CSMigrationType]? {
|
||||
|
||||
return self.defaultStack.requiredMigrationsForSQLiteStore(storage, error: error)
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ public extension CSCoreStore {
|
||||
- returns: a `CSObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorObject(object: NSManagedObject) -> CSObjectMonitor {
|
||||
public static func monitorObject(_ object: NSManagedObject) -> CSObjectMonitor {
|
||||
|
||||
return self.defaultStack.monitorObject(object)
|
||||
}
|
||||
@@ -54,8 +53,7 @@ public extension CSCoreStore {
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorListFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
public static func monitorListFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
return self.defaultStack.monitorListFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -68,7 +66,7 @@ public extension CSCoreStore {
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public static func monitorListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
public static func monitorListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
return self.defaultStack.monitorListByCreatingAsynchronously(
|
||||
createAsynchronously,
|
||||
@@ -86,8 +84,7 @@ public extension CSCoreStore {
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorSectionedListFrom(from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
public static func monitorSectionedListFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
return self.defaultStack.monitorSectionedListFrom(
|
||||
from,
|
||||
@@ -105,7 +102,7 @@ public extension CSCoreStore {
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public static func monitorSectionedListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
public static func monitorSectionedListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
self.defaultStack.monitorSectionedListByCreatingAsynchronously(
|
||||
createAsynchronously,
|
||||
|
||||
@@ -38,8 +38,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObject(object: NSManagedObject) -> AnyObject? {
|
||||
public static func fetchExistingObject(_ object: NSManagedObject) -> Any? {
|
||||
|
||||
return self.defaultStack.fetchExistingObject(object)
|
||||
}
|
||||
@@ -51,8 +50,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public static func fetchExistingObjectWithID(_ objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
return self.defaultStack.fetchExistingObjectWithID(objectID)
|
||||
}
|
||||
@@ -64,8 +62,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjects(objects: [NSManagedObject]) -> [AnyObject] {
|
||||
public static func fetchExistingObjects(_ objects: [NSManagedObject]) -> [Any] {
|
||||
|
||||
return self.defaultStack.fetchExistingObjects(objects)
|
||||
}
|
||||
@@ -77,8 +74,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [AnyObject] {
|
||||
public static func fetchExistingObjectsWithIDs(_ objectIDs: [NSManagedObjectID]) -> [Any] {
|
||||
|
||||
return self.defaultStack.fetchExistingObjectsWithIDs(objectIDs)
|
||||
}
|
||||
@@ -91,8 +87,7 @@ public extension CSCoreStore {
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> AnyObject? {
|
||||
public static func fetchOneFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> Any? {
|
||||
|
||||
return self.defaultStack.fetchOneFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -105,8 +100,7 @@ public extension CSCoreStore {
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [AnyObject]? {
|
||||
public static func fetchAllFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [Any]? {
|
||||
|
||||
return self.defaultStack.fetchAllFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -119,8 +113,7 @@ public extension CSCoreStore {
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
public static func fetchCountFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
return self.defaultStack.fetchCountFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -133,8 +126,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
public static func fetchObjectIDFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -147,8 +139,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
public static func fetchObjectIDsFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDsFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
@@ -164,8 +155,7 @@ public extension CSCoreStore {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
public static func queryValueFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> Any? {
|
||||
|
||||
return self.defaultStack.queryValueFrom(from, selectClause: selectClause, queryClauses: queryClauses)
|
||||
}
|
||||
@@ -181,8 +171,7 @@ public extension CSCoreStore {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public static func queryAttributesFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[String: Any]]? {
|
||||
|
||||
return self.defaultStack.queryAttributesFrom(from, selectClause: selectClause, queryClauses: queryClauses)
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public extension CSCoreStore {
|
||||
- returns: the `NSManagedObject` class for the given entity name, or `nil` if not found
|
||||
*/
|
||||
@objc
|
||||
public static func entityClassWithName(name: String) -> NSManagedObject.Type? {
|
||||
public static func entityClassWithName(_ name: String) -> NSManagedObject.Type? {
|
||||
|
||||
return CoreStore.entityTypesByName[name]
|
||||
}
|
||||
@@ -65,9 +65,9 @@ public extension CSCoreStore {
|
||||
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from `defaultStack`'s model.
|
||||
*/
|
||||
@objc
|
||||
public static func entityDescriptionForClass(type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
public static func entityDescriptionForClass(_ type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return CoreStore.entityDescriptionForType(type)
|
||||
return CoreStore.entityDescription(for: type)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,12 +75,12 @@ public extension CSCoreStore {
|
||||
```
|
||||
CSSQLiteStore *storage = [CSCoreStore addInMemoryStorageAndWaitAndReturnError:&error];
|
||||
```
|
||||
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSInMemoryStore` added to the `defaultStack`
|
||||
*/
|
||||
@objc
|
||||
public static func addInMemoryStorageAndWaitAndReturnError(error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
@discardableResult
|
||||
public static func addInMemoryStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
|
||||
return self.defaultStack.addInMemoryStorageAndWaitAndReturnError(error)
|
||||
}
|
||||
@@ -90,12 +90,12 @@ public extension CSCoreStore {
|
||||
```
|
||||
CSSQLiteStore *storage = [CSCoreStore addSQLiteStorageAndWaitAndReturnError:&error];
|
||||
```
|
||||
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSSQLiteStore` added to the `defaultStack`
|
||||
*/
|
||||
@objc
|
||||
public static func addSQLiteStorageAndWaitAndReturnError(error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
@discardableResult
|
||||
public static func addSQLiteStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
|
||||
return self.defaultStack.addSQLiteStorageAndWaitAndReturnError(error)
|
||||
}
|
||||
@@ -108,13 +108,13 @@ public extension CSCoreStore {
|
||||
addStorageAndWait: [[CSInMemoryStore alloc] initWithConfiguration: @"Config1"]
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSInMemoryStore`
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSInMemoryStore` added to the `defaultStack`
|
||||
*/
|
||||
@objc
|
||||
public static func addInMemoryStorageAndWait(storage: CSInMemoryStore, error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
@discardableResult
|
||||
public static func addInMemoryStorageAndWait(_ storage: CSInMemoryStore, error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
|
||||
return self.defaultStack.addInMemoryStorageAndWait(storage, error: error)
|
||||
}
|
||||
@@ -127,14 +127,14 @@ public extension CSCoreStore {
|
||||
addStorageAndWait: [[CSSQLiteStore alloc] initWithConfiguration: @"Config1"]
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSSQLiteStore`
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSSQLiteStore` added to the `defaultStack`
|
||||
*/
|
||||
@objc
|
||||
public static func addSQLiteStorageAndWait(storage: CSSQLiteStore, error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
@discardableResult
|
||||
public static func addSQLiteStorageAndWait(_ storage: CSSQLiteStore, error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
|
||||
return self.defaultStack.addSQLiteStorageAndWait(storage, error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,11 +36,11 @@ public extension CSCoreStore {
|
||||
- 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`.
|
||||
*/
|
||||
@objc
|
||||
public static func beginAsynchronous(closure: (transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
public static func beginAsynchronous(_ closure: @escaping (_ transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
|
||||
return CoreStore.beginAsynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,13 +51,14 @@ public extension CSCoreStore {
|
||||
- returns: a `CSSaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
|
||||
*/
|
||||
@objc
|
||||
public static func beginSynchronous(closure: (transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
@discardableResult
|
||||
public static func beginSynchronous(_ closure: @escaping (_ transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
CoreStore.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +70,6 @@ public extension CSCoreStore {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func beginUnsafe() -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
@@ -85,8 +85,7 @@ public extension CSCoreStore {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
public static func beginUnsafeWithSupportsUndo(_ supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
|
||||
@@ -44,12 +44,11 @@ public extension CSDataStack {
|
||||
}
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSInMemoryStore` instance
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `CSSetupResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failure `CSSetupResult` result if an error occurs asynchronously.
|
||||
*/
|
||||
@objc
|
||||
public func addInMemoryStorage(storage: CSInMemoryStore, completion: (CSSetupResult) -> Void) {
|
||||
public func addInMemoryStorage(_ storage: CSInMemoryStore, completion: @escaping (CSSetupResult) -> Void) {
|
||||
|
||||
self.bridgeToSwift.addStorage(
|
||||
storage.bridgeToSwift,
|
||||
@@ -72,14 +71,13 @@ public extension CSDataStack {
|
||||
}
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSSQLiteStore` instance
|
||||
- parameter completion: the closure to be executed on the main queue when the process completes, either due to success or failure. The closure's `CSSetupResult` argument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failure `CSSetupResult` result if an error occurs asynchronously. Note that the `CSLocalStorage` associated to the `-[CSSetupResult storage]` may not always be the same instance as the parameter argument if a previous `CSLocalStorage` was already added at the same URL and with the same configuration.
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: an `NSProgress` instance if a migration has started. `nil` if no migrations are required or if `error` was set.
|
||||
*/
|
||||
@objc
|
||||
public func addSQLiteStorage(storage: CSSQLiteStore, completion: (CSSetupResult) -> Void, error: NSErrorPointer) -> NSProgress? {
|
||||
public func addSQLiteStorage(_ storage: CSSQLiteStore, completion: @escaping (CSSetupResult) -> Void, error: NSErrorPointer) -> Progress? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
@@ -99,7 +97,7 @@ public extension CSDataStack {
|
||||
- returns: an `NSProgress` instance if a migration has started. `nil` if no migrations are required or if `error` was set.
|
||||
*/
|
||||
@objc
|
||||
public func upgradeStorageIfNeeded(storage: CSSQLiteStore, completion: (CSMigrationResult) -> Void, error: NSErrorPointer) -> NSProgress? {
|
||||
public func upgradeStorageIfNeeded(_ storage: CSSQLiteStore, completion: @escaping (CSMigrationResult) -> Void, error: NSErrorPointer) -> Progress? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
@@ -118,12 +116,11 @@ public extension CSDataStack {
|
||||
- returns: a `CSMigrationType` array indicating the migration steps required for the store, or an empty array if the file does not exist yet. Otherwise, `nil` is returned and the `error` argument is set if either inspection of the store failed, or if no mapping model was found/inferred.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func requiredMigrationsForSQLiteStore(storage: CSSQLiteStore, error: NSErrorPointer) -> [CSMigrationType]? {
|
||||
public func requiredMigrationsForSQLiteStore(_ storage: CSSQLiteStore, error: NSErrorPointer) -> [CSMigrationType]? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
try self.bridgeToSwift.requiredMigrationsForStorage(storage.bridgeToSwift)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ public extension CSDataStack {
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorObject(object: NSManagedObject) -> CSObjectMonitor {
|
||||
public func monitorObject(_ object: NSManagedObject) -> CSObjectMonitor {
|
||||
|
||||
return bridge {
|
||||
|
||||
@@ -57,11 +56,10 @@ public extension CSDataStack {
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorListFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
public func monitorListFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
@@ -74,9 +72,9 @@ public extension CSDataStack {
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: nil,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -90,10 +88,10 @@ public extension CSDataStack {
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public func monitorListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
public func monitorListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
@@ -104,9 +102,9 @@ public extension CSDataStack {
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: nil,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
},
|
||||
createAsynchronously: {
|
||||
|
||||
@@ -124,11 +122,10 @@ public extension CSDataStack {
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorSectionedListFrom(from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
public func monitorSectionedListFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
@@ -141,9 +138,9 @@ public extension CSDataStack {
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: sectionBy.bridgeToSwift,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -157,10 +154,10 @@ public extension CSDataStack {
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
public func monitorSectionedListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
@@ -171,9 +168,9 @@ public extension CSDataStack {
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: sectionBy.bridgeToSwift,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
},
|
||||
createAsynchronously: {
|
||||
|
||||
|
||||
@@ -38,17 +38,9 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObject(object: NSManagedObject) -> AnyObject? {
|
||||
public func fetchExistingObject(_ object: NSManagedObject) -> Any? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.bridgeToSwift.mainContext.existingObjectWithID(object.objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.bridgeToSwift.mainContext.fetchExisting(object)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,17 +50,9 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public func fetchExistingObjectWithID(_ objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.bridgeToSwift.mainContext.existingObjectWithID(objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.bridgeToSwift.mainContext.fetchExisting(objectID)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,10 +62,9 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjects(objects: [NSManagedObject]) -> [AnyObject] {
|
||||
public func fetchExistingObjects(_ objects: [NSManagedObject]) -> [Any] {
|
||||
|
||||
return objects.flatMap { try? self.bridgeToSwift.mainContext.existingObjectWithID($0.objectID) }
|
||||
return self.bridgeToSwift.mainContext.fetchExisting(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,10 +74,9 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [AnyObject] {
|
||||
public func fetchExistingObjectsWithIDs(_ objectIDs: [NSManagedObjectID]) -> [Any] {
|
||||
|
||||
return objectIDs.flatMap { try? self.bridgeToSwift.mainContext.existingObjectWithID($0) }
|
||||
return self.bridgeToSwift.mainContext.fetchExisting(objectIDs)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,11 +87,10 @@ public extension CSDataStack {
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> AnyObject? {
|
||||
public func fetchOneFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> Any? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.fetchOne(from, fetchClauses)
|
||||
@@ -123,11 +104,10 @@ public extension CSDataStack {
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [AnyObject]? {
|
||||
public func fetchAllFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [Any]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.fetchAll(from, fetchClauses)
|
||||
@@ -141,14 +121,15 @@ public extension CSDataStack {
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
public func fetchCountFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.fetchCount(from, fetchClauses)
|
||||
return self.bridgeToSwift.mainContext
|
||||
.fetchCount(from, fetchClauses)
|
||||
.flatMap { NSNumber(value: $0) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,11 +140,10 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
public func fetchObjectIDFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.fetchObjectID(from, fetchClauses)
|
||||
@@ -177,11 +157,10 @@ public extension CSDataStack {
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
public func fetchObjectIDsFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
@@ -198,11 +177,10 @@ public extension CSDataStack {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func queryValueFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
public func queryValueFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> Any? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
@@ -219,11 +197,10 @@ public extension CSDataStack {
|
||||
- returns: the result of the the query. The type of the return value is specified by the generic type of the `CSSelect` parameter.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func queryAttributesFrom(from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
public func queryAttributesFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[String: Any]]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to query from a \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.bridgeToSwift.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
|
||||
@@ -36,11 +36,11 @@ public extension CSDataStack {
|
||||
- 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`.
|
||||
*/
|
||||
@objc
|
||||
public func beginAsynchronous(closure: (transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
public func beginAsynchronous(_ closure: @escaping (_ transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
|
||||
return self.bridgeToSwift.beginAsynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,13 +51,14 @@ public extension CSDataStack {
|
||||
- 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? {
|
||||
@discardableResult
|
||||
public func beginSynchronous(_ closure: @escaping (_ transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.bridgeToSwift.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +70,6 @@ public extension CSDataStack {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafe() -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
@@ -85,8 +85,7 @@ public extension CSDataStack {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
public func beginUnsafeWithSupportsUndo(_ supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
|
||||
@@ -54,12 +54,12 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter versionChain: the version strings that indicate the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(modelName: String?, bundle: NSBundle?, versionChain: [String]?) {
|
||||
public convenience init(modelName: String?, bundle: Bundle?, versionChain: [String]?) {
|
||||
|
||||
self.init(
|
||||
DataStack(
|
||||
modelName: modelName ?? DataStack.applicationName,
|
||||
bundle: bundle ?? NSBundle.mainBundle(),
|
||||
bundle: bundle ?? Bundle.main,
|
||||
migrationChain: versionChain.flatMap { MigrationChain($0) } ?? nil
|
||||
)
|
||||
)
|
||||
@@ -73,12 +73,12 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter versionTree: the version strings that indicate the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(modelName: String?, bundle: NSBundle?, versionTree: [String: String]?) {
|
||||
public convenience init(modelName: String?, bundle: Bundle?, versionTree: [String: String]?) {
|
||||
|
||||
self.init(
|
||||
DataStack(
|
||||
modelName: modelName ?? DataStack.applicationName,
|
||||
bundle: bundle ?? NSBundle.mainBundle(),
|
||||
bundle: bundle ?? Bundle.main,
|
||||
migrationChain: versionTree.flatMap { MigrationChain($0) } ?? nil
|
||||
)
|
||||
)
|
||||
@@ -142,7 +142,7 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` class for the given entity name, or `nil` if not found
|
||||
*/
|
||||
@objc
|
||||
public func entityClassWithName(name: String) -> NSManagedObject.Type? {
|
||||
public func entityClassWithName(_ name: String) -> NSManagedObject.Type? {
|
||||
|
||||
return self.bridgeToSwift.entityTypesByName[name]
|
||||
}
|
||||
@@ -151,9 +151,9 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from stack's model.
|
||||
*/
|
||||
@objc
|
||||
public func entityDescriptionForClass(type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
public func entityDescriptionForClass(_ type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return self.bridgeToSwift.entityDescriptionForType(type)
|
||||
return self.bridgeToSwift.entityDescription(for: type)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,16 +161,16 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
```
|
||||
CSSQLiteStore *storage = [dataStack addInMemoryStorageAndWaitAndReturnError:&error];
|
||||
```
|
||||
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSInMemoryStore` added to the stack
|
||||
*/
|
||||
@objc
|
||||
public func addInMemoryStorageAndWaitAndReturnError(error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
@discardableResult
|
||||
public func addInMemoryStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
try self.bridgeToSwift.addStorageAndWait(InMemoryStore)
|
||||
try self.bridgeToSwift.addStorageAndWait(InMemoryStore.self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,16 +179,16 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
```
|
||||
CSSQLiteStore *storage = [dataStack addSQLiteStorageAndWaitAndReturnError:&error];
|
||||
```
|
||||
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSSQLiteStore` added to the stack
|
||||
*/
|
||||
@objc
|
||||
public func addSQLiteStorageAndWaitAndReturnError(error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
@discardableResult
|
||||
public func addSQLiteStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
try self.bridgeToSwift.addStorageAndWait(SQLiteStore)
|
||||
try self.bridgeToSwift.addStorageAndWait(SQLiteStore.self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,13 +200,13 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
addStorageAndWait: [[CSInMemoryStore alloc] initWithConfiguration: @"Config1"]
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSInMemoryStore`
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSInMemoryStore` added to the stack
|
||||
*/
|
||||
@objc
|
||||
public func addInMemoryStorageAndWait(storage: CSInMemoryStore, error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
@discardableResult
|
||||
public func addInMemoryStorageAndWait(_ storage: CSInMemoryStore, error: NSErrorPointer) -> CSInMemoryStore? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
@@ -222,13 +222,13 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
addStorageAndWait: [[CSSQLiteStore alloc] initWithConfiguration: @"Config1"]
|
||||
error: &error];
|
||||
```
|
||||
|
||||
- parameter storage: the `CSSQLiteStore`
|
||||
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
|
||||
- returns: the `CSSQLiteStore` added to the stack
|
||||
*/
|
||||
@objc
|
||||
public func addSQLiteStorageAndWait(storage: CSSQLiteStore, error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
@discardableResult
|
||||
public func addSQLiteStorageAndWait(_ storage: CSSQLiteStore, error: NSErrorPointer) -> CSSQLiteStore? {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
@@ -244,7 +244,7 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
return ObjectIdentifier(self.bridgeToSwift).hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSDataStack else {
|
||||
|
||||
@@ -255,7 +255,7 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public final class CSError: NSError, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSError else {
|
||||
|
||||
@@ -64,7 +64,7 @@ public final class CSError: NSError, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
@@ -76,58 +76,7 @@ public final class CSError: NSError, CoreStoreObjectiveCType {
|
||||
|
||||
return swift
|
||||
}
|
||||
|
||||
func createSwiftObject(error: CSError) -> CoreStoreError {
|
||||
|
||||
guard error.domain == CoreStoreErrorDomain else {
|
||||
|
||||
return .InternalError(NSError: self)
|
||||
}
|
||||
|
||||
guard let code = CoreStoreErrorCode(rawValue: error.code) else {
|
||||
|
||||
return .Unknown
|
||||
}
|
||||
|
||||
let info = error.userInfo
|
||||
switch code {
|
||||
|
||||
case .UnknownError:
|
||||
return .Unknown
|
||||
|
||||
case .DifferentPersistentStoreExistsAtURL:
|
||||
guard case let existingPersistentStoreURL as NSURL = info["existingPersistentStoreURL"] else {
|
||||
|
||||
return .Unknown
|
||||
}
|
||||
return .DifferentStorageExistsAtURL(existingPersistentStoreURL: existingPersistentStoreURL)
|
||||
|
||||
case .MappingModelNotFound:
|
||||
guard let localStoreURL = info["localStoreURL"] as? NSURL,
|
||||
let targetModel = info["targetModel"] as? NSManagedObjectModel,
|
||||
let targetModelVersion = info["targetModelVersion"] as? String else {
|
||||
|
||||
return .Unknown
|
||||
}
|
||||
return .MappingModelNotFound(localStoreURL: localStoreURL, targetModel: targetModel, targetModelVersion: targetModelVersion)
|
||||
|
||||
case .ProgressiveMigrationRequired:
|
||||
guard let localStoreURL = info["localStoreURL"] as? NSURL else {
|
||||
|
||||
return .Unknown
|
||||
}
|
||||
return .ProgressiveMigrationRequired(localStoreURL: localStoreURL)
|
||||
|
||||
case .InternalError:
|
||||
guard case let NSError as NSError = info["NSError"] else {
|
||||
|
||||
return .Unknown
|
||||
}
|
||||
return .InternalError(NSError: NSError)
|
||||
}
|
||||
}
|
||||
|
||||
let swift = createSwiftObject(self)
|
||||
let swift = CoreStoreError(_bridgedNSError: self) ?? .unknown
|
||||
self.swiftError = swift
|
||||
return swift
|
||||
}
|
||||
@@ -138,43 +87,7 @@ public final class CSError: NSError, CoreStoreObjectiveCType {
|
||||
public init(_ swiftValue: CoreStoreError) {
|
||||
|
||||
self.swiftError = swiftValue
|
||||
|
||||
let code: CoreStoreErrorCode
|
||||
let info: [NSObject: AnyObject]
|
||||
switch swiftValue {
|
||||
|
||||
case .Unknown:
|
||||
code = .UnknownError
|
||||
info = [:]
|
||||
|
||||
case .DifferentStorageExistsAtURL(let existingPersistentStoreURL):
|
||||
code = .DifferentPersistentStoreExistsAtURL
|
||||
info = [
|
||||
"existingPersistentStoreURL": existingPersistentStoreURL
|
||||
]
|
||||
|
||||
case .MappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
|
||||
code = .MappingModelNotFound
|
||||
info = [
|
||||
"localStoreURL": localStoreURL,
|
||||
"targetModel": targetModel,
|
||||
"targetModelVersion": targetModelVersion
|
||||
]
|
||||
|
||||
case .ProgressiveMigrationRequired(let localStoreURL):
|
||||
code = .ProgressiveMigrationRequired
|
||||
info = [
|
||||
"localStoreURL": localStoreURL
|
||||
]
|
||||
|
||||
case .InternalError(let NSError):
|
||||
code = .InternalError
|
||||
info = [
|
||||
"NSError": NSError
|
||||
]
|
||||
}
|
||||
|
||||
super.init(domain: CoreStoreErrorDomain, code: code.rawValue, userInfo: info)
|
||||
super.init(domain: CoreStoreError.errorDomain, code: swiftValue.errorCode, userInfo: swiftValue.errorUserInfo)
|
||||
}
|
||||
|
||||
public required init?(coder aDecoder: NSCoder) {
|
||||
@@ -203,33 +116,33 @@ public enum CSErrorCode: Int {
|
||||
/**
|
||||
A failure occured because of an unknown error.
|
||||
*/
|
||||
case UnknownError
|
||||
case unknownError
|
||||
|
||||
/**
|
||||
The `NSPersistentStore` could note be initialized because another store existed at the specified `NSURL`.
|
||||
*/
|
||||
case DifferentPersistentStoreExistsAtURL
|
||||
case differentStorageExistsAtURL
|
||||
|
||||
/**
|
||||
An `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case MappingModelNotFound
|
||||
case mappingModelNotFound
|
||||
|
||||
/**
|
||||
Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions.
|
||||
*/
|
||||
case ProgressiveMigrationRequired
|
||||
case progressiveMigrationRequired
|
||||
|
||||
/**
|
||||
An internal SDK call failed with the specified "NSError" userInfo key.
|
||||
*/
|
||||
case InternalError
|
||||
case internalError
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CoreStoreError
|
||||
|
||||
extension CoreStoreError: CoreStoreSwiftType {
|
||||
extension CoreStoreError: CoreStoreSwiftType, _ObjectiveCBridgeableError {
|
||||
|
||||
// MARK: CoreStoreSwiftType
|
||||
|
||||
@@ -237,12 +150,79 @@ extension CoreStoreError: CoreStoreSwiftType {
|
||||
|
||||
return CSError(self)
|
||||
}
|
||||
|
||||
|
||||
// MARK: _ObjectiveCBridgeableError
|
||||
|
||||
public init?(_bridgedNSError error: NSError) {
|
||||
|
||||
guard error.domain == CoreStoreErrorDomain else {
|
||||
|
||||
if error is CSError {
|
||||
|
||||
self = .internalError(NSError: error)
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let code = CoreStoreErrorCode(rawValue: error.code) else {
|
||||
|
||||
if error is CSError {
|
||||
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let info = error.userInfo
|
||||
switch code {
|
||||
|
||||
case .unknownError:
|
||||
self = .unknown
|
||||
|
||||
case .differentStorageExistsAtURL:
|
||||
guard case let existingPersistentStoreURL as URL = info["existingPersistentStoreURL"] else {
|
||||
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
self = .differentStorageExistsAtURL(existingPersistentStoreURL: existingPersistentStoreURL)
|
||||
|
||||
case .mappingModelNotFound:
|
||||
guard let localStoreURL = info["localStoreURL"] as? URL,
|
||||
let targetModel = info["targetModel"] as? NSManagedObjectModel,
|
||||
let targetModelVersion = info["targetModelVersion"] as? String else {
|
||||
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
self = .mappingModelNotFound(localStoreURL: localStoreURL, targetModel: targetModel, targetModelVersion: targetModelVersion)
|
||||
|
||||
case .progressiveMigrationRequired:
|
||||
guard let localStoreURL = info["localStoreURL"] as? URL else {
|
||||
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
self = .progressiveMigrationRequired(localStoreURL: localStoreURL)
|
||||
|
||||
case .internalError:
|
||||
guard case let NSError as NSError = info["NSError"] else {
|
||||
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
self = .internalError(NSError: NSError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal extension ErrorType {
|
||||
internal extension Error {
|
||||
|
||||
internal var bridgeToSwift: CoreStoreError {
|
||||
|
||||
@@ -254,11 +234,11 @@ internal extension ErrorType {
|
||||
case let error as CSError:
|
||||
return error.bridgeToSwift
|
||||
|
||||
case let error as NSError where self.dynamicType is NSError.Type:
|
||||
return .InternalError(NSError: error)
|
||||
case let error as NSError where type(of: self) is NSError.Type:
|
||||
return .internalError(NSError: error)
|
||||
|
||||
default:
|
||||
return .Unknown
|
||||
return .unknown
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
|
||||
May contain `NSString` instances to pertain to named configurations, or `NSNull` to pertain to the default configuration
|
||||
*/
|
||||
@objc
|
||||
public var configurations: [AnyObject]? {
|
||||
public var configurations: [Any]? {
|
||||
|
||||
return self.bridgeToSwift.configurations?.map {
|
||||
|
||||
@@ -68,7 +68,6 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
|
||||
```
|
||||
MyPersonEntity *people = [transaction fetchAllFrom:CSFromClass([MyPersonEntity class])];
|
||||
```
|
||||
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
*/
|
||||
@objc
|
||||
@@ -83,11 +82,10 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
|
||||
MyPersonEntity *people = [transaction fetchAllFrom:
|
||||
CSFromClass([MyPersonEntity class], @"Config1")];
|
||||
```
|
||||
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `[NSNull null]` to use the default configuration.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(entityClass: AnyClass, configuration: AnyObject) {
|
||||
public convenience init(entityClass: AnyClass, configuration: Any) {
|
||||
|
||||
switch configuration {
|
||||
|
||||
@@ -109,12 +107,11 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
|
||||
CSFromClass([MyPersonEntity class],
|
||||
@[[NSNull null], @"Config1"])];
|
||||
```
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter configurations: an array of the `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `[NSNull null]` to use the default configuration.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(entityClass: AnyClass, configurations: [AnyObject]) {
|
||||
public convenience init(entityClass: AnyClass, configurations: [Any]) {
|
||||
|
||||
var arguments = [String?]()
|
||||
for configuration in configurations {
|
||||
@@ -139,7 +136,7 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSGroupBy else {
|
||||
|
||||
@@ -87,14 +87,14 @@ public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSQueryClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) {
|
||||
|
||||
self.bridgeToSwift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public final class CSInMemoryStore: NSObject, CSStorageInterface, CoreStoreObjec
|
||||
The options dictionary for the `NSPersistentStore`. For `CSInMemoryStore`s, this is always set to `nil`.
|
||||
*/
|
||||
@objc
|
||||
public var storeOptions: [String: AnyObject]? {
|
||||
public var storeOptions: [AnyHashable: Any]? {
|
||||
|
||||
return self.bridgeToSwift.storeOptions
|
||||
}
|
||||
@@ -92,7 +92,7 @@ public final class CSInMemoryStore: NSObject, CSStorageInterface, CoreStoreObjec
|
||||
return ObjectIdentifier(self.bridgeToSwift).hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSInMemoryStore else {
|
||||
|
||||
@@ -103,7 +103,7 @@ public final class CSInMemoryStore: NSObject, CSStorageInterface, CoreStoreObjec
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +62,6 @@ public final class CSInto: NSObject, CoreStoreObjectiveCType {
|
||||
MyPersonEntity *person = [transaction createInto:
|
||||
CSIntoClass([MyPersonEntity class])];
|
||||
```
|
||||
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
*/
|
||||
@objc
|
||||
@@ -77,7 +76,6 @@ public final class CSInto: NSObject, CoreStoreObjectiveCType {
|
||||
MyPersonEntity *person = [transaction createInto:
|
||||
CSIntoClass([MyPersonEntity class])];
|
||||
```
|
||||
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
@@ -95,7 +93,7 @@ public final class CSInto: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSInto else {
|
||||
|
||||
@@ -106,7 +104,7 @@ public final class CSInto: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified index
|
||||
*/
|
||||
@objc
|
||||
public subscript(index: Int) -> AnyObject {
|
||||
public subscript(index: Int) -> Any {
|
||||
|
||||
return self.bridgeToSwift[index]
|
||||
}
|
||||
@@ -60,7 +60,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified index, or `nil` if out of bounds
|
||||
*/
|
||||
@objc
|
||||
public func objectAtSafeIndex(index: Int) -> AnyObject? {
|
||||
public func objectAtSafeIndex(_ index: Int) -> Any? {
|
||||
|
||||
return self.bridgeToSwift[safeIndex: index]
|
||||
}
|
||||
@@ -73,12 +73,10 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified section and item index
|
||||
*/
|
||||
@objc
|
||||
public func objectAtSectionIndex(sectionIndex: Int, itemIndex: Int) -> AnyObject {
|
||||
public func objectAtSectionIndex(_ sectionIndex: Int, itemIndex: Int) -> Any {
|
||||
|
||||
return self.bridgeToSwift[sectionIndex, itemIndex]
|
||||
}
|
||||
|
||||
/**
|
||||
} /**
|
||||
Returns the object at the given section and item index, or `nil` if out of bounds. This indexer is typically used for `CSListMonitor`s created as sectioned lists.
|
||||
|
||||
- parameter sectionIndex: the section index for the object. Using a `sectionIndex` with an invalid range will return `nil`.
|
||||
@@ -86,7 +84,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified section and item index, or `nil` if out of bounds
|
||||
*/
|
||||
@objc
|
||||
public func objectAtSafeSectionIndex(sectionIndex: Int, safeItemIndex itemIndex: Int) -> AnyObject? {
|
||||
public func objectAtSafeSectionIndex(_ sectionIndex: Int, safeItemIndex itemIndex: Int) -> Any? {
|
||||
|
||||
return self.bridgeToSwift[safeSectionIndex: sectionIndex, safeItemIndex: itemIndex]
|
||||
}
|
||||
@@ -98,7 +96,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified index path
|
||||
*/
|
||||
@objc
|
||||
public func objectAtIndexPath(indexPath: NSIndexPath) -> AnyObject {
|
||||
public func objectAtIndexPath(_ indexPath: IndexPath) -> Any {
|
||||
|
||||
return self.bridgeToSwift[indexPath]
|
||||
}
|
||||
@@ -110,7 +108,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSManagedObject` at the specified index path, or `nil` if out of bounds
|
||||
*/
|
||||
@objc
|
||||
public func objectAtSafeIndexPath(indexPath: NSIndexPath) -> AnyObject? {
|
||||
public func objectAtSafeIndexPath(_ indexPath: IndexPath) -> Any? {
|
||||
|
||||
return self.bridgeToSwift[safeIndexPath: indexPath]
|
||||
}
|
||||
@@ -121,7 +119,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: `YES` if at least one object in any section exists, `NO` otherwise
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func hasObjects() -> Bool {
|
||||
|
||||
return self.bridgeToSwift.hasObjects()
|
||||
@@ -134,8 +131,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: `YES` if at least one object in the specified section exists, `NO` otherwise
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func hasObjectsInSection(section: Int) -> Bool {
|
||||
public func hasObjectsInSection(_ section: Int) -> Bool {
|
||||
|
||||
return self.bridgeToSwift.hasObjectsInSection(section)
|
||||
}
|
||||
@@ -146,7 +142,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: all objects in all sections
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func objectsInAllSections() -> [NSManagedObject] {
|
||||
|
||||
return self.bridgeToSwift.objectsInAllSections()
|
||||
@@ -159,8 +154,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: all objects in the specified section
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func objectsInSection(section: Int) -> [NSManagedObject] {
|
||||
public func objectsInSection(_ section: Int) -> [NSManagedObject] {
|
||||
|
||||
return self.bridgeToSwift.objectsInSection(section)
|
||||
}
|
||||
@@ -172,7 +166,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: all objects in the specified section, or `nil` if out of bounds
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func objectsInSafeSection(safeSectionIndex section: Int) -> [NSManagedObject]? {
|
||||
|
||||
return self.bridgeToSwift.objectsInSection(safeSectionIndex: section)
|
||||
@@ -184,7 +177,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the number of sections
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func numberOfSections() -> Int {
|
||||
|
||||
return self.bridgeToSwift.numberOfSections()
|
||||
@@ -196,7 +188,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the number of objects in all sections
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func numberOfObjects() -> Int {
|
||||
|
||||
return self.bridgeToSwift.numberOfObjects()
|
||||
@@ -209,8 +200,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the number of objects in the specified section
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func numberOfObjectsInSection(section: Int) -> Int {
|
||||
public func numberOfObjectsInSection(_ section: Int) -> Int {
|
||||
|
||||
return self.bridgeToSwift.numberOfObjectsInSection(section)
|
||||
}
|
||||
@@ -222,10 +212,11 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the number of objects in the specified section, or `nil` if out of bounds
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func numberOfObjectsInSafeSection(safeSectionIndex section: Int) -> NSNumber? {
|
||||
|
||||
return self.bridgeToSwift.numberOfObjectsInSection(safeSectionIndex: section)
|
||||
return self.bridgeToSwift
|
||||
.numberOfObjectsInSection(safeSectionIndex: section)
|
||||
.flatMap { NSNumber(value: $0) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -235,8 +226,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSFetchedResultsSectionInfo` for the specified section
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func sectionInfoAtIndex(section: Int) -> NSFetchedResultsSectionInfo {
|
||||
public func sectionInfoAtIndex(_ section: Int) -> NSFetchedResultsSectionInfo {
|
||||
|
||||
return self.bridgeToSwift.sectionInfoAtIndex(section)
|
||||
}
|
||||
@@ -248,7 +238,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSFetchedResultsSectionInfo` for the specified section, or `nil` if the section index is out of bounds.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func sectionInfoAtSafeSectionIndex(safeSectionIndex section: Int) -> NSFetchedResultsSectionInfo? {
|
||||
|
||||
return self.bridgeToSwift.sectionInfoAtIndex(safeSectionIndex: section)
|
||||
@@ -260,7 +249,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSFetchedResultsSectionInfo`s for all sections
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func sections() -> [NSFetchedResultsSectionInfo] {
|
||||
|
||||
return self.bridgeToSwift.sections()
|
||||
@@ -274,8 +262,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the target section for the specified "Section Index" title and index.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func targetSectionForSectionIndexTitle(title title: String, index: Int) -> Int {
|
||||
public func targetSectionForSectionIndexTitle(title: String, index: Int) -> Int {
|
||||
|
||||
return self.bridgeToSwift.targetSectionForSectionIndex(title: title, index: index)
|
||||
}
|
||||
@@ -286,7 +273,6 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the section index titles for all sections
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func sectionIndexTitles() -> [String] {
|
||||
|
||||
return self.bridgeToSwift.sectionIndexTitles()
|
||||
@@ -299,10 +285,11 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the index of the `NSManagedObject` if it exists in the `CSListMonitor`'s fetched objects, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func indexOf(object: NSManagedObject) -> NSNumber? {
|
||||
public func indexOf(_ object: NSManagedObject) -> NSNumber? {
|
||||
|
||||
return self.bridgeToSwift.indexOf(object)
|
||||
return self.bridgeToSwift
|
||||
.indexOf(object)
|
||||
.flatMap { NSNumber(value: $0) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -312,8 +299,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: the `NSIndexPath` of the `NSManagedObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func indexPathOf(object: NSManagedObject) -> NSIndexPath? {
|
||||
public func indexPathOf(_ object: NSManagedObject) -> IndexPath? {
|
||||
|
||||
return self.bridgeToSwift.indexPathOf(object)
|
||||
}
|
||||
@@ -333,7 +319,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter observer: a `CSListObserver` to send change notifications to
|
||||
*/
|
||||
@objc
|
||||
public func addListObserver(observer: CSListObserver) {
|
||||
public func addListObserver(_ observer: CSListObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
@@ -369,7 +355,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
- parameter observer: a `CSListObjectObserver` to send change notifications to
|
||||
*/
|
||||
public func addListObjectObserver(observer: CSListObjectObserver) {
|
||||
public func addListObjectObserver(_ observer: CSListObjectObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
@@ -425,7 +411,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter observer: a `CSListSectionObserver` to send change notifications to
|
||||
*/
|
||||
@objc
|
||||
public func addListSectionObserver(observer: CSListSectionObserver) {
|
||||
public func addListSectionObserver(_ observer: CSListSectionObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
@@ -488,7 +474,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter observer: a `CSListObserver` to unregister notifications to
|
||||
*/
|
||||
@objc
|
||||
public func removeListObserver(observer: CSListObserver) {
|
||||
public func removeListObserver(_ observer: CSListObserver) {
|
||||
|
||||
self.bridgeToSwift.unregisterObserver(observer)
|
||||
}
|
||||
@@ -513,11 +499,11 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. Note that only specified clauses will be changed; unspecified clauses will use previous values.
|
||||
*/
|
||||
@objc
|
||||
public func refetch(fetchClauses: [CSFetchClause]) {
|
||||
public func refetch(_ fetchClauses: [CSFetchClause]) {
|
||||
|
||||
self.bridgeToSwift.refetch { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,7 +515,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSListMonitor else {
|
||||
|
||||
@@ -540,7 +526,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public protocol CSListObserver: class, AnyObject {
|
||||
- parameter monitor: the `CSListMonitor` monitoring the list being observed
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitorWillChange(monitor: CSListMonitor)
|
||||
optional func listMonitorWillChange(_ monitor: CSListMonitor)
|
||||
|
||||
/**
|
||||
Handles processing right after a change to the observed list occurs
|
||||
@@ -59,7 +59,7 @@ public protocol CSListObserver: class, AnyObject {
|
||||
- parameter monitor: the `CSListMonitor` monitoring the object being observed
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitorDidChange(monitor: CSListMonitor)
|
||||
optional func listMonitorDidChange(_ monitor: CSListMonitor)
|
||||
|
||||
/**
|
||||
This method is broadcast from within the `CSListMonitor`'s `-refetchWithFetchClauses:` method to let observers prepare for the internal `NSFetchedResultsController`'s pending change to its predicate, sort descriptors, etc. Note that the actual refetch will happen after the `NSFetchedResultsController`'s last `-controllerDidChangeContent:` notification completes.
|
||||
@@ -67,7 +67,7 @@ public protocol CSListObserver: class, AnyObject {
|
||||
- parameter monitor: the `CSListMonitor` monitoring the object being observed
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitorWillRefetch(monitor: CSListMonitor)
|
||||
optional func listMonitorWillRefetch(_ monitor: CSListMonitor)
|
||||
|
||||
/**
|
||||
After the `CSListMonitor`'s `-refetchWithFetchClauses:` method is called, this method is broadcast after the `NSFetchedResultsController`'s last `-controllerDidChangeContent:` notification completes.
|
||||
@@ -75,7 +75,7 @@ public protocol CSListObserver: class, AnyObject {
|
||||
- parameter monitor: the `CSListMonitor` monitoring the object being observed
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitorDidRefetch(monitor: CSListMonitor)
|
||||
optional func listMonitorDidRefetch(_ monitor: CSListMonitor)
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ public protocol CSListObjectObserver: CSListObserver {
|
||||
- parameter indexPath: the new `NSIndexPath` for the inserted object
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didInsertObject object: AnyObject, toIndexPath indexPath: NSIndexPath)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didInsertObject object: Any, toIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object was deleted from the specified `NSIndexPath` in the list
|
||||
@@ -113,7 +113,7 @@ public protocol CSListObjectObserver: CSListObserver {
|
||||
- parameter indexPath: the `NSIndexPath` for the deleted object
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didDeleteObject object: AnyObject, fromIndexPath indexPath: NSIndexPath)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didDeleteObject object: Any, fromIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object at the specified `NSIndexPath` was updated
|
||||
@@ -123,7 +123,7 @@ public protocol CSListObjectObserver: CSListObserver {
|
||||
- parameter indexPath: the `NSIndexPath` for the updated object
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didUpdateObject object: AnyObject, atIndexPath indexPath: NSIndexPath)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didUpdateObject object: Any, atIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object's index changed
|
||||
@@ -134,7 +134,7 @@ public protocol CSListObjectObserver: CSListObserver {
|
||||
- parameter toIndexPath: the new `NSIndexPath` for the moved object
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didMoveObject object: AnyObject, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didMoveObject object: Any, fromIndexPath: IndexPath, toIndexPath: IndexPath)
|
||||
}
|
||||
|
||||
|
||||
@@ -163,7 +163,7 @@ public protocol CSListSectionObserver: CSListObjectObserver {
|
||||
- parameter sectionIndex: the new section index for the new section
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int)
|
||||
|
||||
/**
|
||||
Notifies that a section was inserted at the specified index
|
||||
@@ -173,7 +173,7 @@ public protocol CSListSectionObserver: CSListObjectObserver {
|
||||
- parameter sectionIndex: the previous section index for the deleted section
|
||||
*/
|
||||
@objc
|
||||
optional func listMonitor(monitor: CSListMonitor, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int)
|
||||
optional func listMonitor(_ monitor: CSListMonitor, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var isSuccess: Bool {
|
||||
|
||||
return self.bridgeToSwift.boolValue
|
||||
return self.bridgeToSwift.isSuccess
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +52,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var isFailure: Bool {
|
||||
|
||||
return !self.bridgeToSwift.boolValue
|
||||
return !self.bridgeToSwift.isSuccess
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +61,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var migrationTypes: [CSMigrationType]? {
|
||||
|
||||
guard case .Success(let migrationTypes) = self.bridgeToSwift else {
|
||||
guard case .success(let migrationTypes) = self.bridgeToSwift else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var error: NSError? {
|
||||
|
||||
guard case .Failure(let error) = self.bridgeToSwift else {
|
||||
guard case .failure(let error) = self.bridgeToSwift else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -90,15 +90,15 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleSuccess(@noescape success: (migrationTypes: [CSMigrationType]) -> Void, @noescape failure: (error: NSError) -> Void) {
|
||||
public func handleSuccess(_ success: (_ migrationTypes: [CSMigrationType]) -> Void, failure: (_ error: NSError) -> Void) {
|
||||
|
||||
switch self.bridgeToSwift {
|
||||
|
||||
case .Success(let migrationTypes):
|
||||
success(migrationTypes: migrationTypes.map { $0.bridgeToObjectiveC })
|
||||
case .success(let migrationTypes):
|
||||
success(migrationTypes.map { $0.bridgeToObjectiveC })
|
||||
|
||||
case .Failure(let error):
|
||||
failure(error: error.bridgeToObjectiveC)
|
||||
case .failure(let error):
|
||||
failure(error.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,13 +110,13 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter success: the block to execute on success. The block passes an array of `CSMigrationType`s that indicates the migration steps completed.
|
||||
*/
|
||||
@objc
|
||||
public func handleSuccess(@noescape success: (migrationTypes: [CSMigrationType]) -> Void) {
|
||||
public func handleSuccess(_ success: (_ migrationTypes: [CSMigrationType]) -> Void) {
|
||||
|
||||
guard case .Success(let migrationTypes) = self.bridgeToSwift else {
|
||||
guard case .success(let migrationTypes) = self.bridgeToSwift else {
|
||||
|
||||
return
|
||||
}
|
||||
success(migrationTypes: migrationTypes.map { $0.bridgeToObjectiveC })
|
||||
success(migrationTypes.map { $0.bridgeToObjectiveC })
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,13 +127,13 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleFailure(@noescape failure: (error: NSError) -> Void) {
|
||||
public func handleFailure(_ failure: (_ error: NSError) -> Void) {
|
||||
|
||||
guard case .Failure(let error) = self.bridgeToSwift else {
|
||||
guard case .failure(let error) = self.bridgeToSwift else {
|
||||
|
||||
return
|
||||
}
|
||||
failure(error: error.bridgeToObjectiveC)
|
||||
failure(error.bridgeToObjectiveC)
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSMigrationResult else {
|
||||
|
||||
@@ -155,7 +155,7 @@ public final class CSMigrationResult: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class CSMigrationType: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var needsMigration: Bool {
|
||||
|
||||
return self.bridgeToSwift.boolValue
|
||||
return self.bridgeToSwift.hasMigration
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +90,7 @@ public final class CSMigrationType: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSMigrationType else {
|
||||
|
||||
@@ -101,7 +101,7 @@ public final class CSMigrationType: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
/**
|
||||
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
|
||||
*/
|
||||
public var object: AnyObject? {
|
||||
public var object: Any? {
|
||||
|
||||
return self.bridgeToSwift.object
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to send change notifications to
|
||||
*/
|
||||
public func addObjectObserver(observer: CSObjectObserver) {
|
||||
public func addObjectObserver(_ observer: CSObjectObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
@@ -94,7 +94,7 @@ public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to unregister notifications to
|
||||
*/
|
||||
public func removeObjectObserver(observer: CSObjectObserver) {
|
||||
public func removeObjectObserver(_ observer: CSObjectObserver) {
|
||||
|
||||
self.bridgeToSwift.unregisterObserver(observer)
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSObjectMonitor else {
|
||||
|
||||
@@ -118,7 +118,7 @@ public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ public protocol CSObjectObserver: class, AnyObject {
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, willUpdateObject object: AnyObject)
|
||||
optional func objectMonitor(_ monitor: CSObjectMonitor, willUpdateObject object: Any)
|
||||
|
||||
/**
|
||||
Handles processing right after a change to the observed `object` occurs
|
||||
@@ -60,7 +60,7 @@ public protocol CSObjectObserver: class, AnyObject {
|
||||
- parameter changedPersistentKeys: an `NSSet` of key paths for the attributes that were changed. Note that `changedPersistentKeys` only contains keys for attributes/relationships present in the persistent store, thus transient properties will not be reported.
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, didUpdateObject object: AnyObject, changedPersistentKeys: Set<String>)
|
||||
optional func objectMonitor(_ monitor: CSObjectMonitor, didUpdateObject object: Any, changedPersistentKeys: Set<String>)
|
||||
|
||||
/**
|
||||
Handles processing right after `object` is deleted
|
||||
@@ -69,7 +69,7 @@ public protocol CSObjectObserver: class, AnyObject {
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, didDeleteObject object: AnyObject)
|
||||
optional func objectMonitor(_ monitor: CSObjectMonitor, didDeleteObject object: Any)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -53,7 +53,6 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
|
||||
fetchAllFrom:CSFromClass([MyPersonEntity class])
|
||||
fetchClauses:@[CSOrderByKey(CSSortAscending(@"fullname"))]]];
|
||||
```
|
||||
|
||||
- parameter sortDescriptor: a `NSSortDescriptor`
|
||||
*/
|
||||
@objc
|
||||
@@ -69,7 +68,6 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
|
||||
fetchAllFrom:CSFromClass([MyPersonEntity class])
|
||||
fetchClauses:@[CSOrderByKeys(CSSortAscending(@"fullname"), CSSortDescending(@"age"), nil))]]];
|
||||
```
|
||||
|
||||
- parameter sortDescriptors: an array of `NSSortDescriptor`s
|
||||
*/
|
||||
@objc
|
||||
@@ -86,7 +84,7 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSOrderBy else {
|
||||
|
||||
@@ -97,14 +95,14 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) {
|
||||
|
||||
self.bridgeToSwift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
@@ -46,13 +46,13 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `CSLocalStorageOptionsNone`.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(fileURL: NSURL, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: Int) {
|
||||
public convenience init(fileURL: URL, configuration: String?, mappingModelBundles: [Bundle]?, localStorageOptions: Int) {
|
||||
|
||||
self.init(
|
||||
SQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
mappingModelBundles: mappingModelBundles ?? Bundle.allBundles,
|
||||
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions)
|
||||
)
|
||||
)
|
||||
@@ -68,13 +68,13 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `[CSLocalStorageOptions none]`.
|
||||
*/
|
||||
@objc
|
||||
public convenience init(fileName: String, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: Int) {
|
||||
public convenience init(fileName: String, configuration: String?, mappingModelBundles: [Bundle]?, localStorageOptions: Int) {
|
||||
|
||||
self.init(
|
||||
SQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
|
||||
mappingModelBundles: mappingModelBundles ?? Bundle.allBundles,
|
||||
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions)
|
||||
)
|
||||
)
|
||||
@@ -98,16 +98,16 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
The `NSURL` that points to the SQLite file
|
||||
*/
|
||||
@objc
|
||||
public var fileURL: NSURL {
|
||||
public var fileURL: URL {
|
||||
|
||||
return self.bridgeToSwift.fileURL
|
||||
return self.bridgeToSwift.fileURL as URL
|
||||
}
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
@objc
|
||||
public var mappingModelBundles: [NSBundle] {
|
||||
public var mappingModelBundles: [Bundle] {
|
||||
|
||||
return self.bridgeToSwift.mappingModelBundles
|
||||
}
|
||||
@@ -145,7 +145,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
```
|
||||
*/
|
||||
@objc
|
||||
public var storeOptions: [String: AnyObject]? {
|
||||
public var storeOptions: [AnyHashable: Any]? {
|
||||
|
||||
return self.bridgeToSwift.storeOptions
|
||||
}
|
||||
@@ -154,11 +154,11 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `CSSQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
|
||||
*/
|
||||
@objc
|
||||
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?, error: NSErrorPointer) -> Bool {
|
||||
public func eraseStorageAndWait(metadata: NSDictionary, soureModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool {
|
||||
|
||||
return bridge(error) {
|
||||
|
||||
try self.bridgeToSwift.eraseStorageAndWait(soureModel: soureModel)
|
||||
try self.bridgeToSwift.eraseStorageAndWait(metadata: metadata as! [String: Any], soureModelHint: soureModelHint)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
return ObjectIdentifier(self.bridgeToSwift).hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSQLiteStore else {
|
||||
|
||||
@@ -181,7 +181,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var hasChanges: Bool {
|
||||
|
||||
guard case .Success(let hasChanges) = self.bridgeToSwift else {
|
||||
guard case .success(let hasChanges) = self.bridgeToSwift else {
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
@objc
|
||||
public var error: NSError? {
|
||||
|
||||
guard case .Failure(let error) = self.bridgeToSwift else {
|
||||
guard case .failure(let error) = self.bridgeToSwift else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -90,15 +90,15 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleSuccess(@noescape success: (hasChanges: Bool) -> Void, @noescape failure: (error: NSError) -> Void) {
|
||||
public func handleSuccess(_ success: (_ hasChanges: Bool) -> Void, failure: (_ error: NSError) -> Void) {
|
||||
|
||||
switch self.bridgeToSwift {
|
||||
|
||||
case .Success(let hasChanges):
|
||||
success(hasChanges: hasChanges)
|
||||
case .success(let hasChanges):
|
||||
success(hasChanges)
|
||||
|
||||
case .Failure(let error):
|
||||
failure(error: error.bridgeToObjectiveC)
|
||||
case .failure(let error):
|
||||
failure(error.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,13 +110,13 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
- 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) {
|
||||
public func handleSuccess(_ success: (_ hasChanges: Bool) -> Void) {
|
||||
|
||||
guard case .Success(let hasChanges) = self.bridgeToSwift else {
|
||||
guard case .success(let hasChanges) = self.bridgeToSwift else {
|
||||
|
||||
return
|
||||
}
|
||||
success(hasChanges: hasChanges)
|
||||
success(hasChanges)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,13 +127,13 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleFailure(@noescape failure: (error: NSError) -> Void) {
|
||||
public func handleFailure(_ failure: (_ error: NSError) -> Void) {
|
||||
|
||||
guard case .Failure(let error) = self.bridgeToSwift else {
|
||||
guard case .failure(let error) = self.bridgeToSwift else {
|
||||
|
||||
return
|
||||
}
|
||||
failure(error: error.bridgeToObjectiveC)
|
||||
failure(error.bridgeToObjectiveC)
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSaveResult else {
|
||||
|
||||
@@ -155,7 +155,7 @@ public final class CSSaveResult: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public final class CSSectionBy: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: a `CSSectionBy` clause with the key path to use to group `CSListMonitor` objects into sections
|
||||
*/
|
||||
@objc
|
||||
public static func keyPath(sectionKeyPath: KeyPath) -> CSSectionBy {
|
||||
public static func keyPath(_ sectionKeyPath: KeyPath) -> CSSectionBy {
|
||||
|
||||
return self.init(SectionBy(sectionKeyPath))
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public final class CSSectionBy: NSObject, CoreStoreObjectiveCType {
|
||||
- returns: a `CSSectionBy` clause with the key path to use to group `CSListMonitor` objects into sections
|
||||
*/
|
||||
@objc
|
||||
public static func keyPath(sectionKeyPath: KeyPath, sectionIndexTransformer: (sectionName: String?) -> String?) -> CSSectionBy {
|
||||
public static func keyPath(_ sectionKeyPath: KeyPath, sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> CSSectionBy {
|
||||
|
||||
return self.init(SectionBy(sectionKeyPath, sectionIndexTransformer))
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public final class CSSectionBy: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -45,13 +45,12 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
select:CSSelectString(CSAttribute(@"fullname"))
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
*/
|
||||
@objc
|
||||
public convenience init(keyPath: KeyPath) {
|
||||
|
||||
self.init(.Attribute(keyPath))
|
||||
self.init(.attribute(keyPath))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,15 +60,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm average:@"age" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter `as`: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "average(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the average value of an attribute
|
||||
*/
|
||||
@objc
|
||||
public static func average(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
public static func average(_ keyPath: KeyPath, as alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Average(keyPath, As: alias))
|
||||
return self.init(.average(keyPath, as: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,15 +77,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm count:@"employeeID" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "count(<attributeName>)" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for a count query
|
||||
*/
|
||||
@objc
|
||||
public static func count(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
public static func count(_ keyPath: KeyPath, as alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Count(keyPath, As: alias))
|
||||
return self.init(.count(keyPath, as: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,15 +94,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm maximum:@"age" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "max(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the maximum value for an attribute
|
||||
*/
|
||||
@objc
|
||||
public static func maximum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
public static func maximum(_ keyPath: KeyPath, as alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Maximum(keyPath, As: alias))
|
||||
return self.init(.maximum(keyPath, as: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,15 +111,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm minimum:@"age" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "min(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the minimum value for an attribute
|
||||
*/
|
||||
@objc
|
||||
public static func minimum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
public static func minimum(_ keyPath: KeyPath, as alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Minimum(keyPath, As: alias))
|
||||
return self.init(.minimum(keyPath, as: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,15 +128,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect numberForTerm:[CSSelectTerm sum:@"age" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "sum(<attributeName>)" is used
|
||||
- returns: a `CSSelectTerm` to a `CSSelect` clause for querying the sum value for an attribute
|
||||
*/
|
||||
@objc
|
||||
public static func sum(keyPath: KeyPath, `as` alias: KeyPath?) -> CSSelectTerm {
|
||||
public static func sum(_ keyPath: KeyPath, as alias: KeyPath?) -> CSSelectTerm {
|
||||
|
||||
return self.init(.Sum(keyPath, As: alias))
|
||||
return self.init(.sum(keyPath, as: alias))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,15 +146,14 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
select:[CSSelect objectIDForTerm:[CSSelectTerm objectIDAs:nil]]
|
||||
fetchClauses:@[[CSWhere keyPath:@"employeeID" isEqualTo: @1111]]];
|
||||
```
|
||||
|
||||
- parameter keyPath: the attribute name
|
||||
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "objecID" is used
|
||||
- returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute
|
||||
*/
|
||||
@objc
|
||||
public static func objectIDAs(alias: KeyPath? = nil) -> CSSelectTerm {
|
||||
public static func objectIDAs(_ alias: KeyPath? = nil) -> CSSelectTerm {
|
||||
|
||||
return self.init(.ObjectID(As: alias))
|
||||
return self.init(.objectID(as: alias))
|
||||
}
|
||||
|
||||
|
||||
@@ -171,7 +164,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType {
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSelectTerm else {
|
||||
|
||||
@@ -221,7 +214,6 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectNumber(CSAggregateMax(@"age"))
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(numberTerm: CSSelectTerm) {
|
||||
@@ -237,7 +229,6 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectDecimal(CSAggregateAverage(@"price"))
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(decimalTerm: CSSelectTerm) {
|
||||
@@ -253,7 +244,6 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectString(CSAttribute(@"fullname"))
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(stringTerm: CSSelectTerm) {
|
||||
@@ -269,12 +259,11 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectDate(CSAggregateMax(@"updatedDate"))
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(dateTerm: CSSelectTerm) {
|
||||
|
||||
self.init(Select<NSDate>(dateTerm.bridgeToSwift))
|
||||
self.init(Select<Date>(dateTerm.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -285,12 +274,11 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectData(CSAttribute(@"imageData"))
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(dataTerm: CSSelectTerm) {
|
||||
|
||||
self.init(Select<NSData>(dataTerm.bridgeToSwift))
|
||||
self.init(Select<Data>(dataTerm.bridgeToSwift))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,12 +289,11 @@ public final class CSSelect: NSObject {
|
||||
select:CSSelectObjectID()
|
||||
// ...
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
*/
|
||||
public convenience init(objectIDTerm: ()) {
|
||||
|
||||
self.init(Select<NSManagedObjectID>(.ObjectID()))
|
||||
self.init(Select<NSManagedObjectID>(.objectID()))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,11 +303,10 @@ public final class CSSelect: NSObject {
|
||||
queryValueFrom:[CSFrom entityClass:[MyPersonEntity class]]
|
||||
select:[CSSelect dictionaryForTerm:[CSSelectTerm maximum:@"age" as:nil]]];
|
||||
```
|
||||
|
||||
- parameter term: the `CSSelectTerm` specifying the attribute/aggregate value to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func dictionaryForTerm(term: CSSelectTerm) -> CSSelect {
|
||||
public static func dictionaryForTerm(_ term: CSSelectTerm) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDictionary>(term.bridgeToSwift))
|
||||
}
|
||||
@@ -335,11 +321,10 @@ public final class CSSelect: NSObject {
|
||||
[CSSelectTerm attribute:@"age" as:nil]
|
||||
]]];
|
||||
```
|
||||
|
||||
- parameter terms: the `CSSelectTerm`s specifying the attribute/aggregate values to query
|
||||
- returns: a `CSSelect` clause for querying an entity attribute
|
||||
*/
|
||||
public static func dictionaryForTerms(terms: [CSSelectTerm]) -> CSSelect {
|
||||
public static func dictionaryForTerms(_ terms: [CSSelectTerm]) -> CSSelect {
|
||||
|
||||
return self.init(Select<NSDictionary>(terms.map { $0.bridgeToSwift }))
|
||||
}
|
||||
@@ -350,10 +335,10 @@ public final class CSSelect: NSObject {
|
||||
public override var hash: Int {
|
||||
|
||||
return self.attributeType.hashValue
|
||||
^ self.selectTerms.map { $0.hashValue }.reduce(0, combine: ^)
|
||||
^ self.selectTerms.map { $0.hashValue }.reduce(0, ^)
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSelect else {
|
||||
|
||||
@@ -365,7 +350,7 @@ public final class CSSelect: NSObject {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
@@ -381,7 +366,7 @@ public final class CSSelect: NSObject {
|
||||
|
||||
public init<T: SelectResultType>(_ swiftValue: Select<T>) {
|
||||
|
||||
self.attributeType = .UndefinedAttributeType
|
||||
self.attributeType = .undefinedAttributeType
|
||||
self.selectTerms = swiftValue.selectTerms
|
||||
self.bridgeToSwift = swiftValue
|
||||
super.init()
|
||||
|
||||
@@ -76,15 +76,15 @@ public final class CSSetupResult: NSObject {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleSuccess(@noescape success: (storage: CSStorageInterface) -> Void, @noescape failure: (error: NSError) -> Void) {
|
||||
public func handleSuccess(_ success: (_ storage: CSStorageInterface) -> Void, failure: (_ error: NSError) -> Void) {
|
||||
|
||||
if let storage = self.storage {
|
||||
|
||||
success(storage: storage)
|
||||
success(storage)
|
||||
}
|
||||
else {
|
||||
|
||||
failure(error: self.error!)
|
||||
failure(self.error!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,13 +96,13 @@ public final class CSSetupResult: NSObject {
|
||||
- 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: (storage: CSStorageInterface) -> Void) {
|
||||
public func handleSuccess(_ success: (_ storage: CSStorageInterface) -> Void) {
|
||||
|
||||
guard let storage = self.storage else {
|
||||
|
||||
return
|
||||
}
|
||||
success(storage: storage)
|
||||
success(storage)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,13 +113,13 @@ public final class CSSetupResult: NSObject {
|
||||
- parameter failure: the block to execute on failure. The block passes an `NSError` argument that pertains to the actual error.
|
||||
*/
|
||||
@objc
|
||||
public func handleFailure(@noescape failure: (error: NSError) -> Void) {
|
||||
public func handleFailure(_ failure: (_ error: NSError) -> Void) {
|
||||
|
||||
guard let error = self.error else {
|
||||
|
||||
return
|
||||
}
|
||||
failure(error: error)
|
||||
failure(error)
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ public final class CSSetupResult: NSObject {
|
||||
return self.isSuccess.hashValue ^ self.error!.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSSetupResult else {
|
||||
|
||||
@@ -146,21 +146,21 @@ public final class CSSetupResult: NSObject {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreObjectiveCType
|
||||
|
||||
public required init<T: StorageInterface where T: CoreStoreSwiftType, T.ObjectiveCType: CSStorageInterface>(_ swiftValue: SetupResult<T>) {
|
||||
public required init<T: StorageInterface>(_ swiftValue: SetupResult<T>) where T: CoreStoreSwiftType, T.ObjectiveCType: CSStorageInterface {
|
||||
|
||||
switch swiftValue {
|
||||
|
||||
case .Success(let storage):
|
||||
case .success(let storage):
|
||||
self.storage = storage.bridgeToObjectiveC
|
||||
self.error = nil
|
||||
|
||||
case .Failure(let error):
|
||||
case .failure(let error):
|
||||
self.storage = nil
|
||||
self.error = error.bridgeToObjectiveC
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public protocol CSStorageInterface {
|
||||
The options dictionary for the `NSPersistentStore`
|
||||
*/
|
||||
@objc
|
||||
var storeOptions: [String: AnyObject]? { get }
|
||||
var storeOptions: [AnyHashable: Any]? { get }
|
||||
}
|
||||
|
||||
|
||||
@@ -70,22 +70,22 @@ public enum CSLocalStorageOptions: Int {
|
||||
/**
|
||||
Tells the `DataStack` that the store should not be migrated or recreated, and should simply fail on model mismatch
|
||||
*/
|
||||
case None = 0
|
||||
case none = 0
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to delete and recreate the store on model mismatch, otherwise exceptions will be thrown on failure instead
|
||||
*/
|
||||
case RecreateStoreOnModelMismatch = 1
|
||||
case recreateStoreOnModelMismatch = 1
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to prevent progressive migrations for the store
|
||||
*/
|
||||
case PreventProgressiveMigration = 2
|
||||
case preventProgressiveMigration = 2
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to allow lightweight migration for the store when added synchronously
|
||||
*/
|
||||
case AllowSynchronousLightweightMigration = 4
|
||||
case allowSynchronousLightweightMigration = 4
|
||||
}
|
||||
|
||||
|
||||
@@ -103,13 +103,13 @@ public protocol CSLocalStorage: CSStorageInterface {
|
||||
The `NSURL` that points to the store file
|
||||
*/
|
||||
@objc
|
||||
var fileURL: NSURL { get }
|
||||
var fileURL: URL { get }
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
@objc
|
||||
var mappingModelBundles: [NSBundle] { get }
|
||||
var mappingModelBundles: [Bundle] { get }
|
||||
|
||||
/**
|
||||
Options that tell the `CSDataStack` how to setup the persistent store
|
||||
@@ -121,5 +121,5 @@ public protocol CSLocalStorage: CSStorageInterface {
|
||||
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. Implementers can use the `sourceModel` to perform necessary store operations. (SQLite stores for example, can convert WAL journaling mode to DELETE before deleting)
|
||||
*/
|
||||
@objc
|
||||
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?, error: NSErrorPointer) -> Bool
|
||||
func eraseStorageAndWait(metadata: NSDictionary, soureModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool
|
||||
}
|
||||
|
||||
@@ -58,13 +58,14 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- 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? {
|
||||
@discardableResult
|
||||
public func beginSynchronous(_ closure: @escaping (_ transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.bridgeToSwift.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.bridgeToObjectiveC)
|
||||
closure(transaction.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +75,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +88,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
@objc
|
||||
public override func createInto(into: CSInto) -> AnyObject {
|
||||
public override func createInto(_ into: CSInto) -> Any {
|
||||
|
||||
return self.bridgeToSwift.create(into.bridgeToSwift)
|
||||
}
|
||||
@@ -99,8 +100,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public override func editObject(object: NSManagedObject?) -> AnyObject? {
|
||||
public override func editObject(_ object: NSManagedObject?) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(object)
|
||||
}
|
||||
@@ -113,8 +113,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- returns: an editable proxy for the specified `NSManagedObject`.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public override func editInto(into: CSInto, objectID: NSManagedObjectID) -> AnyObject? {
|
||||
public override func editInto(_ into: CSInto, objectID: NSManagedObjectID) -> Any? {
|
||||
|
||||
return self.bridgeToSwift.edit(into.bridgeToSwift, objectID)
|
||||
}
|
||||
@@ -125,7 +124,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
- parameter object: the `NSManagedObject` type to be deleted
|
||||
*/
|
||||
@objc
|
||||
public override func deleteObject(object: NSManagedObject?) {
|
||||
public override func deleteObject(_ object: NSManagedObject?) {
|
||||
|
||||
return self.bridgeToSwift.delete(object)
|
||||
}
|
||||
@@ -135,7 +134,7 @@ public final class CSSynchronousDataTransaction: CSBaseDataTransaction {
|
||||
|
||||
- parameter objects: the `NSManagedObject`s to be deleted
|
||||
*/
|
||||
public override func deleteObjects(objects: [NSManagedObject]) {
|
||||
public override func deleteObjects(_ objects: [NSManagedObject]) {
|
||||
|
||||
self.bridgeToSwift.delete(objects)
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public final class CSTweak: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
The block to customize the `NSFetchRequest`
|
||||
*/
|
||||
@objc
|
||||
public var block: (fetchRequest: NSFetchRequest) -> Void {
|
||||
public var block: (_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Void {
|
||||
|
||||
return self.bridgeToSwift.closure
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public final class CSTweak: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
- parameter block: the block to customize the `NSFetchRequest`
|
||||
*/
|
||||
@objc
|
||||
public convenience init(block: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
public convenience init(block: @escaping (_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) -> Void) {
|
||||
|
||||
self.init(Tweak(block))
|
||||
}
|
||||
@@ -63,14 +63,14 @@ public final class CSTweak: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) {
|
||||
|
||||
self.bridgeToSwift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
@@ -43,11 +43,11 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
|
||||
- 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 commit(_ completion: ((_ result: CSSaveResult) -> Void)?) {
|
||||
|
||||
self.bridgeToSwift.commit { (result) in
|
||||
|
||||
completion?(result: result.bridgeToObjectiveC)
|
||||
completion?(result.bridgeToObjectiveC)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
|
||||
- parameter closure: the closure where changes can be made prior to the flush
|
||||
*/
|
||||
@objc
|
||||
public func flush(block: () -> Void) {
|
||||
public func flush(_ block: () -> Void) {
|
||||
|
||||
self.bridgeToSwift.flush {
|
||||
|
||||
@@ -125,7 +125,6 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafe() -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
@@ -141,8 +140,7 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
public func beginUnsafeWithSupportsUndo(_ supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
@@ -168,7 +166,7 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,7 +53,6 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
fetchAllFrom:CSFromClass([MyPersonEntity class])
|
||||
fetchClauses:@[CSWhereValue(YES)]]];
|
||||
```
|
||||
|
||||
- parameter value: the boolean value for the predicate
|
||||
*/
|
||||
@objc
|
||||
@@ -70,7 +69,6 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
fetchAllFrom:CSFromClass([MyPersonEntity class])
|
||||
fetchClauses:@[CSWherePredicate(predicate)]];
|
||||
```
|
||||
|
||||
- parameter format: the format string for the predicate
|
||||
- parameter argumentArray: the arguments for `format`
|
||||
*/
|
||||
@@ -123,7 +121,7 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSWhere else {
|
||||
|
||||
@@ -134,14 +132,14 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: self.dynamicType))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
return "(\(String(reflecting: type(of: self)))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
public func applyToFetchRequest(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) {
|
||||
|
||||
self.bridgeToSwift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
@@ -79,17 +79,17 @@ public extension CoreStoreSwiftType where ObjectiveCType: CoreStoreObjectiveCTyp
|
||||
|
||||
// MARK: - Internal
|
||||
|
||||
internal func bridge<T: CoreStoreSwiftType where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType>(@noescape closure: () -> T) -> T.ObjectiveCType {
|
||||
internal func bridge<T: CoreStoreSwiftType>(_ closure: () -> T) -> T.ObjectiveCType where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType {
|
||||
|
||||
return closure().bridgeToObjectiveC
|
||||
}
|
||||
|
||||
internal func bridge<T: CoreStoreSwiftType where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType>(@noescape closure: () -> T?) -> T.ObjectiveCType? {
|
||||
internal func bridge<T: CoreStoreSwiftType>(_ closure: () -> T?) -> T.ObjectiveCType? where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType {
|
||||
|
||||
return closure()?.bridgeToObjectiveC
|
||||
}
|
||||
|
||||
internal func bridge<T: CoreStoreSwiftType where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType>(@noescape closure: () throws -> T) throws -> T.ObjectiveCType {
|
||||
internal func bridge<T: CoreStoreSwiftType>(_ closure: () throws -> T) throws -> T.ObjectiveCType where T.ObjectiveCType: CoreStoreObjectiveCType, T == T.ObjectiveCType.SwiftType {
|
||||
|
||||
do {
|
||||
|
||||
@@ -101,7 +101,7 @@ internal func bridge<T: CoreStoreSwiftType where T.ObjectiveCType: CoreStoreObje
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge(@noescape closure: () throws -> Void) throws {
|
||||
internal func bridge(_ closure: () throws -> Void) throws {
|
||||
|
||||
do {
|
||||
|
||||
@@ -113,86 +113,62 @@ internal func bridge(@noescape closure: () throws -> Void) throws {
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge<T: CoreStoreSwiftType>(error: NSErrorPointer, @noescape _ closure: () throws -> T) -> T.ObjectiveCType? {
|
||||
internal func bridge<T: CoreStoreSwiftType>(_ error: NSErrorPointer, _ closure: () throws -> T) -> T.ObjectiveCType? {
|
||||
|
||||
do {
|
||||
|
||||
let result = try closure()
|
||||
if error != nil {
|
||||
|
||||
error.memory = nil
|
||||
}
|
||||
error?.pointee = nil
|
||||
return result.bridgeToObjectiveC
|
||||
}
|
||||
catch let swiftError {
|
||||
|
||||
if error != nil {
|
||||
|
||||
error.memory = swiftError.bridgeToObjectiveC
|
||||
}
|
||||
error?.pointee = swiftError.bridgeToObjectiveC
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge(error: NSErrorPointer, @noescape _ closure: () throws -> Void) -> Bool {
|
||||
internal func bridge(_ error: NSErrorPointer, _ closure: () throws -> Void) -> Bool {
|
||||
|
||||
do {
|
||||
|
||||
try closure()
|
||||
if error != nil {
|
||||
|
||||
error.memory = nil
|
||||
}
|
||||
error?.pointee = nil
|
||||
return true
|
||||
}
|
||||
catch let swiftError {
|
||||
|
||||
if error != nil {
|
||||
|
||||
error.memory = swiftError.bridgeToObjectiveC
|
||||
}
|
||||
error?.pointee = swiftError.bridgeToObjectiveC
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge<T>(error: NSErrorPointer, @noescape _ closure: () throws -> T?) -> T? {
|
||||
internal func bridge<T>(_ error: NSErrorPointer, _ closure: () throws -> T?) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
let result = try closure()
|
||||
if error != nil {
|
||||
|
||||
error.memory = nil
|
||||
}
|
||||
error?.pointee = nil
|
||||
return result
|
||||
}
|
||||
catch let swiftError {
|
||||
|
||||
if error != nil {
|
||||
|
||||
error.memory = swiftError.bridgeToObjectiveC
|
||||
}
|
||||
error?.pointee = swiftError.bridgeToObjectiveC
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge<T: CoreStoreSwiftType>(error: NSErrorPointer, @noescape _ closure: () throws -> [T]) -> [T.ObjectiveCType]? {
|
||||
internal func bridge<T: CoreStoreSwiftType>(_ error: NSErrorPointer, _ closure: () throws -> [T]) -> [T.ObjectiveCType]? {
|
||||
|
||||
do {
|
||||
|
||||
let result = try closure()
|
||||
if error != nil {
|
||||
|
||||
error.memory = nil
|
||||
}
|
||||
error?.pointee = nil
|
||||
return result.map { $0.bridgeToObjectiveC }
|
||||
}
|
||||
catch let swiftError {
|
||||
|
||||
if error != nil {
|
||||
|
||||
error.memory = swiftError.bridgeToObjectiveC
|
||||
}
|
||||
error?.pointee = swiftError.bridgeToObjectiveC
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,38 +29,78 @@ import CoreData
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// MARK: - NSFetchedResultsController
|
||||
// MARK: - CSDataStack
|
||||
|
||||
public extension NSFetchedResultsController {
|
||||
public extension CSDataStack {
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from a `CSDataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstractio
|
||||
Utility for creating an `NSFetchedResultsController` from the `CSDataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `-performFetch:` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter dataStack: the `CSDataStack` to observe objects from
|
||||
- parameter fetchRequest: the `NSFetchRequest` instance to use with the `NSFetchedResultsController`
|
||||
- parameter from: an optional `CSFrom` clause indicating the entity type. If not specified, the `fetchRequest` argument's `entity` property should already be set.
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes the `CSDataStack`
|
||||
*/
|
||||
@objc
|
||||
public static func cs_createForStack(dataStack: CSDataStack, fetchRequest: NSFetchRequest, from: CSFrom?, sectionBy: CSSectionBy?, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController {
|
||||
public func createFetchedResultsControllerFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||
|
||||
return CoreStoreFetchedResultsController(
|
||||
context: dataStack.bridgeToSwift.mainContext,
|
||||
fetchRequest: fetchRequest,
|
||||
from: from?.bridgeToSwift,
|
||||
sectionBy: sectionBy?.bridgeToSwift,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(CSOrderBy)) clause or any custom \(cs_typeName(CSFetchClause)) that provides a sort descriptor."
|
||||
)
|
||||
}
|
||||
return createFRC(
|
||||
fromContext: self.bridgeToSwift.mainContext,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CSUnsafeDataTransaction
|
||||
|
||||
public extension CSUnsafeDataTransaction {
|
||||
|
||||
/**
|
||||
Utility for creating an `NSFetchedResultsController` from the `CSUnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstraction.
|
||||
- Note: It is the caller's responsibility to call `-performFetch:` on the created `NSFetchedResultsController`.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: an `NSFetchedResultsController` that observes an `CSUnsafeDataTransaction`
|
||||
*/
|
||||
@objc
|
||||
public func createFetchedResultsControllerFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||
|
||||
return createFRC(
|
||||
fromContext: self.bridgeToSwift.context,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
fileprivate func createFRC(fromContext context: NSManagedObjectContext, from: CSFrom? = nil, sectionBy: CSSectionBy?, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||
|
||||
let controller = CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: CoreStoreFetchRequest().dynamicCast(),
|
||||
from: from?.bridgeToSwift.upcast(),
|
||||
sectionBy: sectionBy?.bridgeToSwift,
|
||||
applyFetchClauses: { (fetchRequest) in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController<NSManagedObject>.self)) requires a sort information. Specify from a \(cs_typeName(CSOrderBy.self)) clause or any custom \(cs_typeName(CSFetchClause.self)) that provides a sort descriptor."
|
||||
)
|
||||
}
|
||||
)
|
||||
return controller.dynamicCast()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -38,8 +38,7 @@ public extension NSManagedObject {
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func cs_accessValueForKVCKey(KVCKey: KeyPath) -> AnyObject? {
|
||||
public func cs_accessValueForKVCKey(_ KVCKey: KeyPath) -> Any? {
|
||||
|
||||
return self.accessValueForKVCKey(KVCKey)
|
||||
}
|
||||
@@ -51,7 +50,7 @@ public extension NSManagedObject {
|
||||
- parameter KVCKey: the KVC key
|
||||
*/
|
||||
@objc
|
||||
public func cs_setValue(value: AnyObject?, forKVCKey KVCKey: KeyPath) {
|
||||
public func cs_setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
|
||||
|
||||
self.setValue(value, forKVCKey: KVCKey)
|
||||
}
|
||||
|
||||
@@ -34,41 +34,41 @@ internal extension NSManagedObjectContext {
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
internal func fetchOne(_ from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchOne(fetchRequest)
|
||||
return self.fetchOne(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
internal func fetchAll<T: NSManagedObject>(_ from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [T]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchAll(fetchRequest)
|
||||
return self.fetchAll(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> Int? {
|
||||
internal func fetchCount(_ from: CSFrom, _ fetchClauses: [CSFetchClause]) -> Int? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -78,51 +78,51 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchCount(fetchRequest)
|
||||
return self.fetchCount(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
internal func fetchObjectID(_ from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchRequest.resultType = .managedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchObjectID(fetchRequest)
|
||||
return self.fetchObjectID(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
internal func fetchObjectIDs(_ from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchRequest.resultType = .managedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
guard storeFound else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.fetchObjectIDs(fetchRequest)
|
||||
return self.fetchObjectIDs(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll(from: CSFrom, _ deleteClauses: [CSDeleteClause]) -> Int? {
|
||||
internal func deleteAll(_ from: CSFrom, _ deleteClauses: [CSDeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchRequest.returnsObjectsAsFaults = true
|
||||
fetchRequest.includesPropertyValues = false
|
||||
deleteClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
@@ -131,11 +131,11 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.deleteAll(fetchRequest)
|
||||
return self.deleteAll(fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue(from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> AnyObject? {
|
||||
internal func queryValue(_ from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> Any? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -150,11 +150,11 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.queryValue(selectTerms, fetchRequest: fetchRequest)
|
||||
return self.queryValue(selectTerms, fetchRequest: fetchRequest.dynamicCast())
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes(from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> [[NSString: AnyObject]]? {
|
||||
internal func queryAttributes(_ from: CSFrom, _ selectClause: CSSelect, _ queryClauses: [CSQueryClause]) -> [[String: Any]]? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.bridgeToSwift.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -168,6 +168,6 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
return nil
|
||||
}
|
||||
return self.queryAttributes(fetchRequest)
|
||||
return self.queryAttributes(fetchRequest.dynamicCast())
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// NSProgress+ObjectiveC.swift
|
||||
// Progress+ObjectiveC.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2016 John Rommel Estropia
|
||||
@@ -26,9 +26,9 @@
|
||||
import Foundation
|
||||
|
||||
|
||||
// MARK: - NSProgress
|
||||
// MARK: - Progress
|
||||
|
||||
public extension NSProgress {
|
||||
public extension Progress {
|
||||
|
||||
/**
|
||||
Sets a closure that the `NSProgress` calls whenever its `fractionCompleted` changes. You can use this instead of setting up KVO.
|
||||
@@ -36,7 +36,7 @@ public extension NSProgress {
|
||||
- parameter closure: the closure to execute on progress change
|
||||
*/
|
||||
@objc
|
||||
public func cs_setProgressHandler(closure: ((progress: NSProgress) -> Void)?) {
|
||||
public func cs_setProgressHandler(_ closure: ((_ progress: Progress) -> Void)?) {
|
||||
|
||||
self.setProgressHandler(closure)
|
||||
}
|
||||
@@ -39,8 +39,7 @@ public extension CoreStore {
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorObject<T: NSManagedObject>(object: T) -> ObjectMonitor<T> {
|
||||
public static func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorObject(object)
|
||||
}
|
||||
@@ -52,8 +51,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public static func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorList(from, fetchClauses)
|
||||
}
|
||||
@@ -65,8 +63,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public static func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorList(from, fetchClauses)
|
||||
}
|
||||
@@ -78,7 +75,7 @@ public extension CoreStore {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public static func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
public static func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
|
||||
}
|
||||
@@ -90,7 +87,7 @@ public extension CoreStore {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public static func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
public static func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
|
||||
}
|
||||
@@ -103,8 +100,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public static func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -117,8 +113,7 @@ public extension CoreStore {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public static func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -131,7 +126,7 @@ public extension CoreStore {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -144,7 +139,7 @@ public extension CoreStore {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
@@ -42,11 +39,10 @@ public extension DataStack {
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorObject<T: NSManagedObject>(object: T) -> ObjectMonitor<T> {
|
||||
public func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return ObjectMonitor(dataStack: self, object: object)
|
||||
@@ -59,8 +55,7 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.monitorList(from, fetchClauses)
|
||||
}
|
||||
@@ -72,11 +67,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
return ListMonitor(
|
||||
@@ -89,7 +83,7 @@ public extension DataStack {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
|
||||
"An \(cs_typeName(ListMonitor<T>.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."
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -102,7 +96,7 @@ public extension DataStack {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
|
||||
}
|
||||
@@ -114,10 +108,10 @@ public extension DataStack {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
_ = ListMonitor(
|
||||
@@ -130,7 +124,7 @@ public extension DataStack {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
|
||||
"An \(cs_typeName(ListMonitor<T>.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."
|
||||
)
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
@@ -145,8 +139,7 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.monitorSectionedList(from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -159,11 +152,10 @@ public extension DataStack {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
@@ -177,7 +169,7 @@ public extension DataStack {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
|
||||
"An \(cs_typeName(ListMonitor<T>.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."
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -191,7 +183,7 @@ public extension DataStack {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -204,10 +196,10 @@ public extension DataStack {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
@@ -221,7 +213,7 @@ public extension DataStack {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
|
||||
"An \(cs_typeName(ListMonitor<T>.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."
|
||||
)
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
|
||||
+193
-260
File diff suppressed because it is too large
Load Diff
@@ -35,8 +35,8 @@ import CoreData
|
||||
Implement the `ListObserver` protocol to observe changes to a list of `NSManagedObject`s. `ListObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
|
||||
```
|
||||
let monitor = CoreStore.monitorList(
|
||||
From(MyPersonEntity),
|
||||
OrderBy(.Ascending("lastName"))
|
||||
From<MyPersonEntity>(),
|
||||
OrderBy(.ascending("lastName"))
|
||||
)
|
||||
monitor.addObserver(self)
|
||||
```
|
||||
@@ -49,32 +49,37 @@ public protocol ListObserver: class {
|
||||
associatedtype ListEntityType: NSManagedObject
|
||||
|
||||
/**
|
||||
Handles processing just before a change to the observed list occurs
|
||||
Handles processing just before a change to the observed list occurs. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
*/
|
||||
func listMonitorWillChange(monitor: ListMonitor<ListEntityType>)
|
||||
func listMonitorWillChange(_ monitor: ListMonitor<ListEntityType>)
|
||||
|
||||
/**
|
||||
Handles processing right after a change to the observed list occurs
|
||||
Handles processing right after a change to the observed list occurs. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the object being observed
|
||||
*/
|
||||
func listMonitorDidChange(monitor: ListMonitor<ListEntityType>)
|
||||
func listMonitorDidChange(_ monitor: ListMonitor<ListEntityType>)
|
||||
|
||||
/**
|
||||
This method is broadcast from within the `ListMonitor`'s `refetch(...)` method to let observers prepare for the internal `NSFetchedResultsController`'s pending change to its predicate, sort descriptors, etc. Note that the actual refetch will happen after the `NSFetchedResultsController`'s last `controllerDidChangeContent(_:)` notification completes.
|
||||
This method is broadcast from within the `ListMonitor`'s `refetch(...)` method to let observers prepare for the internal `NSFetchedResultsController`'s pending change to its predicate, sort descriptors, etc. (Optional)
|
||||
The default implementation does nothing.
|
||||
- Note: The actual refetch will happen after the `NSFetchedResultsController`'s last `controllerDidChangeContent(_:)` notification completes
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the object being observed
|
||||
*/
|
||||
func listMonitorWillRefetch(monitor: ListMonitor<ListEntityType>)
|
||||
func listMonitorWillRefetch(_ monitor: ListMonitor<ListEntityType>)
|
||||
|
||||
/**
|
||||
After the `ListMonitor`'s `refetch(...)` method is called, this method is broadcast after the `NSFetchedResultsController`'s last `controllerDidChangeContent(_:)` notification completes.
|
||||
After the `ListMonitor`'s `refetch(...)` method is called, this method is broadcast after the `NSFetchedResultsController`'s last `controllerDidChangeContent(_:)` notification completes. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the object being observed
|
||||
*/
|
||||
func listMonitorDidRefetch(monitor: ListMonitor<ListEntityType>)
|
||||
func listMonitorDidRefetch(_ monitor: ListMonitor<ListEntityType>)
|
||||
}
|
||||
|
||||
|
||||
@@ -82,25 +87,13 @@ public protocol ListObserver: class {
|
||||
|
||||
public extension ListObserver {
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitorWillChange(monitor: ListMonitor<ListEntityType>) { }
|
||||
public func listMonitorWillChange(_ monitor: ListMonitor<ListEntityType>) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitorDidChange(monitor: ListMonitor<ListEntityType>) { }
|
||||
public func listMonitorDidChange(_ monitor: ListMonitor<ListEntityType>) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitorWillRefetch(monitor: ListMonitor<ListEntityType>) { }
|
||||
public func listMonitorWillRefetch(_ monitor: ListMonitor<ListEntityType>) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitorDidRefetch(monitor: ListMonitor<ListEntityType>) { }
|
||||
public func listMonitorDidRefetch(_ monitor: ListMonitor<ListEntityType>) { }
|
||||
}
|
||||
|
||||
|
||||
@@ -110,8 +103,8 @@ public extension ListObserver {
|
||||
Implement the `ListObjectObserver` protocol to observe detailed changes to a list's object. `ListObjectObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
|
||||
```
|
||||
let monitor = CoreStore.monitorList(
|
||||
From(MyPersonEntity),
|
||||
OrderBy(.Ascending("lastName"))
|
||||
From<MyPersonEntity>(),
|
||||
OrderBy(.ascending("lastName"))
|
||||
)
|
||||
monitor.addObserver(self)
|
||||
```
|
||||
@@ -119,41 +112,45 @@ public extension ListObserver {
|
||||
public protocol ListObjectObserver: ListObserver {
|
||||
|
||||
/**
|
||||
Notifies that an object was inserted to the specified `NSIndexPath` in the list
|
||||
Notifies that an object was inserted to the specified `NSIndexPath` in the list. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter object: the entity type for the inserted object
|
||||
- parameter indexPath: the new `NSIndexPath` for the inserted object
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didInsertObject object: ListEntityType, toIndexPath indexPath: NSIndexPath)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didInsertObject object: ListEntityType, toIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object was deleted from the specified `NSIndexPath` in the list
|
||||
Notifies that an object was deleted from the specified `NSIndexPath` in the list. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter object: the entity type for the deleted object
|
||||
- parameter indexPath: the `NSIndexPath` for the deleted object
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didDeleteObject object: ListEntityType, fromIndexPath indexPath: NSIndexPath)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didDeleteObject object: ListEntityType, fromIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object at the specified `NSIndexPath` was updated
|
||||
Notifies that an object at the specified `NSIndexPath` was updated. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter object: the entity type for the updated object
|
||||
- parameter indexPath: the `NSIndexPath` for the updated object
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didUpdateObject object: ListEntityType, atIndexPath indexPath: NSIndexPath)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didUpdateObject object: ListEntityType, atIndexPath indexPath: IndexPath)
|
||||
|
||||
/**
|
||||
Notifies that an object's index changed
|
||||
Notifies that an object's index changed. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter object: the entity type for the moved object
|
||||
- parameter fromIndexPath: the previous `NSIndexPath` for the moved object
|
||||
- parameter toIndexPath: the new `NSIndexPath` for the moved object
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didMoveObject object: ListEntityType, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didMoveObject object: ListEntityType, fromIndexPath: IndexPath, toIndexPath: IndexPath)
|
||||
}
|
||||
|
||||
|
||||
@@ -161,25 +158,13 @@ public protocol ListObjectObserver: ListObserver {
|
||||
|
||||
public extension ListObjectObserver {
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didInsertObject object: ListEntityType, toIndexPath indexPath: NSIndexPath) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didInsertObject object: ListEntityType, toIndexPath indexPath: IndexPath) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didDeleteObject object: ListEntityType, fromIndexPath indexPath: NSIndexPath) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didDeleteObject object: ListEntityType, fromIndexPath indexPath: IndexPath) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didUpdateObject object: ListEntityType, atIndexPath indexPath: NSIndexPath) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didUpdateObject object: ListEntityType, atIndexPath indexPath: IndexPath) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didMoveObject object: ListEntityType, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didMoveObject object: ListEntityType, fromIndexPath: IndexPath, toIndexPath: IndexPath) { }
|
||||
}
|
||||
|
||||
|
||||
@@ -189,9 +174,9 @@ public extension ListObjectObserver {
|
||||
Implement the `ListSectionObserver` protocol to observe changes to a list's section info. `ListSectionObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
|
||||
```
|
||||
let monitor = CoreStore.monitorSectionedList(
|
||||
From(MyPersonEntity),
|
||||
From<MyPersonEntity>(),
|
||||
SectionBy("age") { "Age \($0)" },
|
||||
OrderBy(.Ascending("lastName"))
|
||||
OrderBy(.ascending("lastName"))
|
||||
)
|
||||
monitor.addObserver(self)
|
||||
```
|
||||
@@ -199,22 +184,24 @@ public extension ListObjectObserver {
|
||||
public protocol ListSectionObserver: ListObjectObserver {
|
||||
|
||||
/**
|
||||
Notifies that a section was inserted at the specified index
|
||||
Notifies that a section was inserted at the specified index. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter sectionInfo: the `NSFetchedResultsSectionInfo` for the inserted section
|
||||
- parameter sectionIndex: the new section index for the new section
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int)
|
||||
|
||||
/**
|
||||
Notifies that a section was inserted at the specified index
|
||||
Notifies that a section was inserted at the specified index. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ListMonitor` monitoring the list being observed
|
||||
- parameter sectionInfo: the `NSFetchedResultsSectionInfo` for the deleted section
|
||||
- parameter sectionIndex: the previous section index for the deleted section
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int)
|
||||
func listMonitor(_ monitor: ListMonitor<ListEntityType>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int)
|
||||
}
|
||||
|
||||
|
||||
@@ -222,15 +209,9 @@ public protocol ListSectionObserver: ListObjectObserver {
|
||||
|
||||
public extension ListSectionObserver {
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func listMonitor(monitor: ListMonitor<ListEntityType>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int) { }
|
||||
public func listMonitor(_ monitor: ListMonitor<ListEntityType>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int) { }
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
@@ -44,14 +41,14 @@ 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<T: NSManagedObject> {
|
||||
public final class ObjectMonitor<EntityType: NSManagedObject>: Equatable {
|
||||
|
||||
/**
|
||||
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
|
||||
*/
|
||||
public var object: T? {
|
||||
public var object: EntityType? {
|
||||
|
||||
return self.fetchedResultsController.fetchedObjects?.first as? T
|
||||
return self.fetchedResultsController.fetchedObjects?.first as? EntityType
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +70,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
- parameter observer: an `ObjectObserver` to send change notifications to
|
||||
*/
|
||||
public func addObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
|
||||
public func addObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == EntityType {
|
||||
|
||||
self.unregisterObserver(observer)
|
||||
self.registerObserver(
|
||||
@@ -100,12 +97,25 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
- parameter observer: an `ObjectObserver` to unregister notifications to
|
||||
*/
|
||||
public func removeObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
|
||||
public func removeObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == EntityType {
|
||||
|
||||
self.unregisterObserver(observer)
|
||||
}
|
||||
|
||||
|
||||
// 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 {
|
||||
@@ -116,25 +126,25 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal convenience init(dataStack: DataStack, object: T) {
|
||||
internal convenience init(dataStack: DataStack, object: EntityType) {
|
||||
|
||||
self.init(context: dataStack.mainContext, object: object)
|
||||
}
|
||||
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: T) {
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: EntityType) {
|
||||
|
||||
self.init(context: unsafeTransaction.context, object: object)
|
||||
}
|
||||
|
||||
internal func registerObserver<U: AnyObject>(observer: U, willChangeObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didDeleteObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didUpdateObject: (observer: U, monitor: ObjectMonitor<T>, object: T, changedPersistentKeys: Set<String>) -> Void) {
|
||||
internal func registerObserver<U: AnyObject>(_ observer: U, willChangeObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType, _ changedPersistentKeys: Set<String>) -> Void) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to add an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
Thread.isMainThread,
|
||||
"Attempted to add an observer of type \(cs_typeName(observer as AnyObject)) outside the main thread."
|
||||
)
|
||||
self.registerChangeNotification(
|
||||
&self.willChangeObjectKey,
|
||||
name: ObjectMonitorWillChangeObjectNotification,
|
||||
name: Notification.Name.objectMonitorWillChangeObject,
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
@@ -142,12 +152,12 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
return
|
||||
}
|
||||
willChangeObject(observer: observer, monitor: monitor, object: object)
|
||||
willChangeObject(observer, monitor, object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
&self.didDeleteObjectKey,
|
||||
name: ObjectMonitorDidDeleteObjectNotification,
|
||||
name: Notification.Name.objectMonitorDidDeleteObject,
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object) -> Void in
|
||||
|
||||
@@ -155,12 +165,12 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
return
|
||||
}
|
||||
didDeleteObject(observer: observer, monitor: monitor, object: object)
|
||||
didDeleteObject(observer, monitor, object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
&self.didUpdateObjectKey,
|
||||
name: ObjectMonitorDidUpdateObjectNotification,
|
||||
name: Notification.Name.objectMonitorDidUpdateObject,
|
||||
toObserver: observer,
|
||||
callback: { [weak self, weak observer] (monitor, object) -> Void in
|
||||
|
||||
@@ -170,7 +180,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
}
|
||||
|
||||
let previousCommitedAttributes = self.lastCommittedAttributes
|
||||
let currentCommitedAttributes = object.committedValuesForKeys(nil) as! [String: NSObject]
|
||||
let currentCommitedAttributes = object.committedValues(forKeys: nil) as! [String: NSObject]
|
||||
|
||||
var changedKeys = Set<String>()
|
||||
for key in currentCommitedAttributes.keys {
|
||||
@@ -182,20 +192,15 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
}
|
||||
|
||||
self.lastCommittedAttributes = currentCommitedAttributes
|
||||
didUpdateObject(
|
||||
observer: observer,
|
||||
monitor: monitor,
|
||||
object: object,
|
||||
changedPersistentKeys: changedKeys
|
||||
)
|
||||
didUpdateObject(observer, monitor, object, changedKeys)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
internal func unregisterObserver(observer: AnyObject) {
|
||||
internal func unregisterObserver(_ observer: AnyObject) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to remove an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
)
|
||||
|
||||
@@ -207,7 +212,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
internal func upcast() -> ObjectMonitor<NSManagedObject> {
|
||||
|
||||
return unsafeBitCast(self, ObjectMonitor<NSManagedObject>.self)
|
||||
return unsafeBitCast(self, to: ObjectMonitor<NSManagedObject>.self)
|
||||
}
|
||||
|
||||
deinit {
|
||||
@@ -219,19 +224,19 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
// MARK: Private
|
||||
|
||||
private let fetchedResultsController: CoreStoreFetchedResultsController
|
||||
private let fetchedResultsControllerDelegate: FetchedResultsControllerDelegate
|
||||
private let fetchedResultsControllerDelegate: FetchedResultsControllerDelegate<EntityType>
|
||||
private var lastCommittedAttributes = [String: NSObject]()
|
||||
|
||||
private var willChangeObjectKey: Void?
|
||||
private var didDeleteObjectKey: Void?
|
||||
private var didUpdateObjectKey: Void?
|
||||
|
||||
private init(context: NSManagedObjectContext, object: T) {
|
||||
private init(context: NSManagedObjectContext, object: EntityType) {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
fetchRequest.entity = object.entity
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.resultType = .managedObjectResultType
|
||||
fetchRequest.sortDescriptors = []
|
||||
fetchRequest.includesPendingChanges = false
|
||||
fetchRequest.shouldRefreshRefetchedObjects = true
|
||||
@@ -239,11 +244,11 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
let objectID = object.objectID
|
||||
let fetchedResultsController = CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: fetchRequest,
|
||||
fetchRequest: fetchRequest.dynamicCast(),
|
||||
applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest
|
||||
)
|
||||
|
||||
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
|
||||
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate<EntityType>()
|
||||
|
||||
self.fetchedResultsController = fetchedResultsController
|
||||
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
|
||||
@@ -252,10 +257,10 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
|
||||
try! fetchedResultsController.performFetchFromSpecifiedStores()
|
||||
|
||||
self.lastCommittedAttributes = (self.object?.committedValuesForKeys(nil) as? [String: NSObject]) ?? [:]
|
||||
self.lastCommittedAttributes = (self.object?.committedValues(forKeys: nil) as? [String: NSObject]) ?? [:]
|
||||
}
|
||||
|
||||
private func registerChangeNotification(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: AnyObject, callback: (monitor: ObjectMonitor<T>) -> Void) {
|
||||
private func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<EntityType>) -> Void) {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
@@ -267,7 +272,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
return
|
||||
}
|
||||
callback(monitor: self)
|
||||
callback(self)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -275,7 +280,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
)
|
||||
}
|
||||
|
||||
private func registerObjectNotification(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: AnyObject, callback: (monitor: ObjectMonitor<T>, object: T) -> Void) {
|
||||
private func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void) {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
@@ -285,11 +290,11 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
guard let `self` = self,
|
||||
let userInfo = note.userInfo,
|
||||
let object = userInfo[UserInfoKeyObject] as? T else {
|
||||
let object = userInfo[String(describing: NSManagedObject.self)] as? EntityType else {
|
||||
|
||||
return
|
||||
}
|
||||
callback(monitor: self, object: object)
|
||||
callback(self, object)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -299,53 +304,39 @@ public final class ObjectMonitor<T: 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 {
|
||||
|
||||
// MARK: FetchedResultsControllerHandler
|
||||
|
||||
internal func controllerWillChangeContent(controller: NSFetchedResultsController) {
|
||||
internal func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
||||
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
ObjectMonitorWillChangeObjectNotification,
|
||||
NotificationCenter.default.post(
|
||||
name: Notification.Name.objectMonitorWillChangeObject,
|
||||
object: self
|
||||
)
|
||||
}
|
||||
|
||||
internal func controllerDidChangeContent(controller: NSFetchedResultsController) { }
|
||||
internal func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { }
|
||||
|
||||
internal func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
|
||||
internal func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeObject anObject: Any, atIndexPath indexPath: IndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
|
||||
|
||||
switch type {
|
||||
|
||||
case .Delete:
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
ObjectMonitorDidDeleteObjectNotification,
|
||||
case .delete:
|
||||
NotificationCenter.default.post(
|
||||
name: Notification.Name.objectMonitorDidDeleteObject,
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyObject: anObject]
|
||||
userInfo: [String(describing: NSManagedObject.self): anObject]
|
||||
)
|
||||
|
||||
case .Update:
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
ObjectMonitorDidUpdateObjectNotification,
|
||||
case .update,
|
||||
.move where indexPath == newIndexPath:
|
||||
NotificationCenter.default.post(
|
||||
name: Notification.Name.objectMonitorDidUpdateObject,
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyObject: anObject]
|
||||
userInfo: [String(describing: NSManagedObject.self): anObject]
|
||||
)
|
||||
|
||||
default:
|
||||
@@ -353,19 +344,22 @@ extension ObjectMonitor: FetchedResultsControllerHandler {
|
||||
}
|
||||
}
|
||||
|
||||
internal func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { }
|
||||
internal func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { }
|
||||
|
||||
internal func controller(controller: NSFetchedResultsController, sectionIndexTitleForSectionName sectionName: String?) -> String? {
|
||||
internal func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, sectionIndexTitleForSectionName sectionName: String?) -> String? {
|
||||
|
||||
return sectionName
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Notification.Name
|
||||
|
||||
|
||||
private let ObjectMonitorWillChangeObjectNotification = "ObjectMonitorWillChangeObjectNotification"
|
||||
private let ObjectMonitorDidDeleteObjectNotification = "ObjectMonitorDidDeleteObjectNotification"
|
||||
private let ObjectMonitorDidUpdateObjectNotification = "ObjectMonitorDidUpdateObjectNotification"
|
||||
|
||||
private let UserInfoKeyObject = "UserInfoKeyObject"
|
||||
fileprivate extension Notification.Name {
|
||||
|
||||
fileprivate static let objectMonitorWillChangeObject = Notification.Name(rawValue: "objectMonitorWillChangeObject")
|
||||
fileprivate static let objectMonitorDidDeleteObject = Notification.Name(rawValue: "objectMonitorDidDeleteObject")
|
||||
fileprivate static let objectMonitorDidUpdateObject = Notification.Name(rawValue: "objectMonitorDidUpdateObject")
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -46,29 +46,32 @@ public protocol ObjectObserver: class {
|
||||
associatedtype ObjectEntityType: NSManagedObject
|
||||
|
||||
/**
|
||||
Handles processing just before a change to the observed `object` occurs
|
||||
Handles processing just before a change to the observed `object` occurs. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, willUpdateObject object: ObjectEntityType)
|
||||
func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, willUpdateObject object: ObjectEntityType)
|
||||
|
||||
/**
|
||||
Handles processing right after a change to the observed `object` occurs
|
||||
Handles processing right after a change to the observed `object` occurs. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
- parameter changedPersistentKeys: a `Set` of key paths for the attributes that were changed. Note that `changedPersistentKeys` only contains keys for attributes/relationships present in the persistent store, thus transient properties will not be reported.
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set<KeyPath>)
|
||||
func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set<KeyPath>)
|
||||
|
||||
/**
|
||||
Handles processing right after `object` is deleted
|
||||
Handles processing right after `object` is deleted. (Optional)
|
||||
The default implementation does nothing.
|
||||
|
||||
- parameter monitor: the `ObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, didDeleteObject object: ObjectEntityType)
|
||||
func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, didDeleteObject object: ObjectEntityType)
|
||||
}
|
||||
|
||||
|
||||
@@ -76,20 +79,11 @@ public protocol ObjectObserver: class {
|
||||
|
||||
public extension ObjectObserver {
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, willUpdateObject object: ObjectEntityType) { }
|
||||
public func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, willUpdateObject object: ObjectEntityType) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set<KeyPath>) { }
|
||||
public func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set<KeyPath>) { }
|
||||
|
||||
/**
|
||||
The default implementation does nothing.
|
||||
*/
|
||||
func objectMonitor(monitor: ObjectMonitor<ObjectEntityType>, didDeleteObject object: ObjectEntityType) { }
|
||||
public func objectMonitor(_ monitor: ObjectMonitor<ObjectEntityType>, didDeleteObject object: ObjectEntityType) { }
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -35,9 +35,9 @@ import CoreData
|
||||
The `SectionBy` clause indicates the key path to use to group the `ListMonitor` objects into sections. An optional closure can also be provided to transform the value into an appropriate section name:
|
||||
```
|
||||
let monitor = CoreStore.monitorSectionedList(
|
||||
From(MyPersonEntity),
|
||||
From<MyPersonEntity>(),
|
||||
SectionBy("age") { "Age \($0)" },
|
||||
OrderBy(.Ascending("lastName"))
|
||||
OrderBy(.ascending("lastName"))
|
||||
)
|
||||
```
|
||||
*/
|
||||
@@ -60,7 +60,7 @@ public struct SectionBy {
|
||||
- parameter sectionKeyPath: the key path to use to group the objects into sections
|
||||
- parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name
|
||||
*/
|
||||
public init(_ sectionKeyPath: KeyPath, _ sectionIndexTransformer: (sectionName: String?) -> String?) {
|
||||
public init(_ sectionKeyPath: KeyPath, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) {
|
||||
|
||||
self.sectionKeyPath = sectionKeyPath
|
||||
self.sectionIndexTransformer = sectionIndexTransformer
|
||||
@@ -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
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
@@ -42,8 +39,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorObject<T: NSManagedObject>(object: T) -> ObjectMonitor<T> {
|
||||
public func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> {
|
||||
|
||||
return ObjectMonitor(
|
||||
unsafeTransaction: self,
|
||||
@@ -58,8 +54,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.monitorList(from, fetchClauses)
|
||||
}
|
||||
@@ -71,8 +66,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
@@ -97,7 +91,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
|
||||
}
|
||||
@@ -109,7 +103,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
@@ -136,8 +130,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.monitorSectionedList(from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -150,8 +143,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public func monitorSectionedList<T: NSManagedObject>(from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
@@ -177,7 +169,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
|
||||
|
||||
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
|
||||
}
|
||||
@@ -190,7 +182,7 @@ public extension UnsafeDataTransaction {
|
||||
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously createAsynchronously: (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
|
||||
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - CoreStore
|
||||
@@ -53,9 +50,9 @@ public extension CoreStore {
|
||||
/**
|
||||
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from `defaultStack`'s model.
|
||||
*/
|
||||
public static func entityDescriptionForType(type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
public static func entityDescription(for type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return self.defaultStack.entityDescriptionForType(type)
|
||||
return self.defaultStack.entityDescription(for: type)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,25 +60,25 @@ public extension CoreStore {
|
||||
```
|
||||
try CoreStore.addStorageAndWait()
|
||||
```
|
||||
|
||||
- returns: the local SQLite storage added to the `defaultStack`
|
||||
*/
|
||||
@discardableResult
|
||||
public static func addStorageAndWait() throws -> SQLiteStore {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(SQLiteStore)
|
||||
return try self.defaultStack.addStorageAndWait(SQLiteStore.self)
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `StorageInterface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
||||
```
|
||||
try CoreStore.addStorageAndWait(InMemoryStore)
|
||||
try CoreStore.addStorageAndWait(InMemoryStore.self)
|
||||
```
|
||||
|
||||
- parameter storeType: the `StorageInterface` type
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the `StorageInterface` added to the `defaultStack`
|
||||
*/
|
||||
public static func addStorageAndWait<T: StorageInterface where T: DefaultInitializableStore>(storeType: T.Type) throws -> T {
|
||||
@discardableResult
|
||||
public static func addStorageAndWait<T: StorageInterface>(_ storeType: T.Type) throws -> T where T: DefaultInitializableStore {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(storeType.init())
|
||||
}
|
||||
@@ -91,27 +88,27 @@ public extension CoreStore {
|
||||
```
|
||||
try CoreStore.addStorageAndWait(InMemoryStore(configuration: "Config1"))
|
||||
```
|
||||
|
||||
- parameter storage: the `StorageInterface`
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the `StorageInterface` added to the `defaultStack`
|
||||
*/
|
||||
public static func addStorageAndWait<T: StorageInterface>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public static func addStorageAndWait<T: StorageInterface>(_ storage: T) throws -> T {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(storage)
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `LocalStorageface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
||||
Creates a `LocalStorageInterface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
||||
```
|
||||
try CoreStore.addStorageAndWait(SQLiteStore)
|
||||
try CoreStore.addStorageAndWait(SQLiteStore.self)
|
||||
```
|
||||
|
||||
- parameter storeType: the `LocalStorageface` type
|
||||
- parameter storeType: the `LocalStorageInterface` type
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the local storage added to the `defaultStack`
|
||||
*/
|
||||
public static func addStorageAndWait<T: LocalStorage where T: DefaultInitializableStore>(storageType: T.Type) throws -> T {
|
||||
@discardableResult
|
||||
public static func addStorageAndWait<T: LocalStorage>(_ storageType: T.Type) throws -> T where T: DefaultInitializableStore {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(storageType.init())
|
||||
}
|
||||
@@ -121,12 +118,12 @@ public extension CoreStore {
|
||||
```
|
||||
try CoreStore.addStorageAndWait(SQLiteStore(configuration: "Config1"))
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the local storage added to the `defaultStack`. Note that this may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public static func addStorageAndWait<T: LocalStorage>(_ storage: T) throws -> T {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(storage)
|
||||
}
|
||||
@@ -140,19 +137,19 @@ public extension CoreStore {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
}
|
||||
try CoreStore.addStorageAndWait(storage)
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the cloud storage added to the stack. Note that this may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorageAndWait<T: CloudStorage>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public static func addStorageAndWait<T: CloudStorage>(_ storage: T) throws -> T {
|
||||
|
||||
return try self.defaultStack.addStorageAndWait(storage)
|
||||
}
|
||||
@@ -161,60 +158,11 @@ public extension CoreStore {
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `InMemoryStore` instance.
|
||||
```
|
||||
try CoreStore.addStorage(InMemoryStore(configuration: configuration))
|
||||
```
|
||||
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from `defaultStack`'s model.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, obsoleted=2.0.0, message="Use addStorageAndWait(_:) by passing an InMemoryStore instance.")
|
||||
public static func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
|
||||
@available(*, deprecated: 3.0.0, renamed: "entityDescription(for:)")
|
||||
public static func entityDescriptionForType(_ type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return try self.defaultStack.addInMemoryStoreAndWait(configuration: configuration)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
```
|
||||
try CoreStore.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
localStorageOptions: .RecreateStoreOnModelMismatch
|
||||
)
|
||||
)
|
||||
```
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func addSQLiteStoreAndWait(fileName fileName: String, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) throws -> NSPersistentStore {
|
||||
|
||||
return try self.defaultStack.addSQLiteStoreAndWait(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
resetStoreOnModelMismatch: resetStoreOnModelMismatch
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
```
|
||||
try CoreStore.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
localStorageOptions: .RecreateStoreOnModelMismatch
|
||||
)
|
||||
)
|
||||
```
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public static func addSQLiteStoreAndWait(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) throws -> NSPersistentStore {
|
||||
|
||||
return try self.defaultStack.addSQLiteStoreAndWait(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
resetStoreOnModelMismatch: resetStoreOnModelMismatch
|
||||
)
|
||||
return self.entityDescription(for: type)
|
||||
}
|
||||
}
|
||||
|
||||
+108
-181
@@ -25,9 +25,6 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
#if USE_FRAMEWORKS
|
||||
import GCDKit
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: - DataStack
|
||||
@@ -35,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`.
|
||||
@@ -44,7 +41,7 @@ public final class DataStack {
|
||||
- parameter bundle: an optional bundle to load models from. If not specified, the main bundle will be used.
|
||||
- parameter migrationChain: the `MigrationChain` that indicates the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
|
||||
*/
|
||||
public convenience init(modelName: String = DataStack.applicationName, bundle: NSBundle = NSBundle.mainBundle(), migrationChain: MigrationChain = nil) {
|
||||
public convenience init(modelName: String = DataStack.applicationName, bundle: Bundle = Bundle.main, migrationChain: MigrationChain = nil) {
|
||||
|
||||
let model = NSManagedObjectModel.fromBundle(
|
||||
bundle,
|
||||
@@ -64,7 +61,7 @@ public final class DataStack {
|
||||
|
||||
CoreStore.assert(
|
||||
migrationChain.valid,
|
||||
"Invalid migration chain passed to the \(cs_typeName(DataStack)). Check that the model versions' order is correct and that no repetitions or ambiguities exist."
|
||||
"Invalid migration chain passed to the \(cs_typeName(DataStack.self)). Check that the model versions' order is correct and that no repetitions or ambiguities exist."
|
||||
)
|
||||
|
||||
self.coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
|
||||
@@ -74,6 +71,8 @@ public final class DataStack {
|
||||
self.migrationChain = migrationChain
|
||||
|
||||
self.rootSavingContext.parentStack = self
|
||||
|
||||
self.mainContext.isDataStackContext = true
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,20 +94,20 @@ public final class DataStack {
|
||||
/**
|
||||
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass.
|
||||
*/
|
||||
public func entityDescriptionForType(type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
public func entityDescription(for type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return NSEntityDescription.entityForName(
|
||||
self.model.entityNameForClass(type),
|
||||
inManagedObjectContext: self.mainContext
|
||||
return NSEntityDescription.entity(
|
||||
forEntityName: self.model.entityNameForClass(type),
|
||||
in: self.mainContext
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the `NSManagedObjectID` for the specified object URI if it exists in the persistent store.
|
||||
*/
|
||||
public func objectIDForURIRepresentation(url: NSURL) -> NSManagedObjectID? {
|
||||
public func objectID(forURIRepresentation url: URL) -> NSManagedObjectID? {
|
||||
|
||||
return self.coordinator.managedObjectIDForURIRepresentation(url)
|
||||
return self.coordinator.managedObjectID(forURIRepresentation: url)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,26 +115,26 @@ public final class DataStack {
|
||||
```
|
||||
try dataStack.addStorageAndWait()
|
||||
```
|
||||
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the local SQLite storage added to the stack
|
||||
*/
|
||||
@discardableResult
|
||||
public func addStorageAndWait() throws -> SQLiteStore {
|
||||
|
||||
return try self.addStorageAndWait(SQLiteStore)
|
||||
return try self.addStorageAndWait(SQLiteStore.self)
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `StorageInterface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
|
||||
```
|
||||
try dataStack.addStorageAndWait(InMemoryStore)
|
||||
try dataStack.addStorageAndWait(InMemoryStore.self)
|
||||
```
|
||||
|
||||
- parameter storeType: the `StorageInterface` type
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the `StorageInterface` added to the stack
|
||||
*/
|
||||
public func addStorageAndWait<T: StorageInterface where T: DefaultInitializableStore>(storeType: T.Type) throws -> T {
|
||||
@discardableResult
|
||||
public func addStorageAndWait<T: StorageInterface>(_ storeType: T.Type) throws -> T where T: DefaultInitializableStore {
|
||||
|
||||
return try self.addStorageAndWait(storeType.init())
|
||||
}
|
||||
@@ -145,12 +144,12 @@ public final class DataStack {
|
||||
```
|
||||
try dataStack.addStorageAndWait(InMemoryStore(configuration: "Config1"))
|
||||
```
|
||||
|
||||
- parameter storage: the `StorageInterface`
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the `StorageInterface` added to the stack
|
||||
*/
|
||||
public func addStorageAndWait<T: StorageInterface>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public func addStorageAndWait<T: StorageInterface>(_ storage: T) throws -> T {
|
||||
|
||||
do {
|
||||
|
||||
@@ -160,8 +159,7 @@ public final class DataStack {
|
||||
|
||||
return storage
|
||||
}
|
||||
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: nil,
|
||||
finalStoreOptions: storage.storeOptions
|
||||
@@ -181,16 +179,16 @@ public final class DataStack {
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `LocalStorageface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
|
||||
Creates a `LocalStorageInterface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
|
||||
```
|
||||
try dataStack.addStorageAndWait(SQLiteStore)
|
||||
try dataStack.addStorageAndWait(SQLiteStore.self)
|
||||
```
|
||||
|
||||
- parameter storeType: the `LocalStorageface` type
|
||||
- parameter storeType: the `LocalStorageInterface` type
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the local storage added to the stack
|
||||
*/
|
||||
public func addStorageAndWait<T: LocalStorage where T: DefaultInitializableStore>(storageType: T.Type) throws -> T {
|
||||
@discardableResult
|
||||
public func addStorageAndWait<T: LocalStorage>(_ storageType: T.Type) throws -> T where T: DefaultInitializableStore {
|
||||
|
||||
return try self.addStorageAndWait(storageType.init())
|
||||
}
|
||||
@@ -200,18 +198,18 @@ public final class DataStack {
|
||||
```
|
||||
try dataStack.addStorageAndWait(SQLiteStore(configuration: "Config1"))
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the local storage added to the stack. Note that this may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public func addStorageAndWait<T: LocalStorage>(_ storage: T) throws -> T {
|
||||
|
||||
return try self.coordinator.performSynchronously {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
CoreStore.assert(
|
||||
fileURL.fileURL,
|
||||
fileURL.isFileURL,
|
||||
"The specified store URL for the \"\(cs_typeName(storage))\" is invalid: \"\(fileURL)\""
|
||||
)
|
||||
|
||||
@@ -220,18 +218,18 @@ public final class DataStack {
|
||||
return storage
|
||||
}
|
||||
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(fileURL) {
|
||||
if let persistentStore = self.coordinator.persistentStore(for: fileURL as URL) {
|
||||
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
if let existingStorage = persistentStore.storageInterface as? T,
|
||||
storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
return existingStorage
|
||||
}
|
||||
|
||||
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
|
||||
let error = CoreStoreError.differentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(fileURL)\" because a different \(cs_typeName(NSPersistentStore)) at that URL already exists."
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(fileURL)\" because a different \(cs_typeName(NSPersistentStore.self)) at that URL already exists."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
@@ -239,36 +237,39 @@ public final class DataStack {
|
||||
do {
|
||||
|
||||
var localStorageOptions = storage.localStorageOptions
|
||||
localStorageOptions.remove(.RecreateStoreOnModelMismatch)
|
||||
localStorageOptions.remove(.recreateStoreOnModelMismatch)
|
||||
|
||||
let storeOptions = storage.storeOptionsForOptions(localStorageOptions)
|
||||
let storeOptions = storage.dictionary(forOptions: localStorageOptions)
|
||||
do {
|
||||
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
try FileManager.default.createDirectory(
|
||||
at: fileURL.deletingLastPathComponent(),
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: fileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
)
|
||||
return storage
|
||||
}
|
||||
catch let error as NSError where storage.localStorageOptions.contains(.RecreateStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
catch let error as NSError where storage.localStorageOptions.contains(.recreateStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: fileURL,
|
||||
options: storeOptions
|
||||
)
|
||||
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
|
||||
|
||||
try self.createPersistentStoreFromStorage(
|
||||
try storage.eraseStorageAndWait(
|
||||
metadata: metadata,
|
||||
soureModelHint: self.model[metadata]
|
||||
)
|
||||
let finalStoreOptions = storage.dictionary(forOptions: storage.localStorageOptions)
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: fileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
finalStoreOptions: finalStoreOptions
|
||||
)
|
||||
return storage
|
||||
}
|
||||
@@ -294,19 +295,19 @@ public final class DataStack {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
}
|
||||
try dataStack.addStorageAndWait(storage)
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- throws: a `CoreStoreError` value indicating the failure
|
||||
- returns: the cloud storage added to the stack. Note that this may not always be the same instance as the parameter argument if a previous `CloudStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorageAndWait<T: CloudStorage>(storage: T) throws -> T {
|
||||
@discardableResult
|
||||
public func addStorageAndWait<T: CloudStorage>(_ storage: T) throws -> T {
|
||||
|
||||
return try self.coordinator.performSynchronously {
|
||||
|
||||
@@ -316,18 +317,18 @@ public final class DataStack {
|
||||
}
|
||||
|
||||
let cacheFileURL = storage.cacheFileURL
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(cacheFileURL) {
|
||||
if let persistentStore = self.coordinator.persistentStore(for: cacheFileURL as URL) {
|
||||
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
if let existingStorage = persistentStore.storageInterface as? T,
|
||||
storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
return existingStorage
|
||||
}
|
||||
|
||||
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: cacheFileURL)
|
||||
let error = CoreStoreError.differentStorageExistsAtURL(existingPersistentStoreURL: cacheFileURL)
|
||||
CoreStore.log(
|
||||
error,
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(cacheFileURL)\" because a different \(cs_typeName(NSPersistentStore)) at that URL already exists."
|
||||
"Failed to add \(cs_typeName(storage)) at \"\(cacheFileURL)\" because a different \(cs_typeName(NSPersistentStore.self)) at that URL already exists."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
@@ -335,36 +336,31 @@ public final class DataStack {
|
||||
do {
|
||||
|
||||
var cloudStorageOptions = storage.cloudStorageOptions
|
||||
cloudStorageOptions.remove(.RecreateLocalStoreOnModelMismatch)
|
||||
cloudStorageOptions.remove(.recreateLocalStoreOnModelMismatch)
|
||||
|
||||
let storeOptions = storage.storeOptionsForOptions(cloudStorageOptions)
|
||||
let storeOptions = storage.dictionary(forOptions: cloudStorageOptions)
|
||||
do {
|
||||
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
cacheFileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: cacheFileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
)
|
||||
return storage
|
||||
}
|
||||
catch let error as NSError where storage.cloudStorageOptions.contains(.RecreateLocalStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
catch let error as NSError where storage.cloudStorageOptions.contains(.recreateLocalStoreOnModelMismatch) && error.isCoreDataMigrationError {
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: cacheFileURL,
|
||||
let finalStoreOptions = storage.dictionary(forOptions: storage.cloudStorageOptions)
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
at: cacheFileURL,
|
||||
options: storeOptions
|
||||
)
|
||||
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
|
||||
|
||||
try self.createPersistentStoreFromStorage(
|
||||
_ = try self.model[metadata].flatMap(storage.eraseStorageAndWait)
|
||||
_ = try self.createPersistentStoreFromStorage(
|
||||
storage,
|
||||
finalURL: cacheFileURL,
|
||||
finalStoreOptions: storeOptions
|
||||
finalStoreOptions: finalStoreOptions
|
||||
)
|
||||
return storage
|
||||
}
|
||||
@@ -382,53 +378,51 @@ public final class DataStack {
|
||||
}
|
||||
|
||||
|
||||
// MARK: Equatable
|
||||
|
||||
public static func == (lhs: DataStack, rhs: DataStack) -> Bool {
|
||||
|
||||
return lhs === rhs
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static let applicationName = (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData"
|
||||
internal static let applicationName = (Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String) ?? "CoreData"
|
||||
|
||||
internal let coordinator: NSPersistentStoreCoordinator
|
||||
internal let rootSavingContext: NSManagedObjectContext
|
||||
internal let mainContext: NSManagedObjectContext
|
||||
internal let model: NSManagedObjectModel
|
||||
internal let migrationChain: MigrationChain
|
||||
internal let childTransactionQueue: GCDQueue = .createSerial("com.coreStore.dataStack.childTransactionQueue")
|
||||
internal let storeMetadataUpdateQueue = GCDQueue.createConcurrent("com.coreStore.persistentStoreBarrierQueue")
|
||||
internal let migrationQueue: NSOperationQueue = {
|
||||
internal let childTransactionQueue = DispatchQueue.serial("com.coreStore.dataStack.childTransactionQueue")
|
||||
internal let storeMetadataUpdateQueue = DispatchQueue.concurrent("com.coreStore.persistentStoreBarrierQueue")
|
||||
internal let migrationQueue: OperationQueue = {
|
||||
|
||||
let migrationQueue = NSOperationQueue()
|
||||
let migrationQueue = OperationQueue()
|
||||
migrationQueue.maxConcurrentOperationCount = 1
|
||||
migrationQueue.name = "com.coreStore.migrationOperationQueue"
|
||||
#if USE_FRAMEWORKS
|
||||
|
||||
migrationQueue.qualityOfService = .Utility
|
||||
migrationQueue.underlyingQueue = dispatch_queue_create("com.coreStore.migrationQueue", DISPATCH_QUEUE_SERIAL)
|
||||
#else
|
||||
|
||||
if #available(iOS 8.0, *) {
|
||||
|
||||
migrationQueue.qualityOfService = .Utility
|
||||
migrationQueue.underlyingQueue = dispatch_queue_create("com.coreStore.migrationQueue", DISPATCH_QUEUE_SERIAL)
|
||||
}
|
||||
#endif
|
||||
migrationQueue.qualityOfService = .utility
|
||||
migrationQueue.underlyingQueue = DispatchQueue.serial("com.coreStore.migrationQueue", qos: .userInitiated)
|
||||
return migrationQueue
|
||||
}()
|
||||
|
||||
internal func persistentStoreForStorage(storage: StorageInterface) -> NSPersistentStore? {
|
||||
internal func persistentStoreForStorage(_ storage: StorageInterface) -> NSPersistentStore? {
|
||||
|
||||
return self.coordinator.persistentStores
|
||||
.filter { $0.storageInterface === storage }
|
||||
.first
|
||||
}
|
||||
|
||||
internal func entityNameForEntityClass(entityClass: AnyClass) -> String? {
|
||||
internal func entityNameForEntityClass(_ entityClass: AnyClass) -> String? {
|
||||
|
||||
return self.model.entityNameForClass(entityClass)
|
||||
}
|
||||
|
||||
internal func persistentStoresForEntityClass(entityClass: AnyClass) -> [NSPersistentStore]? {
|
||||
internal func persistentStoresForEntityClass(_ entityClass: AnyClass) -> [NSPersistentStore]? {
|
||||
|
||||
var returnValue: [NSPersistentStore]? = nil
|
||||
self.storeMetadataUpdateQueue.barrierSync {
|
||||
self.storeMetadataUpdateQueue.sync(flags: .barrier) {
|
||||
|
||||
returnValue = self.entityConfigurationsMapping[NSStringFromClass(entityClass)]?.map {
|
||||
|
||||
@@ -438,57 +432,54 @@ public final class DataStack {
|
||||
return returnValue
|
||||
}
|
||||
|
||||
internal func persistentStoreForEntityClass(entityClass: AnyClass, configuration: String?, inferStoreIfPossible: Bool) -> (store: NSPersistentStore?, isAmbiguous: Bool) {
|
||||
internal func persistentStoreForEntityClass(_ entityClass: AnyClass, configuration: String?, inferStoreIfPossible: Bool) -> (store: NSPersistentStore?, isAmbiguous: Bool) {
|
||||
|
||||
var returnValue: (store: NSPersistentStore?, isAmbiguous: Bool) = (store: nil, isAmbiguous: false)
|
||||
self.storeMetadataUpdateQueue.barrierSync {
|
||||
return self.storeMetadataUpdateQueue.sync(flags: .barrier) { () -> (store: NSPersistentStore?, isAmbiguous: Bool) in
|
||||
|
||||
let configurationsForEntity = self.entityConfigurationsMapping[NSStringFromClass(entityClass)] ?? []
|
||||
if let configuration = configuration {
|
||||
|
||||
if configurationsForEntity.contains(configuration) {
|
||||
|
||||
returnValue = (store: self.configurationStoreMapping[configuration], isAmbiguous: false)
|
||||
return
|
||||
return (store: self.configurationStoreMapping[configuration], isAmbiguous: false)
|
||||
}
|
||||
else if !inferStoreIfPossible {
|
||||
|
||||
return
|
||||
return (store: nil, isAmbiguous: false)
|
||||
}
|
||||
}
|
||||
|
||||
switch configurationsForEntity.count {
|
||||
|
||||
case 0:
|
||||
return
|
||||
return (store: nil, isAmbiguous: false)
|
||||
|
||||
case 1 where inferStoreIfPossible:
|
||||
returnValue = (store: self.configurationStoreMapping[configurationsForEntity.first!], isAmbiguous: false)
|
||||
return (store: self.configurationStoreMapping[configurationsForEntity.first!], isAmbiguous: false)
|
||||
|
||||
default:
|
||||
returnValue = (store: nil, isAmbiguous: true)
|
||||
return (store: nil, isAmbiguous: true)
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
internal func createPersistentStoreFromStorage(storage: StorageInterface, finalURL: NSURL?, finalStoreOptions: [String: AnyObject]?) throws -> NSPersistentStore {
|
||||
internal func createPersistentStoreFromStorage(_ storage: StorageInterface, finalURL: URL?, finalStoreOptions: [AnyHashable: Any]?) throws -> NSPersistentStore {
|
||||
|
||||
let persistentStore = try self.coordinator.addPersistentStoreWithType(
|
||||
storage.dynamicType.storeType,
|
||||
configuration: storage.configuration,
|
||||
URL: finalURL,
|
||||
let persistentStore = try self.coordinator.addPersistentStore(
|
||||
ofType: type(of: storage).storeType,
|
||||
configurationName: storage.configuration,
|
||||
at: finalURL,
|
||||
options: finalStoreOptions
|
||||
)
|
||||
persistentStore.storageInterface = storage
|
||||
|
||||
self.storeMetadataUpdateQueue.barrierAsync {
|
||||
self.storeMetadataUpdateQueue.async(flags: .barrier) {
|
||||
|
||||
let configurationName = persistentStore.configurationName
|
||||
self.configurationStoreMapping[configurationName] = persistentStore
|
||||
for entityDescription in (self.coordinator.managedObjectModel.entitiesForConfiguration(configurationName) ?? []) {
|
||||
for entityDescription in (self.coordinator.managedObjectModel.entities(forConfigurationName: configurationName) ?? []) {
|
||||
|
||||
let managedObjectClassName = entityDescription.managedObjectClassName
|
||||
let managedObjectClassName = entityDescription.managedObjectClassName!
|
||||
CoreStore.assert(
|
||||
NSClassFromString(managedObjectClassName) != nil,
|
||||
"The class \(cs_typeName(managedObjectClassName)) for the entity \(cs_typeName(entityDescription.name)) does not exist. Check if the subclass type and module name are properly configured."
|
||||
@@ -520,7 +511,7 @@ public final class DataStack {
|
||||
|
||||
coordinator.persistentStores.forEach {
|
||||
|
||||
_ = try? coordinator.removePersistentStore($0)
|
||||
_ = try? coordinator.remove($0)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -529,79 +520,15 @@ public final class DataStack {
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `InMemoryStore` instance.
|
||||
```
|
||||
try dataStack.addStorage(InMemoryStore(configuration: configuration))
|
||||
```
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing an InMemoryStore instance.")
|
||||
public func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
|
||||
@available(*, deprecated: 3.0.0, renamed: "entityDescription(for:)")
|
||||
public func entityDescriptionForType(_ type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
let storage = try self.addStorageAndWait(InMemoryStore(configuration: configuration))
|
||||
return self.persistentStoreForStorage(storage)!
|
||||
return self.entityDescription(for: type)
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
```
|
||||
try dataStack.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
localStorageOptions: .RecreateStoreOnModelMismatch
|
||||
)
|
||||
)
|
||||
```
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func addSQLiteStoreAndWait(fileName fileName: String, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) throws -> NSPersistentStore {
|
||||
@available(*, deprecated: 3.0.0, renamed: "objectID(forURIRepresentation:)")
|
||||
public func objectIDForURIRepresentation(_ url: URL) -> NSManagedObjectID? {
|
||||
|
||||
let storage = try self.addStorageAndWait(
|
||||
LegacySQLiteStore(
|
||||
fileName: fileName,
|
||||
configuration: configuration,
|
||||
localStorageOptions: resetStoreOnModelMismatch ? .RecreateStoreOnModelMismatch : .None
|
||||
)
|
||||
)
|
||||
return self.persistentStoreForStorage(storage)!
|
||||
}
|
||||
|
||||
/**
|
||||
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
|
||||
```
|
||||
try dataStack.addStorage(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
localStorageOptions: .RecreateStoreOnModelMismatch
|
||||
)
|
||||
)
|
||||
```
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
|
||||
public func addSQLiteStoreAndWait(fileURL fileURL: NSURL = LegacySQLiteStore.defaultFileURL, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) throws -> NSPersistentStore {
|
||||
|
||||
let storage = try self.addStorageAndWait(
|
||||
LegacySQLiteStore(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
localStorageOptions: resetStoreOnModelMismatch ? .RecreateStoreOnModelMismatch : .None
|
||||
)
|
||||
)
|
||||
return self.persistentStoreForStorage(storage)!
|
||||
return self.objectID(forURIRepresentation: url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - DataStack: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: DataStack, rhs: DataStack) -> Bool {
|
||||
|
||||
return lhs === rhs
|
||||
}
|
||||
|
||||
extension DataStack: Equatable {}
|
||||
|
||||
@@ -34,7 +34,7 @@ import CoreData
|
||||
/**
|
||||
A storage interface backed by an SQLite database managed by iCloud.
|
||||
*/
|
||||
public class ICloudStore: CloudStorage {
|
||||
public final class ICloudStore: CloudStorage {
|
||||
|
||||
/**
|
||||
Initializes an iCloud store interface from the given ubiquitous store information. Returns `nil` if the container could not be located or if iCloud storage is unavailable for the current user or device
|
||||
@@ -45,7 +45,7 @@ public class ICloudStore: CloudStorage {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
@@ -57,41 +57,41 @@ public class ICloudStore: CloudStorage {
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
- parameter ubiquitousContentName: the name of the store in iCloud. This is required and should not be empty, and should not contain periods (`.`).
|
||||
- parameter ubiquitousContentTransactionLogsSubdirectory: an optional subdirectory path for the transaction logs
|
||||
- parameter ubiquitousContentTransactionLogsSubdirectory: a required relative path for the transaction logs
|
||||
- parameter ubiquitousContainerID: a container if your app has multiple ubiquity container identifiers in its entitlements
|
||||
- parameter ubiquitousPeerToken: a per-application salt to allow multiple apps on the same device to share a Core Data store integrated with iCloud
|
||||
- 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 `ubiquitousContentName` explicitly for each of them.
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
|
||||
- parameter cloudStorageOptions: When the `ICloudStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public required init?(ubiquitousContentName: String, ubiquitousContentTransactionLogsSubdirectory: String? = nil, ubiquitousContainerID: String? = nil, ubiquitousPeerToken: String? = nil, configuration: String? = nil, cloudStorageOptions: CloudStorageOptions = nil) {
|
||||
public required init?(ubiquitousContentName: String, ubiquitousContentTransactionLogsSubdirectory: String, ubiquitousContainerID: String? = nil, ubiquitousPeerToken: String? = nil, configuration: String? = nil, cloudStorageOptions: CloudStorageOptions = nil) {
|
||||
|
||||
CoreStore.assert(
|
||||
!ubiquitousContentName.isEmpty,
|
||||
"The ubiquitousContentName cannot be empty."
|
||||
)
|
||||
CoreStore.assert(
|
||||
!ubiquitousContentName.containsString("."),
|
||||
!ubiquitousContentName.contains("."),
|
||||
"The ubiquitousContentName cannot contain periods."
|
||||
)
|
||||
CoreStore.assert(
|
||||
ubiquitousContentTransactionLogsSubdirectory?.isEmpty != true,
|
||||
"The ubiquitousContentURLRelativePath should not be empty if provided."
|
||||
!ubiquitousContentTransactionLogsSubdirectory.isEmpty,
|
||||
"The ubiquitousContentURLRelativePath should not be empty."
|
||||
)
|
||||
CoreStore.assert(
|
||||
ubiquitousPeerToken?.isEmpty != true,
|
||||
"The ubiquitousPeerToken should not be empty if provided."
|
||||
)
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
guard let cacheFileURL = fileManager.URLForUbiquityContainerIdentifier(ubiquitousContainerID) else {
|
||||
let fileManager = FileManager.default
|
||||
guard let cacheFolderURL = fileManager.url(forUbiquityContainerIdentifier: ubiquitousContainerID) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
let cacheFileURL = cacheFolderURL.appendingPathComponent(ubiquitousContentTransactionLogsSubdirectory)
|
||||
|
||||
var storeOptions: [String: AnyObject] = [
|
||||
var storeOptions: [String: Any] = [
|
||||
NSSQLitePragmasOption: ["journal_mode": "WAL"],
|
||||
NSPersistentStoreUbiquitousContentNameKey: ubiquitousContentName
|
||||
]
|
||||
@@ -110,10 +110,10 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
- parameter observer: the observer to start sending ubiquitous notifications to
|
||||
*/
|
||||
public func addObserver<T: ICloudStoreObserver>(observer: T) {
|
||||
public func addObserver<T: ICloudStoreObserver>(_ observer: T) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to add an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
)
|
||||
|
||||
@@ -121,7 +121,7 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
self.registerNotification(
|
||||
&self.willFinishInitialImportKey,
|
||||
name: ICloudUbiquitousStoreWillFinishInitialImportNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreWillFinishInitialImport,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -130,7 +130,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.didFinishInitialImportKey,
|
||||
name: ICloudUbiquitousStoreDidFinishInitialImportNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreDidFinishInitialImport,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -139,7 +139,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.willAddAccountKey,
|
||||
name: ICloudUbiquitousStoreWillAddAccountNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreWillAddAccount,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -148,7 +148,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.didAddAccountKey,
|
||||
name: ICloudUbiquitousStoreDidAddAccountNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreDidAddAccount,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -157,7 +157,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.willRemoveAccountKey,
|
||||
name: ICloudUbiquitousStoreWillRemoveAccountNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreWillRemoveAccount,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -166,7 +166,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.didRemoveAccountKey,
|
||||
name: ICloudUbiquitousStoreDidRemoveAccountNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreDidRemoveAccount,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -175,7 +175,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.willRemoveContentKey,
|
||||
name: ICloudUbiquitousStoreWillRemoveContentNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreWillRemoveContent,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -184,7 +184,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
self.registerNotification(
|
||||
&self.didRemoveContentKey,
|
||||
name: ICloudUbiquitousStoreDidRemoveContentNotification,
|
||||
name: Notification.Name.iCloudUbiquitousStoreDidRemoveContent,
|
||||
toObserver: observer,
|
||||
callback: { (observer, storage, dataStack) in
|
||||
|
||||
@@ -198,10 +198,10 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
- parameter observer: the observer to stop sending ubiquitous notifications to
|
||||
*/
|
||||
public func removeObserver(observer: ICloudStoreObserver) {
|
||||
public func removeObserver(_ observer: ICloudStoreObserver) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to remove an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
)
|
||||
let nilValue: AnyObject? = nil
|
||||
@@ -266,12 +266,12 @@ public class ICloudStore: CloudStorage {
|
||||
[NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
```
|
||||
*/
|
||||
public let storeOptions: [String: AnyObject]?
|
||||
public let storeOptions: [AnyHashable: Any]?
|
||||
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.didRemoveFromDataStack(dataStack)
|
||||
|
||||
@@ -280,7 +280,7 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
notificationName: NSPersistentStoreCoordinatorStoresWillChangeNotification,
|
||||
notificationName: Notification.Name.NSPersistentStoreCoordinatorStoresWillChange,
|
||||
object: coordinator,
|
||||
closure: { [weak self, weak dataStack] (note) -> Void in
|
||||
|
||||
@@ -292,28 +292,28 @@ public class ICloudStore: CloudStorage {
|
||||
return
|
||||
}
|
||||
|
||||
let notification: String
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.unsignedIntegerValue) {
|
||||
let notification: Notification.Name
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.uintValue) {
|
||||
|
||||
case .InitialImportCompleted?:
|
||||
notification = ICloudUbiquitousStoreWillFinishInitialImportNotification
|
||||
case .initialImportCompleted?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreWillFinishInitialImport
|
||||
|
||||
case .AccountAdded?:
|
||||
notification = ICloudUbiquitousStoreWillAddAccountNotification
|
||||
case .accountAdded?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreWillAddAccount
|
||||
|
||||
case .AccountRemoved?:
|
||||
notification = ICloudUbiquitousStoreWillRemoveAccountNotification
|
||||
case .accountRemoved?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreWillRemoveAccount
|
||||
|
||||
case .ContentRemoved?:
|
||||
notification = ICloudUbiquitousStoreWillRemoveContentNotification
|
||||
case .contentRemoved?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreWillRemoveContent
|
||||
|
||||
default:
|
||||
return
|
||||
}
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
notification,
|
||||
NotificationCenter.default.post(
|
||||
name: notification,
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyDataStack: dataStack]
|
||||
userInfo: [String(describing: DataStack.self): dataStack]
|
||||
)
|
||||
}
|
||||
),
|
||||
@@ -322,7 +322,7 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
notificationName: NSPersistentStoreCoordinatorStoresDidChangeNotification,
|
||||
notificationName: NSNotification.Name.NSPersistentStoreCoordinatorStoresDidChange,
|
||||
object: coordinator,
|
||||
closure: { [weak self, weak dataStack] (note) -> Void in
|
||||
|
||||
@@ -334,28 +334,28 @@ public class ICloudStore: CloudStorage {
|
||||
return
|
||||
}
|
||||
|
||||
let notification: String
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.unsignedIntegerValue) {
|
||||
let notification: Notification.Name
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.uintValue) {
|
||||
|
||||
case .InitialImportCompleted?:
|
||||
notification = ICloudUbiquitousStoreDidFinishInitialImportNotification
|
||||
case .initialImportCompleted?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreDidFinishInitialImport
|
||||
|
||||
case .AccountAdded?:
|
||||
notification = ICloudUbiquitousStoreDidAddAccountNotification
|
||||
case .accountAdded?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreDidAddAccount
|
||||
|
||||
case .AccountRemoved?:
|
||||
notification = ICloudUbiquitousStoreDidRemoveAccountNotification
|
||||
case .accountRemoved?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreDidRemoveAccount
|
||||
|
||||
case .ContentRemoved?:
|
||||
notification = ICloudUbiquitousStoreDidRemoveContentNotification
|
||||
case .contentRemoved?:
|
||||
notification = Notification.Name.iCloudUbiquitousStoreDidRemoveContent
|
||||
|
||||
default:
|
||||
return
|
||||
}
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
notification,
|
||||
NotificationCenter.default.post(
|
||||
name: notification,
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyDataStack: dataStack]
|
||||
userInfo: [String(describing: DataStack.self): dataStack]
|
||||
)
|
||||
}
|
||||
),
|
||||
@@ -367,7 +367,7 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
let coordinator = dataStack.coordinator
|
||||
let nilValue: AnyObject? = nil
|
||||
@@ -391,7 +391,7 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
The `NSURL` that points to the ubiquity container file
|
||||
*/
|
||||
public let cacheFileURL: NSURL
|
||||
public let cacheFileURL: URL
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -401,20 +401,20 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
The options dictionary for the specified `CloudStorageOptions`
|
||||
*/
|
||||
public func storeOptionsForOptions(options: CloudStorageOptions) -> [String: AnyObject]? {
|
||||
public func dictionary(forOptions options: CloudStorageOptions) -> [AnyHashable: Any]? {
|
||||
|
||||
if options == .None {
|
||||
if options == .none {
|
||||
|
||||
return self.storeOptions
|
||||
}
|
||||
|
||||
var storeOptions = self.storeOptions ?? [:]
|
||||
if options.contains(.AllowSynchronousLightweightMigration) {
|
||||
if options.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
storeOptions[NSMigratePersistentStoresAutomaticallyOption] = true
|
||||
storeOptions[NSInferMappingModelAutomaticallyOption] = true
|
||||
}
|
||||
if options.contains(.RecreateLocalStoreOnModelMismatch) {
|
||||
if options.contains(.recreateLocalStoreOnModelMismatch) {
|
||||
|
||||
storeOptions[NSPersistentStoreRebuildFromUbiquitousContentOption] = true
|
||||
}
|
||||
@@ -424,48 +424,39 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
Called by the `DataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `SQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
|
||||
*/
|
||||
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws {
|
||||
public func eraseStorageAndWait(soureModel: NSManagedObjectModel) throws {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
let cacheFileURL = self.cacheFileURL
|
||||
guard let soureModel = soureModel else {
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
try fileManager.removeItemAtURL(cacheFileURL)
|
||||
_ = try? fileManager.removeItemAtPath("\(cacheFileURL.absoluteString)-wal")
|
||||
_ = try? fileManager.removeItemAtPath("\(cacheFileURL.absoluteString)-shm")
|
||||
return
|
||||
}
|
||||
try cs_autoreleasepool {
|
||||
try autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let options = [
|
||||
NSSQLitePragmasOption: ["journal_mode": "DELETE"],
|
||||
NSPersistentStoreRemoveUbiquitousMetadataOption: true
|
||||
]
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: cacheFileURL,
|
||||
] as [String : Any]
|
||||
let store = try journalUpdatingCoordinator.addPersistentStore(
|
||||
ofType: type(of: self).storeType,
|
||||
configurationName: self.configuration,
|
||||
at: cacheFileURL,
|
||||
options: options
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSPersistentStoreCoordinator.removeUbiquitousContentAndPersistentStoreAtURL(
|
||||
cacheFileURL,
|
||||
try journalUpdatingCoordinator.remove(store)
|
||||
try NSPersistentStoreCoordinator.removeUbiquitousContentAndPersistentStore(
|
||||
at: cacheFileURL,
|
||||
options: options
|
||||
)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(cacheFileURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private struct Static {
|
||||
fileprivate struct Static {
|
||||
|
||||
private static var persistentStoreCoordinatorWillChangeStores: Void?
|
||||
private static var persistentStoreCoordinatorDidChangeStores: Void?
|
||||
fileprivate static var persistentStoreCoordinatorWillChangeStores: Void?
|
||||
fileprivate static var persistentStoreCoordinatorDidChangeStores: Void?
|
||||
}
|
||||
|
||||
private var willFinishInitialImportKey: Void?
|
||||
@@ -479,7 +470,7 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
private weak var dataStack: DataStack?
|
||||
|
||||
private func registerNotification<T: ICloudStoreObserver>(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: T, callback: (observer: T, storage: ICloudStore, dataStack: DataStack) -> Void) {
|
||||
private func registerNotification<T: ICloudStoreObserver>(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: T, callback: @escaping (_ observer: T, _ storage: ICloudStore, _ dataStack: DataStack) -> Void) {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
@@ -489,12 +480,12 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
guard let `self` = self,
|
||||
let observer = observer,
|
||||
let dataStack = note.userInfo?[UserInfoKeyDataStack] as? DataStack
|
||||
where self.dataStack === dataStack else {
|
||||
let dataStack = note.userInfo?[String(describing: DataStack.self)] as? DataStack,
|
||||
self.dataStack === dataStack else {
|
||||
|
||||
return
|
||||
}
|
||||
callback(observer: observer, storage: self, dataStack: dataStack)
|
||||
callback(observer, self, dataStack)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -505,16 +496,17 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
|
||||
// MARK: - Notification Keys
|
||||
|
||||
private let ICloudUbiquitousStoreWillFinishInitialImportNotification = "ICloudUbiquitousStoreWillFinishInitialImportNotification"
|
||||
private let ICloudUbiquitousStoreDidFinishInitialImportNotification = "ICloudUbiquitousStoreDidFinishInitialImportNotification"
|
||||
private let ICloudUbiquitousStoreWillAddAccountNotification = "ICloudUbiquitousStoreWillAddAccountNotification"
|
||||
private let ICloudUbiquitousStoreDidAddAccountNotification = "ICloudUbiquitousStoreDidAddAccountNotification"
|
||||
private let ICloudUbiquitousStoreWillRemoveAccountNotification = "ICloudUbiquitousStoreWillRemoveAccountNotification"
|
||||
private let ICloudUbiquitousStoreDidRemoveAccountNotification = "ICloudUbiquitousStoreDidRemoveAccountNotification"
|
||||
private let ICloudUbiquitousStoreWillRemoveContentNotification = "ICloudUbiquitousStoreWillRemoveContentNotification"
|
||||
private let ICloudUbiquitousStoreDidRemoveContentNotification = "ICloudUbiquitousStoreDidRemoveContentNotification"
|
||||
|
||||
private let UserInfoKeyDataStack = "UserInfoKeyDataStack"
|
||||
|
||||
fileprivate extension Notification.Name {
|
||||
|
||||
fileprivate static let iCloudUbiquitousStoreWillFinishInitialImport = Notification.Name(rawValue: "iCloudUbiquitousStoreWillFinishInitialImport")
|
||||
fileprivate static let iCloudUbiquitousStoreDidFinishInitialImport = Notification.Name(rawValue: "iCloudUbiquitousStoreDidFinishInitialImport")
|
||||
fileprivate static let iCloudUbiquitousStoreWillAddAccount = Notification.Name(rawValue: "iCloudUbiquitousStoreWillAddAccount")
|
||||
fileprivate static let iCloudUbiquitousStoreDidAddAccount = Notification.Name(rawValue: "iCloudUbiquitousStoreDidAddAccount")
|
||||
fileprivate static let iCloudUbiquitousStoreWillRemoveAccount = Notification.Name(rawValue: "iCloudUbiquitousStoreWillRemoveAccount")
|
||||
fileprivate static let iCloudUbiquitousStoreDidRemoveAccount = Notification.Name(rawValue: "iCloudUbiquitousStoreDidRemoveAccount")
|
||||
fileprivate static let iCloudUbiquitousStoreWillRemoveContent = Notification.Name(rawValue: "iCloudUbiquitousStoreWillRemoveContent")
|
||||
fileprivate static let iCloudUbiquitousStoreDidRemoveContent = Notification.Name(rawValue: "iCloudUbiquitousStoreDidRemoveContent")
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -45,7 +45,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that the initial ubiquitous store import completed
|
||||
@@ -53,7 +53,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account will be added to the coordinator
|
||||
@@ -61,7 +61,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillAddAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillAddAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account was added to the coordinator
|
||||
@@ -69,7 +69,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidAddAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidAddAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account will be removed from the coordinator
|
||||
@@ -77,7 +77,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillRemoveAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillRemoveAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account was removed from the coordinator
|
||||
@@ -85,7 +85,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidRemoveAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidRemoveAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that iCloud contents will be deleted
|
||||
@@ -93,7 +93,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillRemoveContent(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillRemoveContent(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that iCloud contents were deleted
|
||||
@@ -101,26 +101,26 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidRemoveContent(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidRemoveContent(storage: ICloudStore, dataStack: DataStack)
|
||||
}
|
||||
|
||||
public extension ICloudStoreObserver {
|
||||
|
||||
public func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillAddAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillAddAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidAddAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidAddAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillRemoveAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillRemoveAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidRemoveAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidRemoveAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillRemoveContent(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillRemoveContent(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidRemoveContent(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidRemoveContent(storage: ICloudStore, dataStack: DataStack) {}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -69,12 +69,12 @@ public final class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
/**
|
||||
The options dictionary for the `NSPersistentStore`. For `InMemoryStore`s, this is always set to `nil`.
|
||||
*/
|
||||
public let storeOptions: [String: AnyObject]? = nil
|
||||
public let storeOptions: [AnyHashable: Any]? = nil
|
||||
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public final class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = nil
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileURL: URL, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = fileURL
|
||||
self.configuration = configuration
|
||||
@@ -61,11 +61,12 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = LegacySQLiteStore.defaultRootDirectory.URLByAppendingPathComponent(
|
||||
self.fileURL = LegacySQLiteStore.defaultRootDirectory.appendingPathComponent(
|
||||
fileName,
|
||||
isDirectory: false)!
|
||||
isDirectory: false
|
||||
)
|
||||
self.configuration = configuration
|
||||
self.mappingModelBundles = mappingModelBundles
|
||||
self.localStorageOptions = localStorageOptions
|
||||
@@ -83,7 +84,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
|
||||
self.fileURL = LegacySQLiteStore.defaultFileURL
|
||||
self.configuration = nil
|
||||
self.mappingModelBundles = NSBundle.allBundles()
|
||||
self.mappingModelBundles = Bundle.allBundles
|
||||
self.localStorageOptions = nil
|
||||
}
|
||||
|
||||
@@ -98,15 +99,15 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The options dictionary for the specified `LocalStorageOptions`
|
||||
*/
|
||||
public func storeOptionsForOptions(options: LocalStorageOptions) -> [String: AnyObject]? {
|
||||
public func dictionary(forOptions options: LocalStorageOptions) -> [AnyHashable: Any]? {
|
||||
|
||||
if options == .None {
|
||||
if options == .none {
|
||||
|
||||
return self.storeOptions
|
||||
}
|
||||
|
||||
var storeOptions = self.storeOptions ?? [:]
|
||||
if options.contains(.AllowSynchronousLightweightMigration) {
|
||||
if options.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
storeOptions[NSMigratePersistentStoresAutomaticallyOption] = true
|
||||
storeOptions[NSInferMappingModelAutomaticallyOption] = true
|
||||
@@ -125,12 +126,12 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
[NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
```
|
||||
*/
|
||||
public let storeOptions: [String: AnyObject]? = [NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
public let storeOptions: [AnyHashable: Any]? = [NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
@@ -138,7 +139,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = nil
|
||||
}
|
||||
@@ -149,12 +150,12 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The `NSURL` that points to the SQLite file
|
||||
*/
|
||||
public let fileURL: NSURL
|
||||
public let fileURL: URL
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
public let mappingModelBundles: [NSBundle]
|
||||
public let mappingModelBundles: [Bundle]
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -164,53 +165,90 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Called by the `DataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `SQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
|
||||
*/
|
||||
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws {
|
||||
public func eraseStorageAndWait(metadata: [String: Any], soureModelHint: NSManagedObjectModel?) throws {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
let fileURL = self.fileURL
|
||||
guard let soureModel = soureModel else {
|
||||
func deleteFiles(storeURL: URL, extraFiles: [String] = []) throws {
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
try fileManager.removeItemAtURL(fileURL)
|
||||
_ = try? fileManager.removeItemAtPath("\(fileURL.absoluteString)-wal")
|
||||
_ = try? fileManager.removeItemAtPath("\(fileURL.absoluteString)-shm")
|
||||
return
|
||||
let fileManager = FileManager.default
|
||||
let extraFiles: [String] = [
|
||||
storeURL.path.appending("-wal"),
|
||||
storeURL.path.appending("-shm")
|
||||
]
|
||||
do {
|
||||
|
||||
let trashURL = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!)
|
||||
.appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack", isDirectory: true)
|
||||
.appendingPathComponent("trash", isDirectory: true)
|
||||
try fileManager.createDirectory(
|
||||
at: trashURL,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let temporaryFileURL = trashURL.appendingPathComponent(UUID().uuidString, isDirectory: false)
|
||||
try fileManager.moveItem(at: storeURL, to: temporaryFileURL)
|
||||
|
||||
let extraTemporaryFiles = extraFiles.map { (extraFile) -> String in
|
||||
|
||||
let temporaryFile = trashURL.appendingPathComponent(UUID().uuidString, isDirectory: false).path
|
||||
if let _ = try? fileManager.moveItem(atPath: extraFile, toPath: temporaryFile) {
|
||||
|
||||
return temporaryFile
|
||||
}
|
||||
return extraFile
|
||||
}
|
||||
DispatchQueue.global(qos: .background).async {
|
||||
|
||||
_ = try? fileManager.removeItem(at: temporaryFileURL)
|
||||
extraTemporaryFiles.forEach({ _ = try? fileManager.removeItem(atPath: $0) })
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
try fileManager.removeItem(at: storeURL)
|
||||
extraFiles.forEach({ _ = try? fileManager.removeItem(atPath: $0) })
|
||||
}
|
||||
}
|
||||
try cs_autoreleasepool {
|
||||
|
||||
let fileURL = self.fileURL
|
||||
try autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
|
||||
if let soureModel = soureModelHint ?? NSManagedObjectModel.mergedModel(from: nil, forStoreMetadata: metadata) {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let store = try journalUpdatingCoordinator.addPersistentStore(
|
||||
ofType: type(of: self).storeType,
|
||||
configurationName: self.configuration,
|
||||
at: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try journalUpdatingCoordinator.remove(store)
|
||||
}
|
||||
try deleteFiles(storeURL: fileURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static let defaultRootDirectory: NSURL = {
|
||||
internal static let defaultRootDirectory: URL = {
|
||||
|
||||
#if os(tvOS)
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
|
||||
let systemDirectorySearchPath = FileManager.SearchPathDirectory.cachesDirectory
|
||||
#else
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
|
||||
let systemDirectorySearchPath = FileManager.SearchPathDirectory.applicationSupportDirectory
|
||||
#endif
|
||||
|
||||
return NSFileManager.defaultManager().URLsForDirectory(
|
||||
systemDirectorySearchPath,
|
||||
inDomains: .UserDomainMask
|
||||
).first!
|
||||
return FileManager.default.urls(
|
||||
for: systemDirectorySearchPath,
|
||||
in: .userDomainMask).first!
|
||||
}()
|
||||
|
||||
internal static let defaultFileURL = LegacySQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(DataStack.applicationName, isDirectory: false)!
|
||||
.URLByAppendingPathExtension("sqlite")!
|
||||
.appendingPathComponent(DataStack.applicationName, isDirectory: false)
|
||||
.appendingPathExtension("sqlite")
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user