mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-31 06:33:06 +02:00
Tighter generics implementations. You can now pass any SequenceType's for methods that previously only accepts Array's.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
Pod::Spec.new do |s|
|
Pod::Spec.new do |s|
|
||||||
s.name = "CoreStore"
|
s.name = "CoreStore"
|
||||||
s.version = "1.3.0"
|
s.version = "1.3.1"
|
||||||
s.license = "MIT"
|
s.license = "MIT"
|
||||||
s.summary = "Simple, elegant, and smart Core Data programming with Swift"
|
s.summary = "Simple, elegant, and smart Core Data programming with Swift"
|
||||||
s.homepage = "https://github.com/JohnEstropia/CoreStore"
|
s.homepage = "https://github.com/JohnEstropia/CoreStore"
|
||||||
|
|||||||
@@ -78,17 +78,9 @@ public extension BaseDataTransaction {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func fetchExisting<T: NSManagedObject>(objects: [T]) -> [T] {
|
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||||
|
|
||||||
var existingObjects = [T]()
|
return objects.flatMap { (try? self.context.existingObjectWithID($0.objectID)) as? T }
|
||||||
for object in objects {
|
|
||||||
|
|
||||||
if let existingObject = (try? self.context.existingObjectWithID(object.objectID)) as? T {
|
|
||||||
|
|
||||||
existingObjects.append(existingObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return existingObjects
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,17 +90,9 @@ public extension BaseDataTransaction {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func fetchExisting<T: NSManagedObject>(objectIDs: [NSManagedObjectID]) -> [T] {
|
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||||
|
|
||||||
var existingObjects = [T]()
|
return objectIDs.flatMap { (try? self.context.existingObjectWithID($0)) as? T }
|
||||||
for objectID in objectIDs {
|
|
||||||
|
|
||||||
if let existingObject = (try? self.context.existingObjectWithID(objectID)) as? T {
|
|
||||||
|
|
||||||
existingObjects.append(existingObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return existingObjects
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public extension CoreStore {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public static func fetchExisting<T: NSManagedObject>(objects: [T]) -> [T] {
|
public static func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||||
|
|
||||||
return self.defaultStack.fetchExisting(objects)
|
return self.defaultStack.fetchExisting(objects)
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ public extension CoreStore {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public static func fetchExisting<T: NSManagedObject>(objectIDs: [NSManagedObjectID]) -> [T] {
|
public static func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||||
|
|
||||||
return self.defaultStack.fetchExisting(objectIDs)
|
return self.defaultStack.fetchExisting(objectIDs)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,17 +79,9 @@ public extension DataStack {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func fetchExisting<T: NSManagedObject>(objects: [T]) -> [T] {
|
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == T>(objects: S) -> [T] {
|
||||||
|
|
||||||
var existingObjects = [T]()
|
return objects.flatMap { (try? self.mainContext.existingObjectWithID($0.objectID)) as? T }
|
||||||
for object in objects {
|
|
||||||
|
|
||||||
if let existingObject = (try? self.mainContext.existingObjectWithID(object.objectID)) as? T {
|
|
||||||
|
|
||||||
existingObjects.append(existingObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return existingObjects
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,17 +91,9 @@ public extension DataStack {
|
|||||||
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
|
||||||
*/
|
*/
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func fetchExisting<T: NSManagedObject>(objectIDs: [NSManagedObjectID]) -> [T] {
|
public func fetchExisting<T: NSManagedObject, S: SequenceType where S.Generator.Element == NSManagedObjectID>(objectIDs: S) -> [T] {
|
||||||
|
|
||||||
var existingObjects = [T]()
|
return objectIDs.flatMap { (try? self.mainContext.existingObjectWithID($0)) as? T }
|
||||||
for objectID in objectIDs {
|
|
||||||
|
|
||||||
if let existingObject = (try? self.mainContext.existingObjectWithID(objectID)) as? T {
|
|
||||||
|
|
||||||
existingObjects.append(existingObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return existingObjects
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -102,9 +102,9 @@ public extension BaseDataTransaction {
|
|||||||
- parameter sourceArray: the array of objects to import values from
|
- parameter sourceArray: the array of objects to import values from
|
||||||
- parameter postProcess: a closure that exposes the array of created objects
|
- parameter postProcess: a closure that exposes the array of created objects
|
||||||
*/
|
*/
|
||||||
public func importObjects<T where T: NSManagedObject, T: ImportableObject>(
|
public func importObjects<T, S: SequenceType where T: NSManagedObject, T: ImportableObject, S.Generator.Element == T.ImportSource>(
|
||||||
into: Into<T>,
|
into: Into<T>,
|
||||||
sourceArray: [T.ImportSource],
|
sourceArray: S,
|
||||||
@noescape postProcess: (sorted: [T]) -> Void) throws {
|
@noescape postProcess: (sorted: [T]) -> Void) throws {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
@@ -185,10 +185,10 @@ public extension BaseDataTransaction {
|
|||||||
- parameter sourceArray: the array of objects to import values from
|
- 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.
|
- 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.
|
||||||
*/
|
*/
|
||||||
public func importUniqueObjects<T where T: NSManagedObject, T: ImportableUniqueObject>(
|
public func importUniqueObjects<T, S: SequenceType where T: NSManagedObject, T: ImportableUniqueObject, S.Generator.Element == T.ImportSource>(
|
||||||
into: Into<T>,
|
into: Into<T>,
|
||||||
sourceArray: [T.ImportSource],
|
sourceArray: S,
|
||||||
preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil) throws {
|
@noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
|
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
|
||||||
@@ -211,13 +211,7 @@ public extension BaseDataTransaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let preProcess = preProcess {
|
mapping = try autoreleasepool { try preProcess(mapping: mapping) }
|
||||||
|
|
||||||
try autoreleasepool {
|
|
||||||
|
|
||||||
mapping = try preProcess(mapping: mapping)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] {
|
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] {
|
||||||
|
|
||||||
@@ -260,10 +254,10 @@ public extension BaseDataTransaction {
|
|||||||
- 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.
|
- 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.
|
||||||
- parameter postProcess: a closure that exposes the array of created/updated objects
|
- parameter postProcess: a closure that exposes the array of created/updated objects
|
||||||
*/
|
*/
|
||||||
public func importUniqueObjects<T where T: NSManagedObject, T: ImportableUniqueObject>(
|
public func importUniqueObjects<T, S: SequenceType where T: NSManagedObject, T: ImportableUniqueObject, S.Generator.Element == T.ImportSource>(
|
||||||
into: Into<T>,
|
into: Into<T>,
|
||||||
sourceArray: [T.ImportSource],
|
sourceArray: S,
|
||||||
preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil,
|
@noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 },
|
||||||
@noescape postProcess: (sorted: [T]) -> Void) throws {
|
@noescape postProcess: (sorted: [T]) -> Void) throws {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
@@ -289,13 +283,7 @@ public extension BaseDataTransaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let preProcess = preProcess {
|
mapping = try autoreleasepool { try preProcess(mapping: mapping) }
|
||||||
|
|
||||||
try autoreleasepool {
|
|
||||||
|
|
||||||
mapping = try preProcess(mapping: mapping)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var objects = Dictionary<T.UniqueIDType, T>()
|
var objects = Dictionary<T.UniqueIDType, T>()
|
||||||
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] {
|
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<key>CFBundlePackageType</key>
|
<key>CFBundlePackageType</key>
|
||||||
<string>FMWK</string>
|
<string>FMWK</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>1.3.0</string>
|
<string>1.3.1</string>
|
||||||
<key>CFBundleSignature</key>
|
<key>CFBundleSignature</key>
|
||||||
<string>????</string>
|
<string>????</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
|
|||||||
@@ -25,9 +25,9 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
internal func autoreleasepool<T>(@noescape closure: () -> T?) -> T? {
|
internal func autoreleasepool<T>(@noescape closure: () -> T) -> T {
|
||||||
|
|
||||||
var closureValue: T?
|
var closureValue: T!
|
||||||
ObjectiveC.autoreleasepool {
|
ObjectiveC.autoreleasepool {
|
||||||
|
|
||||||
closureValue = closure()
|
closureValue = closure()
|
||||||
@@ -36,9 +36,9 @@ internal func autoreleasepool<T>(@noescape closure: () -> T?) -> T? {
|
|||||||
return closureValue
|
return closureValue
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func autoreleasepool<T>(@noescape closure: () throws -> T?) throws -> T? {
|
internal func autoreleasepool<T>(@noescape closure: () throws -> T) throws -> T {
|
||||||
|
|
||||||
var closureValue: T?
|
var closureValue: T!
|
||||||
var closureError: ErrorType?
|
var closureError: ErrorType?
|
||||||
ObjectiveC.autoreleasepool {
|
ObjectiveC.autoreleasepool {
|
||||||
|
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
|
|||||||
"Attempted to delete an entities from an already committed \(typeName(self))."
|
"Attempted to delete an entities from an already committed \(typeName(self))."
|
||||||
)
|
)
|
||||||
|
|
||||||
super.delete([object1, object2] + objects)
|
super.delete(([object1, object2] + objects).flatMap { $0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -179,7 +179,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
|
|||||||
|
|
||||||
- parameter objects: the `NSManagedObject`s type to be deleted
|
- parameter objects: the `NSManagedObject`s type to be deleted
|
||||||
*/
|
*/
|
||||||
public override func delete(objects: [NSManagedObject?]) {
|
public override func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
!self.isCommitted,
|
!self.isCommitted,
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ public /*abstract*/ class BaseDataTransaction {
|
|||||||
*/
|
*/
|
||||||
public func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) {
|
public func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) {
|
||||||
|
|
||||||
self.delete([object1, object2] + objects)
|
self.delete(([object1, object2] + objects).flatMap { $0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -175,7 +175,7 @@ public /*abstract*/ class BaseDataTransaction {
|
|||||||
|
|
||||||
- parameter objects: the `NSManagedObject`s to be deleted
|
- parameter objects: the `NSManagedObject`s to be deleted
|
||||||
*/
|
*/
|
||||||
public func delete(objects: [NSManagedObject?]) {
|
public func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
|
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
|
||||||
@@ -183,10 +183,7 @@ public /*abstract*/ class BaseDataTransaction {
|
|||||||
)
|
)
|
||||||
|
|
||||||
let context = self.context
|
let context = self.context
|
||||||
for case let object? in objects {
|
objects.forEach { context.fetchExisting($0)?.deleteFromContext() }
|
||||||
|
|
||||||
context.fetchExisting(object)?.deleteFromContext()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Saving changes
|
// MARK: Saving changes
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
|
|||||||
"Attempted to delete an entities from an already committed \(typeName(self))."
|
"Attempted to delete an entities from an already committed \(typeName(self))."
|
||||||
)
|
)
|
||||||
|
|
||||||
super.delete([object1, object2] + objects)
|
super.delete(([object1, object2] + objects).flatMap { $0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -169,7 +169,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
|
|||||||
|
|
||||||
- parameter objects: the `NSManagedObject`s to be deleted
|
- parameter objects: the `NSManagedObject`s to be deleted
|
||||||
*/
|
*/
|
||||||
public override func delete(objects: [NSManagedObject?]) {
|
public override func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
|
||||||
|
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
!self.isCommitted,
|
!self.isCommitted,
|
||||||
|
|||||||
Reference in New Issue
Block a user