Skip to content

Instantly share code, notes, and snippets.

@rbsgn
Created October 3, 2016 18:50
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 rbsgn/d02aa6d1039befd3f6d747cdd0a7e7f8 to your computer and use it in GitHub Desktop.
Save rbsgn/d02aa6d1039befd3f6d747cdd0a7e7f8 to your computer and use it in GitHub Desktop.
import CoreData
func newManagedObjectModel() -> NSManagedObjectModel {
// you get new instance of MOM with the same properties
// each time you call the function.
// this function uses entity description returned by
// newEntityDescription function
}
func newEntityDescription() -> NSEntityDescription {
// again, you get new instance of entity description
// with same properties each time you call the function
}
final class ManagedFoo: NSManagedObject {
@NSManaged var bar: String
}
func test_SavingWithManagedObjectContext() {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: newManagedObjectModel())
let context = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
context.persistentStoreCoordinator = coordinator
let foo = ManagedFoo(entity: newEntityDescription(), insertIntoManagedObjectContext: nil)
context.insert(foo)
try! context.save() // code will crash here with pretty obscure error 134020 “Store cannot hold instances of entity”
}
@rbsgn
Copy link
Author

rbsgn commented Oct 3, 2016

One solution:

let managedObjectModel: NSManagedObjectModel = {
  // you get new instance of MOM with the same properties
  // each time you call the function.
  // this function uses entity description returned by 
  // newEntityDescription function
}()

let newEntityDescription: NSEntityDescription = {
  // again, you get new instance of entity description
  // with same properties each time you call the function
}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment