如何删除所有对象?我知道我可以通过
删除一个[managedObjectContext deleteObject:objToDelete];
是否可以在不迭代所有数组的情况下删除所有数据? 谢谢
答案 0 :(得分:11)
此函数从磁盘中删除当前的SQLite db文件并创建一个新文件。它比任何迭代删除都要快得多。
-(void)deleteAndRecreateStore{
NSPersistentStore * store = [[self.persistentStoreCoordinator persistentStores] lastObject];
NSError * error;
[self.persistentStoreCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtURL:[store URL] error:&error];
__managedObjectContext = nil;
__persistentStoreCoordinator = nil;
[self managedObjectContext];//Rebuild The CoreData Stack
}
如果您想在Application Delegate外部调用此方法(假设是模板化CoreData集成),您可以使用this来获取对您的app delegate的引用:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
不要忘记导入标题。
答案 1 :(得分:8)
将对象标记为删除然后保存按照它的方式工作,因为Core Data仍然需要为所有被删除的对象运行验证规则。毕竟,对象可以根据它对-validateForDelete:
的响应方式拒绝删除。
如果:
然后:
答案 2 :(得分:8)
这就是我“重置”我的数据存储所做的事情:
- (BOOL)resetDatastore
{
[[self managedObjectContext] lock];
[[self managedObjectContext] reset];
NSPersistentStore *store = [[[self persistentStoreCoordinator] persistentStores] lastObject];
BOOL resetOk = NO;
if (store)
{
NSURL *storeUrl = store.URL;
NSError *error;
if ([[self persistentStoreCoordinator] removePersistentStore:store error:&error])
{
[[self persistentStoreCoordinator] release];
__persistentStoreCoordinator = nil;
[[self managedObjectContext] release];
__managedObjectContext = nil;
if (![[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:&error])
{
NSLog(@"\nresetDatastore. Error removing file of persistent store: %@",
[error localizedDescription]);
resetOk = NO;
}
else
{
//now recreate persistent store
[self persistentStoreCoordinator];
[[self managedObjectContext] unlock];
resetOk = YES;
}
}
else
{
NSLog(@"\nresetDatastore. Error removing persistent store: %@",
[error localizedDescription]);
resetOk = NO;
}
return resetOk;
}
else
{
NSLog(@"\nresetDatastore. Could not find the persistent store");
return resetOk;
}
}
答案 3 :(得分:6)
您也可以拆掉堆栈(释放NSManagedObjectContext
,NSPersistentStore
和NSManagedObjectModel
)并删除该文件。可能比遍历整个数据库并单独删除每个对象更快。
此外,它们不太可能在将来提供此功能,因为它很容易删除该文件。但是,如果您认为重要,那么请提交雷达并告知Apple。否则他们不会知道有多少人想要这个功能。
答案 4 :(得分:2)
只需迭代数组并删除它们。没有定义的方法可以全部删除它们。
答案 5 :(得分:1)
删除所有缓存和文档时,您将删除该数据库。不是对managedObjectContext的必要调用
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSMutableArray *paths = [NSMutableArray array];
[paths addObjectsFromArray:documents];
[paths addObjectsFromArray:caches];
for (NSUInteger i = 0; i < [paths count]; i++) {
NSString *folderPath = [paths objectAtIndex:i];
NSLog(@"Attempting to remove contents for: %@", folderPath);
//Remove all cached data in the local app directory
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
for (NSString *strName in dirContents) {
[[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:strName] error:&error];
if (error != nil) {
NSLog(@"Error removing item: %@ : %@", strName, error.description);
} else {
NSLog(@"Removed item: %@", strName);
}
}
}
答案 6 :(得分:0)
我使用了stifin的代码并将其更新为使用-performBlockAndWait:
- (BOOL)reset
{
__block BOOL result = YES;
[[self mainContext] performBlockAndWait:^{
[[self mainContext] reset];
NSArray* stores = [[self persistentStoreCoordinator] persistentStores];
_mainContext = nil;
_persistedContext = nil;
for(NSPersistentStore* store in stores) {
NSError* error;
if(![[self persistentStoreCoordinator] removePersistentStore:store error:&error]) {
debuglog(@"Error removing persistent store: %@", [error localizedDescription]);
result = NO;
}
else {
if(![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
debuglog(@"Error removing file of persistent store: %@", [error localizedDescription]);
result = NO;
}
}
}
_persistentStoreCoordinator = nil;
}];
return result;
}