1
我一直在開發一個需要在運行時替換SQLite數據庫文件的應用程序,我有代碼來替換數據庫但不更新數據庫仍然顯示舊數據。請指導我。在運行時替換SQLite數據庫
這是獲取該文件並替換
ASIHTTPRequest *requestToDownloadDB = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.turfnutritiontool.com/dev.ios/services/turfnutritiontool_ver_two.db"]];
// ======================================================================================================
// All methods below remove old store from coordinator
// ======================================================================================================
NSError *error = nil;
NSPersistentStore *persistentStore = [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0];
[self.persistentStoreCoordinator removePersistentStore:persistentStore error:&error];
if (error) {
NSLog(@"error removing persistent store %@ %@", error, [error userInfo]);
}
// then update
//NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"turfnutritiontool_ver_two.db"];
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent: @"turfnutritiontool_ver_two.db"];
NSLog(@"This is store URL %@ ", storeURL);
[requestToDownloadDB setDownloadDestinationPath:[NSString stringWithString:[storeURL absoluteString]]];
[requestToDownloadDB startSynchronous];
// add clean store file back by adding a new persistent store with the same store URL
if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]) {
NSLog(@"failed to add db file, error %@, %@", error, [error userInfo]);
}
Pelase看看我的代碼加入你的代碼之後。
我收到
NSLog(failed to add db file, error (null), (null));
我應該現在做的
我可以直接使用NSPersistentStore類,因爲它給我一個錯誤,抱歉,但我是新的核心數據。我需要創建NSPersistentStoreCoordinator的對象嗎?請幫助我 – Retro 2012-03-20 08:13:14
我的應用程序現在不使用核心數據,只有SQlite使用db,所以我需要使用或添加核心數據功能來實現這個 – Retro 2012-03-20 08:16:04
我添加CoreData lib並創建對象?它會工作 – Retro 2012-03-20 08:29:51