How to force coredata to rebuild sqlite database model?

后端 未结 3 1525
野性不改
野性不改 2020-12-23 10:37

In my app I sometimes need to rebuild and repopulate database file. SQLite databse is created and managed by CoreData stack.

What I\'m trying to do is drop the file

3条回答
  •  北海茫月
    2020-12-23 11:14

    You can keep a "clean" copy of your sqlite database as part of the application bundle, then just copy over the version in the documents directory whenever you'd like to refresh the database.

    Here's some code from an App that does something similar (although this version will not copy over and existing db):

    // Check for the existence of the seed database
    // Get the path to the documents directory and append the databaseName
    NSString* databasePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kDatabaseName];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ( ![fileManager fileExistsAtPath: databasePath] ) 
    {
        NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] 
                                         stringByAppendingPathComponent: kDatabaseName];
    
        [fileManager copyItemAtPath: databasePathFromApp 
                             toPath: databasePath 
                              error: nil];
    }
    [fileManager release];
    

提交回复
热议问题