Pragmatic ball boy

iOSを中心にやってる万年球拾いの老害エンジニアメモ

CoreDataのデータを全て削除


(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;
}
}