====我的解決方案==== 我解決了這個問題,將數據庫存儲在Main Bundle中並更改數據庫的名稱每次我更新應用程序。我的程序沒有更新只讀核心數據sqlite數據庫的新版本
我確實看過刷新模型等,但沒有真正得到那個工作,因爲我想。
====原來的問題====
我道歉,我已經問過這個問題,在不同的格式,但之前我一直沒能解決這個問題,可能是由於我問了這個問題。我沒有刪除舊的問題。
我有一個應用程序使用核心數據sqlite只讀數據庫,我試圖更新數據庫的新版本(版本#inbeded)的應用程序。無論我做什麼,除了手動清理目標和重置模擬器,它仍然讀取舊數據庫。我需要能夠通過代碼來做到這一點。
在初始設計期間,我發佈了以下內容以嘗試解決相同的問題:The initial design。現在數據庫非常大,在第一次啓動時需要花費大量的時間來構建它。
的過程:
A.),我的新的數據庫版本添加到Xcode項目,並手動將其通過的Xcode
B.)我檢查添加到包是什麼版本的數據庫使用
C.)如果有一個新的數據庫,我在文件目錄中刪除舊的數據庫,然後從主束複製新版本的文檔目錄
D.)我n附加的代碼我也刪除緩存目錄並重新創建它,我發現了一些指出緩存可能是原因的帖子。這僅用於測試,但仍包含在代碼中
儘管如此,應用程序仍然無法訪問更新後的sqlite數據庫。
我有一個新的數據庫版本加載到主包中,我有一個版本參數「EEEEEE」,當我讀取當前數據庫時它有「SSSSSS」參數,所以數據庫應該更新。
我一直在處理這個問題,並在網上搜索了很長時間,我不明白什麼是錯的。爲什麼它不訪問文檔目錄中的新數據庫。
我很感激所有幫助,我可以得到。我希望代碼看起來不奇怪,因爲我已經包含了我用來試圖瞭解發生的事情的NSLog。
跟蹤(的NSLog):
applicationDidFinishLaunchingWithOptions
readQuestion
persistentStoreCoordinator
==>PARAMETER is EEEEEE (000000)
==> Update needed
=======================
====BEFORE REPLACE====
===>CURRENT: SSSSSS: 999999
=======================
replaceDatabase function
=======================
=======================
DELETE CACHE
=======================
Remove successful
=======================
CREATE NEW CACHE
=======================
===============================
REMOVE sqlite FROM DOCUMENT DIR
===============================
removeItemAtPath:dbDocumentsPath: /Users/xxxxxx/Library/Application Support/iPhone Simulator/5.1/Applications/912962FC-933E-4305-971B-FF094BBD9051/Documents/database.sqlite
=====================================
CHECK IF sqlite EXIST IN DOCUMENT DIR
=====================================
===FILE DOES NOT EXIST===
========================================
MOVE sqlite FROM BUNDLE TO DOCUMENTS DIR
========================================
==>copyItemAtPath:dbBundlePath<<
====================================
CHECK IF sqlite EXIST IN DOCUMENT DIR
=====================================
===FILE EXIST===
readQuestion
=======================
=====AFTER REPLACE=====
=======================
Updated database is SSSSSS
========>STOP<=========
這裏是從委託的代碼。男, 「didFinishLaunchingWithOptions」:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"applicationDidFinishLaunchingWithOptions");
NSLog(@" ");
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
//==CHECK IF DATABASE NEEDS TO BE UPDATED==//
//====Check if database needs to be refreshed====//
AccessQuestionsDB *refreshDB_function = [AccessQuestionsDB new];
qArray = [refreshDB_function readQuestion: 999999];
NSLog(@" ");
//Tell what language DB
if ([k_DBVERSION isEqualToString:@"999999"]) {
NSLog(@"==>PARAMETER is SSSSSS (999999)");
} else {
NSLog(@"==>PARAMETER is EEEEEE (000000)");
}
NSLog(@" ");
//==== CHECK IF DATABASE NEEDS TO BE UPDATED ====//
if ([[qArray objectAtIndex:2] isEqualToString: k_DBVERSION]) {
// No update needed
NSLog(@"==> No update needed");
NSLog(@" ");
}
else {
NSLog(@"==> Update needed");
NSLog(@" ");
NSLog(@" ");
NSLog(@"=======================");
NSLog(@"====BEFORE REPLACE====");
if ([[qArray objectAtIndex:2] isEqualToString:@"000000"]) {
NSLog(@"===>CURRENT: EEEEEE: %@", [qArray objectAtIndex:2]);
} else {
NSLog(@"===>CURRENT: SSSSSS: %@", [qArray objectAtIndex:2]);
}
[self replaceDatabase];
}
qArray = [refreshDB_function readQuestion: 999999];
NSLog(@" ");
NSLog(@" ");
NSLog(@"=======================");
NSLog(@"=====AFTER REPLACE=====");
NSLog(@"=======================");
if ([[qArray objectAtIndex:2] isEqualToString:@"000000"]) {
NSLog(@"Updated database is EEEEEE");
} else {
NSLog(@"Updated database is SSSSSS");
}
NSLog(@" ");
NSLog(@"========>STOP<=========");
NSLog(@" ");
exit(0); //>>>>STOP HERE DURING TESTING
return YES;
}
replaceDatabase:
- (void)replaceDatabase {
NSLog(@"=======================");
NSLog(@"replaceDatabase function");
NSLog(@"=======================");
NSLog(@" ");
NSFileManager *fileManager = [NSFileManager defaultManager];
// remove old sqlite database from documents directory
NSURL *dbDocumentsURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
NSString *dbDocumentsPath = [dbDocumentsURL path];
//====Cache directory testing====//
//====This is just for testing====//
//===============================//
NSLog(@"=======================");
NSLog(@" DELETE CACHE");
NSLog(@"=======================");
NSLog(@" ");
if ([fileManager removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL] == YES)
NSLog (@"Remove successful");
else
NSLog (@"Remove failed");
NSLog(@"=======================");
NSLog(@" CREATE NEW CACHE");
NSLog(@"=======================");
NSLog(@" ");
[fileManager createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
//===============================//
NSLog(@"===============================");
NSLog(@"REMOVE sqlite FROM DOCUMENT DIR");
NSLog(@"===============================");
NSLog(@" ");
if ([fileManager fileExistsAtPath:dbDocumentsPath]) {
NSError *error = nil;
[fileManager removeItemAtPath:dbDocumentsPath error:&error];
NSLog(@"removeItemAtPath:dbDocumentsPath: %@",dbDocumentsPath);
NSLog(@" ");
if (error) {
NSLog(@"Error deleting sqlite database: %@", [error localizedDescription]);
}
} else {
NSLog(@"file does not ExistsAtPath:dbDocumentsPath: %@",dbDocumentsPath);
NSLog(@" ");
}
NSLog(@"=====================================");
NSLog(@"CHECK IF sqlite EXIST IN DOCUMENT DIR");
NSLog(@"=====================================");
NSLog(@" ");
if ([fileManager fileExistsAtPath:dbDocumentsPath]) {
NSLog(@"===FILE EXIST===");
} else {
NSLog(@"===FILE DOES NOT EXIST===");
}
NSLog(@" ");
// move new sqlite database from bundle to documents directory
NSLog(@"========================================");
NSLog(@"MOVE sqlite FROM BUNDLE TO DOCUMENTS DIR");
NSLog(@"========================================");
NSLog(@" ");
NSString *dbBundlePath = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sqlite"];
if (dbBundlePath) {
NSError *error = nil;
[fileManager copyItemAtPath:dbBundlePath toPath:dbDocumentsPath error:&error];
NSLog(@"==>copyItemAtPath:dbBundlePath<<");
if (error) {
NSLog(@"Error copying sqlite database: %@", [error localizedDescription]);
}
}
NSLog(@"=====================================");
NSLog(@"CHECK IF sqlite EXIST IN DOCUMENT DIR");
NSLog(@"=====================================");
NSLog(@" ");
if ([fileManager fileExistsAtPath:dbDocumentsPath]) {
NSLog(@"===FILE EXIST===");
} else {
NSLog(@"===FILE DOES NOT EXIST===");
}
NSLog(@" ");
}
persistentStoreCoordinator:
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSLog(@"persistentStoreCoordinator");
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
readQuestion:
- (NSMutableArray *)readQuestion: (int)questionNr {
NSLog(@"readQuestion");
NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];
NSError *error;
//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) { managedObjectContext = [(xxxxxAppDelegate *)
[[UIApplication sharedApplication] delegate] managedObjectContext]; }
// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"questions" inManagedObjectContext:qContext];
[fetchRequest setEntity:entity];
NSString *qNR = [NSString stringWithFormat:@"%d", questionNr];
if (questionNr == 999999) { // This is the call to check if database should be updated
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(question = %@)", @"Hur mycket är 37532 * 3?"];
[fetchRequest setPredicate: pred];
}
else {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(idQ = %@)", qNR];
[fetchRequest setPredicate: pred];
}
NSManagedObject *matches = nil;
NSArray *objects = [qContext executeFetchRequest:fetchRequest error:&error];
if ([objects count] == 0) { // If no match refresh database
[read_Question addObject:@"Update_DB"];
[read_Question addObject:@"Update_DB"];
[read_Question addObject:@"Update_DB"];
[read_Question addObject:@"Update_DB"];
} else {
matches = [objects objectAtIndex:0];
[read_Question addObject:[matches valueForKey:@"question"]];
[read_Question addObject:[matches valueForKey:@"qRightAnswer"]];
[read_Question addObject:[matches valueForKey:@"qWrongAnswer1"]];
[read_Question addObject:[matches valueForKey:@"qWrongAnswer2"]];
}
[fetchRequest release];
return [read_Question autorelease];
}
我猜你的persistentStoreCoordinator已經緩存了舊的值,所以當你再次要求它時,它甚至不需要去商店獲取值並簡單地返回舊值。也許設置persistentStoreCoordinator爲零(也許你的managedObjectContext)將強制重新加載並解決你的問題。 – lnafziger 2012-07-16 04:22:17