Created
April 15, 2016 15:23
-
-
Save joshuadutton/964ddfee6067953028c9b5a726fa8986 to your computer and use it in GitHub Desktop.
Functional Core Data in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreData | |
extension NSFetchRequest { | |
func withPredicate(format: String, arguments: [AnyObject]? = nil) -> NSFetchRequest { | |
return withPredicate(NSPredicate(format: format, argumentArray: arguments)) | |
} | |
func andPredicate(format: String, arguments: [AnyObject]? = nil) -> NSFetchRequest { | |
return andPredicates(NSPredicate(format: format, argumentArray: arguments)) | |
} | |
func withPredicate(predicate: NSPredicate?) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.predicate = predicate | |
return request | |
} | |
func andPredicates(predicates: NSPredicate...) -> NSFetchRequest { | |
return andPredicates(predicates) | |
} | |
func andPredicates(predicates: [NSPredicate]) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
if let originalPredicate = request.predicate { | |
request.predicate = originalPredicate.and(predicates) | |
} else { | |
request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) | |
} | |
return request | |
} | |
func withSortDescriptors(descriptors: NSSortDescriptor...) -> NSFetchRequest { | |
return withSortDescriptors(descriptors) | |
} | |
func withSortDescriptors(descriptors: [NSSortDescriptor]?) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.sortDescriptors = descriptors | |
return request | |
} | |
func appendingSortDescriptors(descriptors: NSSortDescriptor...) -> NSFetchRequest { | |
return appendingSortDescriptors(descriptors) | |
} | |
func appendingSortDescriptors(descriptors: [NSSortDescriptor]) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
if request.sortDescriptors != nil { | |
request.sortDescriptors?.appendContentsOf(descriptors) | |
} else { | |
request.sortDescriptors = descriptors | |
} | |
return request | |
} | |
func withLimit(limit: Int) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.fetchLimit = limit | |
return request | |
} | |
func withOffset(offset: Int) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.fetchOffset = offset | |
return request | |
} | |
func withBatchSize(size: Int) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.fetchBatchSize = size | |
return request | |
} | |
func forPropertyWithKeyPath(keyPath: String) -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.resultType = .DictionaryResultType | |
request.propertiesToFetch = [keyPath] | |
return request | |
} | |
func forObjectIDs() -> NSFetchRequest { | |
let request = self.copy() as! NSFetchRequest | |
request.resultType = .ManagedObjectIDResultType | |
return request | |
} | |
func execute<T: AnyObject>(context: NSManagedObjectContext) -> [T] { | |
do { | |
let results = try context.executeFetchRequest(self) | |
if let results = results as? [T] { | |
return results | |
} | |
if let propertiesToFetch = propertiesToFetch | |
where resultType == .DictionaryResultType && propertiesToFetch.count == 1 { | |
let propertyDescription = propertiesToFetch.first as! NSPropertyDescription | |
let dictionaryResults = results as! [NSDictionary] | |
return dictionaryResults.map { | |
return $0.objectForKey(propertyDescription.name) as! T | |
} | |
} | |
} catch { | |
LogError(error) | |
fatalError(String(error)) | |
} | |
return [] | |
} | |
func count(context: NSManagedObjectContext) -> Int { | |
var error: NSError? | |
let count = context.countForFetchRequest(self, error: &error) | |
if let error = error where count == NSNotFound { | |
LogError(error) | |
fatalError(String(error)) | |
} | |
return count | |
} | |
func firstObject<T: NSManagedObject>(context: NSManagedObjectContext) -> T? { | |
return withLimit(1).withOffset(0).execute(context).first | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreData | |
extension NSManagedObject { | |
class func entityName() -> String { | |
return NSStringFromClass(self).componentsSeparatedByString(".").last! | |
} | |
class func fetchRequest() -> NSFetchRequest { | |
return NSFetchRequest(entityName: entityName()) | |
} | |
class func createInContext<T where T: NSManagedObject>(context: NSManagedObjectContext) -> T { | |
return NSEntityDescription.insertNewObjectForEntityForName(entityName(), inManagedObjectContext: context) as! T | |
} | |
// MARK: - Predicates | |
class func predicateWithObjectIDs(objectIDs: [NSManagedObjectID]) -> NSPredicate { | |
return NSPredicate(format: "self IN %@", argumentArray: [objectIDs]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension NSPredicate { | |
var not: NSPredicate { | |
return NSCompoundPredicate(notPredicateWithSubpredicate: self) | |
} | |
func and(predicates: NSPredicate...) -> NSPredicate { | |
return and(predicates) | |
} | |
func and(predicates: [NSPredicate]) -> NSPredicate { | |
var allPredicates = [self] | |
allPredicates.appendContentsOf(predicates) | |
return NSCompoundPredicate(andPredicateWithSubpredicates: allPredicates) | |
} | |
func and(format: String, arguments: [AnyObject]? = nil) -> NSPredicate { | |
return and([NSPredicate(format: format, argumentArray: arguments)]) | |
} | |
func or(predicates: NSPredicate...) -> NSPredicate { | |
return or(predicates) | |
} | |
func or(predicates: [NSPredicate]) -> NSPredicate { | |
var allPredicates = [self] | |
allPredicates.appendContentsOf(predicates) | |
return NSCompoundPredicate(orPredicateWithSubpredicates: allPredicates) | |
} | |
func or(format: String, arguments: [AnyObject]? = nil) -> NSPredicate { | |
return or([NSPredicate(format: format, argumentArray: arguments)]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment