我正在使用Microsoft Azure Web服务创建应用程序。在单击注销按钮时,我需要完全清除本地核心数据库,并在使用相同的用户名和密码再次登录后重新加载所有数据。要删除核心数据,请使用以下方法
- (void) resetApplicationModel {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;
for (NSPersistentStore *store in delegate.persistentStoreCoordinator.persistentStores) {
NSError *error;
NSURL *storeURL = store.URL;
NSLog(@"storeURL: %@", storeURL);
NSPersistentStoreCoordinator *storeCoordinator = delegate.persistentStoreCoordinator;
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
NSLog(@"There are errors: %@", error);
}
delegate.persistentStoreCoordinator = nil;
context = nil;
delegate.managedObjectModel = nil;
}
这是删除我的所有数据,但当我尝试再次登录而不关闭应用程序时,它会给我以下错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.'
这是因为删除数据时我将persistentStoreCoordinator设置为nil,并且正在AppDelegate文件中创建其实例。有人可以建议我解决这个问题吗?
提前感谢您的帮助。
答案 0 :(得分:0)
您的代码有很多错误。
无论如何,要实现您的需求,请尝试使用此方法的修改版resetApplicationModel
...
- (void) resetApplicationModel {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// NSManagedObjectContext *context = delegate.managedObjectContext;
for (NSPersistentStore *store in delegate.persistentStoreCoordinator.persistentStores) {
NSError *error;
NSURL *storeURL = store.URL;
NSLog(@"storeURL: %@", storeURL);
// NSPersistentStoreCoordinator *storeCoordinator = delegate.persistentStoreCoordinator;
// [storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
NSLog(@"There are errors: %@", error);
}
// delegate.persistentStoreCoordinator = nil;
// context = nil;
// delegate.managedObjectModel = nil;
}
目的是删除已注释掉的代码行。