2011-08-01 43 views
1

我有一個核心數據應用程序,我正在使用一個SQL Lite數據庫。我已經在我的模擬器中填充了它,現在我想將它與應用程序一起發送,但是只要我將它安裝在ipad上,db就是空白!填充SQL Lite數據庫空白時轉移到ipad

我已經添加了填充分貝項目(從模擬器文件夾),並在委託我有以下幾點:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[window addSubview:splitViewController.view]; 
[window makeKeyAndVisible]; 

NSString *Path = [[NSBundle mainBundle] bundlePath]; 
NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"]; 

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath]; 
self.data = tempDict; 
[tempDict release]; 

// Override point for customization after application launch. 
// Add the split view controller's view to the window and display. 
[self createEditableCopyOfDatabaseIfNeeded]; 


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"NO" forKey:@"enableRotation"]; 
[appDefaults setObject:@"0" forKey:@"orientation"]; 
[appDefaults setObject:@"100" forKey:@"repID"]; 
[appDefaults setObject:@"Atlanta Dental" forKey:@"RepName"]; 
[defaults registerDefaults:appDefaults]; 

//self.window.rootViewController = self.splitViewController; 
return YES; 
} 

-(void)createEditableCopyOfDatabaseIfNeeded 
{ 
// Testing for existence 
BOOL success; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME]; 

success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success) 
    return; 

// The writable database does not exist, so copy the default to 
// the appropriate location. 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] 
          stringByAppendingPathComponent:DATABASE_NAME]; 
success = [fileManager copyItemAtPath:defaultDBPath 
           toPath:writableDBPath 
           error:&error]; 
if(!success) 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    //if (! [fileManager fileExistsAtPath:storePath]) 
    //{ 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"D4" ofType:@"sqlite"]; 
     if (defaultStorePath) 
     { 
      [fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL]; 
     } 
    //} 
    //NSAssert1(0,@"Failed to create writable database file with Message : '%@'.", 
    //  [error localizedDescription]); 
} 
} 


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
if (persistentStoreCoordinator != nil) 
{ 
    return persistentStoreCoordinator; 
} 

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:DATABASE_NAME]; 

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

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
{ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
}  

return persistentStoreCoordinator; 
} 

任何想法?

編輯補充:

當我用iphone的探險家探索了iPad,我可以看到原來的人口DB的應用程序路徑,因此它得到複製。這意味着我必須有上述錯誤。 。 。

對不對?

回答

1

由於商店始終是空白,這意味着持久性存儲協調員沒有找到在URL現有的商店,你提供那麼它會創建一個新的空店面。缺少商店的最可能的解釋是它不會被複制到您認爲正在被複制的地方。

您從不使用相同的方法兩次獲取應用程序包中默認文件的路徑或文檔中讀寫文件的路徑。每次創建路徑時,都使用不同的代碼。這使得您可能會在代碼中的不同位置生成不同的路徑。選擇一種方法來生成每個路徑並根據需要重新使用。

我建議記錄所有的路徑和URL以確認它們都指向你認爲他們做的地方。我還建議爲文件管理操作捕獲錯誤。即使布爾返回值爲YES,您也可能會遇到錯誤。

+0

它在試圖複製填充的內容之前正在訪問商店。好決定。 –

0

這是一個解決方案。將填充的數據庫從模擬器複製到您的項目中。然後從設備中刪除應用程序,然後執行全新安裝,然後應用程序將複製填充的數據庫,如果空白數據庫已存在,則不會再複製數據庫。看到你的代碼

NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME]; 

success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success)//database already copied 
    return;