Tighter generics implementations. You can now pass any SequenceType's for methods that previously only accepts Array's.

This commit is contained in:
John Rommel Estropia
2015-09-19 18:20:52 +09:00
parent 7451fbe026
commit 114b7ce605
10 changed files with 33 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CoreStore"
s.version = "1.3.0"
s.version = "1.3.1"
s.license = "MIT"
s.summary = "Simple, elegant, and smart Core Data programming with Swift"
s.homepage = "https://github.com/JohnEstropia/CoreStore"

View File

@@ -78,17 +78,9 @@ public extension BaseDataTransaction {
- returns: the `NSManagedObject` array for objects that exists in the transaction
*/
@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]()
for object in objects {
if let existingObject = (try? self.context.existingObjectWithID(object.objectID)) as? T {
existingObjects.append(existingObject)
}
}
return existingObjects
return objects.flatMap { (try? self.context.existingObjectWithID($0.objectID)) as? T }
}
/**
@@ -98,17 +90,9 @@ public extension BaseDataTransaction {
- returns: the `NSManagedObject` array for objects that exists in the transaction
*/
@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]()
for objectID in objectIDs {
if let existingObject = (try? self.context.existingObjectWithID(objectID)) as? T {
existingObjects.append(existingObject)
}
}
return existingObjects
return objectIDs.flatMap { (try? self.context.existingObjectWithID($0)) as? T }
}
/**

View File

@@ -62,7 +62,7 @@ public extension CoreStore {
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
*/
@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)
}
@@ -74,7 +74,7 @@ public extension CoreStore {
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
*/
@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)
}

View File

@@ -79,17 +79,9 @@ public extension DataStack {
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
*/
@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]()
for object in objects {
if let existingObject = (try? self.mainContext.existingObjectWithID(object.objectID)) as? T {
existingObjects.append(existingObject)
}
}
return existingObjects
return objects.flatMap { (try? self.mainContext.existingObjectWithID($0.objectID)) as? T }
}
/**
@@ -99,17 +91,9 @@ public extension DataStack {
- returns: the `NSManagedObject` array for objects that exists in the `DataStack`
*/
@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]()
for objectID in objectIDs {
if let existingObject = (try? self.mainContext.existingObjectWithID(objectID)) as? T {
existingObjects.append(existingObject)
}
}
return existingObjects
return objectIDs.flatMap { (try? self.mainContext.existingObjectWithID($0)) as? T }
}
/**

View File

@@ -102,9 +102,9 @@ public extension BaseDataTransaction {
- parameter sourceArray: the array of objects to import values from
- 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>,
sourceArray: [T.ImportSource],
sourceArray: S,
@noescape postProcess: (sorted: [T]) -> Void) throws {
CoreStore.assert(
@@ -185,10 +185,10 @@ public extension BaseDataTransaction {
- 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.
*/
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>,
sourceArray: [T.ImportSource],
preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil) throws {
sourceArray: S,
@noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws {
CoreStore.assert(
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
@@ -211,13 +211,7 @@ public extension BaseDataTransaction {
}
}
if let preProcess = preProcess {
try autoreleasepool {
mapping = try preProcess(mapping: mapping)
}
}
mapping = try autoreleasepool { try preProcess(mapping: mapping) }
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 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>,
sourceArray: [T.ImportSource],
preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil,
sourceArray: S,
@noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 },
@noescape postProcess: (sorted: [T]) -> Void) throws {
CoreStore.assert(
@@ -289,13 +283,7 @@ public extension BaseDataTransaction {
}
}
if let preProcess = preProcess {
try autoreleasepool {
mapping = try preProcess(mapping: mapping)
}
}
mapping = try autoreleasepool { try preProcess(mapping: mapping) }
var objects = Dictionary<T.UniqueIDType, T>()
for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] {

View File

@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.0</string>
<string>1.3.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>

View File

@@ -25,9 +25,9 @@
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 {
closureValue = closure()
@@ -36,9 +36,9 @@ internal func autoreleasepool<T>(@noescape closure: () -> T?) -> T? {
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?
ObjectiveC.autoreleasepool {

View File

@@ -171,7 +171,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
"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
*/
public override func delete(objects: [NSManagedObject?]) {
public override func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
CoreStore.assert(
!self.isCommitted,

View File

@@ -167,7 +167,7 @@ public /*abstract*/ class BaseDataTransaction {
*/
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
*/
public func delete(objects: [NSManagedObject?]) {
public func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
CoreStore.assert(
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
@@ -183,10 +183,7 @@ public /*abstract*/ class BaseDataTransaction {
)
let context = self.context
for case let object? in objects {
context.fetchExisting(object)?.deleteFromContext()
}
objects.forEach { context.fetchExisting($0)?.deleteFromContext() }
}
// MARK: Saving changes

View File

@@ -161,7 +161,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
"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
*/
public override func delete(objects: [NSManagedObject?]) {
public override func delete<S: SequenceType where S.Generator.Element == NSManagedObject>(objects: S) {
CoreStore.assert(
!self.isCommitted,