Last active
July 4, 2018 20:42
-
-
Save andrehsouza/4993fa33f4dfebe05f0dac90820b1e02 to your computer and use it in GitHub Desktop.
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 | |
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