Skip to content

Instantly share code, notes, and snippets.

@dglancy
Last active August 29, 2015 14:07
Show Gist options
  • Save dglancy/2f1e313cfcc7d61cff8c to your computer and use it in GitHub Desktop.
Save dglancy/2f1e313cfcc7d61cff8c to your computer and use it in GitHub Desktop.
extension NSManagedObjectContext {
//MARK: - Public Properties
public class var defaultContext: NSManagedObjectContext! {
get {
let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
context.persistentStoreCoordinator = self.persistentStoreCoordinator // This provided by more extension properties
return context
}
}
}
extension NSManagedObject {
override convenience init() {
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: NSManagedObjectContext.defaultContext) // Person will be replaced with utility property if I get this appraoch working
self.init(entity: entity!, insertIntoManagedObjectContext: NSManagedObjectContext.defaultContext)
}
}
public class Person: NSManagedObject {
@NSManaged var name: String
@NSManaged var dateOfBirth: NSDate
}
Unit test:
func testCreateEntity() {
let entity = Person() // <=== compile error: Missing argument for parameter 'entity' in call
XCTAssertNotNil(entity, "entity should not be nil")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment