removeitematpath для удаления Olddb.sqlite - PullRequest
0 голосов
/ 30 августа 2010

Я получил это приложение, которое необходимо обновить новой базой данных.

Я хочу удалить старую и заменить ее новой.

Это кодиспользуя:

 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

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


     NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Newdb.sqlite"];

     NSFileManager *fileManager = [NSFileManager defaultManager];
     // If the expected store doesn't exist, copy the default store.
     if (![fileManager fileExistsAtPath:storePath]) {
          NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Newdb" ofType:@"sqlite"];
          if (defaultStorePath) {
               [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
          }
     }

     NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber   numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

     NSError *error;
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {

          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          exit(-1);  // Fail
     }    

     return persistentStoreCoordinator;
}

Я хочу, чтобы мое приложение проверяло при запуске, есть ли Olddb.sqlite, и если да, удалите его!

1 Ответ

1 голос
/ 31 августа 2010

вот решение ...

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [documentPaths objectAtIndex:0];
    NSString *pathInDocuments = [docsDir stringByAppendingPathComponent:@"Olddb.sqlite"];
if (pathInDocuments) {
    [fileManager removeItemAtPath:pathInDocuments error:NULL];
}
...