Use NSManagedObject class without initWithEntity:?

后端 未结 2 373
深忆病人
深忆病人 2020-12-16 02:41

My problem is similar to: Problem creating NSManagedObject derived class

I have setup a NSManagedObject in Core Data and have a class for it. However, instead of cre

相关标签:
2条回答
  • 2020-12-16 02:47

    If you don't save the MOC, then you can simply delete the object before the save and it will never be persisted.

    While Core Data is great for persisting, it is not required. In fact MOCs are often described as a scratch pad. You can generate objects and then throw them away.

    An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application.

    Another solution is to have a separate MOC for temporary objects and then either throw away the temp MOC or move the MOs into your persistent MOC.

    So in this case you would - (void)insertObject:(NSManagedObject *)object on the "Persistent MOC" and then - (void)deleteObject:(NSManagedObject *)object on the "Temporary MOC".

    0 讨论(0)
  • 2020-12-16 03:02

    Just use initWithEntity:insertIntoManagedObjectContext: and pass a nil context, then call insertObject: in your NSMAnagedObjectContext when you are ready:

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyModelClass" inManagedObjectContext:myContext];
    id object = [[MyModelClass alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
    
    0 讨论(0)
提交回复
热议问题