Skip to content

Instantly share code, notes, and snippets.

@andrehsouza
Last active July 4, 2018 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrehsouza/4993fa33f4dfebe05f0dac90820b1e02 to your computer and use it in GitHub Desktop.
Save andrehsouza/4993fa33f4dfebe05f0dac90820b1e02 to your computer and use it in GitHub Desktop.
import Foundation
import CoreData
protocol FetchableProtocol: class {
associatedtype FetchableType: NSManagedObject = Self
}
extension FetchableProtocol where Self : NSManagedObject, FetchableType == Self {
static func fetchAll() -> [FetchableType] {
let fetchRequest = NSFetchRequest<FetchableType>(entityName: objectName)
do {
let array = try CoreDataStack.sharedInstance.persistentContainer.viewContext.fetch(fetchRequest) as [FetchableType]
return array
} catch let errore {
print("error FetchRequest \(errore)")
}
return []
}
}
class CustomNSManagedObject: NSManagedObject {
convenience init() {
self.init(context: CoreDataStack.sharedInstance.persistentContainer.viewContext)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment