Core Data Reset [duplicate]

天涯浪子 提交于 2019-11-29 18:26:51

I do not know if this is the reason, but after you set

__managedObjectContext = nil;
__persistentStoreCoordinator = nil;

you call

if ([[self.managedObjectContext persistentStoreCoordinator] removePersistentStore:      [[[self.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])

so if you have getter methods (as default xcode creates them) you initialize them again, if not then you call methods on nil objects.

Edit: trying to explain (better)

After you set

__managedObjectContext = nil;

you call

[self.managedObjectContext persistentStoreCoordinator]

which means

[nil persistentStoreCoordinator]

unless you have

- (NSManagedObjectContext *)managedObjectContext

method in the class. if you have the method it creates managedObjectContext for the same(old) data model again.

So if you want to create new coordinator, context... with new model file, then you need to set them nil after you remove the old file.

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