gpt4 book ai didi

iphone - 重置 CoreData 持久存储

转载 作者:行者123 更新时间:2023-12-03 18:22:40 25 4
gpt4 key购买 nike

基本上,我想做的是清除 CoreData 持久存储中的所有数据,然后导入新数据。你会怎么做?看来最简单的解决方案是调用 [NSPersistentStoreCoordinator removePersistentStore:error:] 然后删除文件。这是可用的最佳实践吗?它是线程安全的吗?

非常感谢,

#

问题0.1:是

我正在尝试更新 CoreData 持久存储中的数据。我的用户正在看到带有统计数据的表格 View 。我想通过删除所有现有数据然后导入新数据来更新应用程序。我想显示一个进度 View 来告诉用户应用程序没有挂起。

我在我的AppDelegate中添加了以下resetPersistentStore方法(给出persistentStoreCoordinator供引用):

// ...
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
// ...

/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kPersistentStoreFilename]];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return persistentStoreCoordinator;
}

/**
* Will remove the persistent store
*/
- (NSPersistentStoreCoordinator *)resetPersistentStore {
NSError *error;

[managedObjectContext lock];

// FIXME: dirty. If there are many stores...
NSPersistentStore *store = [[persistentStoreCoordinator persistentStores] objectAtIndex:0];

if (![persistentStoreCoordinator removePersistentStore:store error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

// Delete file
if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

// Delete the reference to non-existing store
[persistentStoreCoordinator release];
persistentStoreCoordinator = nil;

NSPersistentStoreCoordinator *r = [self persistentStoreCoordinator];
[managedObjectContext unlock];

return r;
}

然后在我看来我这样做了(在另一个线程中,因为我正在使用 MBProgressHUD:

PatrimoineAppDelegate *appDelegate = (PatrimoineAppDelegate *)[[UIApplication sharedApplication] delegate]; 
// Delete everything
[appDelegate resetPersistentStore];

我得到一个 EXC_BAD_ACESS...

我不太了解 CoreData 或多线程,也许我犯了一个明显的错误......

最佳答案

对于那些在 iOS9+ 上尝试执行此操作的用户,现在提供了 destroyPersistentStoreAtURLreplacePersistentStoreAtURL API。

关于iphone - 重置 CoreData 持久存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2280158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com