我想确认我删除核心数据存储(sqlite)的方法是正确的。它似乎可以正常工作而不会崩溃,但想确认这是正确的方法。之后,当用户连接到数据库时,会自动生成一个新的 sqlite 文件。
这是我删除数据存储的代码:
- (BOOL)dropDataStore{
// ----------------------
// This method removes all traces of the Core Data store
// ----------------------
NSError *_error = nil;
NSURL *_storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *_store = [persistentStoreCoordinator persistentStoreForURL:_storeURL];
// Remove the SQL store and the file associated with it
if ([persistentStoreCoordinator removePersistentStore:_store error:&_error]) {
[[NSFileManager defaultManager] removeItemAtPath:_storeURL.path error:&_error];
}
if (_error) {
return NO;
}
persistentStoreCoordinator = nil;
managedObjectContext = nil;
managedObjectModel = nil;
return YES;
}