我目前正在开发一个已经使用 Objective-C 开发的 IOS 应用程序。我添加了一个模块,用户在登录时存储有关用户的详细信息。但由于应用程序已经有一些代码,当我按下注销按钮时,它会从数据库中删除所有实体。为此,他们正在使用类似下面的代码。
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSError *error = nil;
// retrieve the store URL
NSURL *storeURL = [[managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
// lock the current context
[managedObjectContext lock];
[managedObjectContext reset];//to drop pending changes
//delete the store from the current managedObjectContext
if ([[managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error]){
// remove the file containing the data
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
//recreate the store like in the appDelegate method
[[managedObjectContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];//recreates the persistent store
}
[managedObjectContext unlock];
通过保留断点,我了解到他们正在检索数据库的 url 并将其删除并重新创建它。假设他们有 3 个表 A、B 和 C,我想删除 A 和 B 但不是 C。参考 - 持久存储协调器
我的理解正确吗?我怎样才能做到这一点?
TIA