Use NSManagedObject class without initWithEntity:?

时间秒杀一切 提交于 2019-11-29 02:01:03

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];
logancautrell

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".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!