Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i remove coredata from iphone

You know how you can Reset the coredata store on an iPhone simulator when you've changed your entity structure?

Do I need to perform a similar process when I've created a new version of my core data store that is different from what I last ran on my iPhone? If so, how, please?

Thanks

like image 672
Jazzmine Avatar asked Dec 05 '22 20:12

Jazzmine


2 Answers

Just for convenience, until you code a way to remove the persistent store through your app, you can just delete the app off the phone. (Hold your finger on the home screen until icons get wiggly, then click the x on your app.) Then with your phone connected to your Mac, choose Product > Run in XCode and it will reinstall your app on the phone, but with empty data directories.

For deployment, of course, you need to come up with a way to do it without deleting the app, if you will ever change your data model after deployment (assume you will). Data migration is the best option, but if all else fails delete the persistent store file. It would be preferable to prompt for the user's approval before doing that. If they have important data they can decline and maybe get the old version of your app back to view the data and migrate it by hand, or they can wait until you release version 2.0.1 that fixes your data migration bug.

like image 109
morningstar Avatar answered Dec 21 '22 00:12

morningstar


Here is the routine I use to reset my App content. It erases the store and any other file stored.

- (void) resetContent
{
    NSFileManager *localFileManager = [[NSFileManager alloc] init];
    NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];

    NSArray *content = [localFileManager contentsOfDirectoryAtURL:rootURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];

    for (NSURL *itemURL in content) {
        [localFileManager removeItemAtURL:itemURL error:NULL];      
    }

    [localFileManager release];
}

If you only want to erase the store, since you know its file name, you can refrain from enumerating the document directory content:

- (void) resetContent
{
    NSFileManager *localFileManager = [[NSFileManager alloc] init];
    NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];

    NSURL *storeURL = [rootURL URLByAppendingPathComponent:@"myStore.sqlite"];
    [localFileManager removeItemAtURL:storeURL error:NULL];     

    [localFileManager release];
}

But please note that in many cases, its better to migrate your store when you change your model, rather than to delete it.

like image 44
Jean-Denis Muys Avatar answered Dec 21 '22 01:12

Jean-Denis Muys