Skip to content

Instantly share code, notes, and snippets.

@robnadin
Last active August 29, 2015 14:26
Show Gist options
  • Save robnadin/3a4fe58c19de1f70571a to your computer and use it in GitHub Desktop.
Save robnadin/3a4fe58c19de1f70571a to your computer and use it in GitHub Desktop.
An extendable protocol for NSManagedObject subclasses for simple data fetching
//
// Fetchable.swift
// CoreDataGenerics
//
// Created by Rob Nadin on 29/07/2015.
// Copyright © 2015 Rob Nadin. All rights reserved.
//
import CoreData
protocol Fetchable {
typealias FetchableType: NSManagedObject = Self
static var entityName: String { get }
static func objectsInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> [FetchableType]
static func singleObjectInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> FetchableType?
static func objectCountInContext(context: NSManagedObjectContext, predicate: NSPredicate?) -> Int
static func fetchRequest(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) -> NSFetchRequest
}
extension Fetchable where Self: NSManagedObject, FetchableType == Self {
static var entityName: String {
let components = NSStringFromClass(FetchableType).componentsSeparatedByString(".")
return components.last ?? components.first!
}
static func singleObjectInContext(context: NSManagedObjectContext, predicate: NSPredicate? = nil, sortedBy: String? = nil, ascending: Bool = false) throws -> FetchableType? {
let results: [FetchableType] = try objectsInContext(context, predicate: predicate, sortedBy: sortedBy, ascending: ascending)
guard results.count > 0 else {
return nil
}
return results.first
}
static func objectCountInContext(context: NSManagedObjectContext, predicate: NSPredicate? = nil) -> Int {
let request = fetchRequest(context, predicate: predicate)
let error: NSErrorPointer = nil
let count = context.countForFetchRequest(request, error: error)
guard error != nil else {
return 0
}
return count
}
static func objectsInContext(context: NSManagedObjectContext, predicate: NSPredicate? = nil, sortedBy: String? = nil, ascending: Bool = false) throws -> [FetchableType] {
let request = fetchRequest(context, predicate: predicate, sortedBy: sortedBy, ascending: ascending)
return try context.executeFetchRequest(request) as! [FetchableType]
}
static func fetchRequest(context: NSManagedObjectContext, predicate: NSPredicate? = nil, sortedBy: String? = nil, ascending: Bool = false) -> NSFetchRequest {
let request = NSFetchRequest(entityName: entityName)
request.predicate = predicate
if let key = sortedBy {
request.sortDescriptors = [NSSortDescriptor(key: key, ascending: ascending)]
}
return request
}
}
//
// Event.swift
// CoreDataGenerics
//
// Created by Rob Nadin on 29/07/2015.
// Copyright © 2015 Rob Nadin. All rights reserved.
//
import Foundation
import CoreData
@objc(Event)
class Event: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
extension Event: Fetchable {}
do {
let events = try Event.objectsInContext(managedObjectContext)
for event in events {
print(event)
}
} catch {
print("Couldn't get Event objects")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment