2012-02-18 101 views
0

我在一個應用程序中創建了一個核心數據庫,它涉及從API中抽取信息並填充數據庫。預加載核心數據數據庫不工作

我現在想在另一個應用程序中使用它。

我已經複製了.xcdatamodeld文件和NSManagedObject類。

我已經添加並導入了Core Data框架。

我已將.sqlite文件複製到我的新應用程序資源中作爲默認數據庫。

我正在使用下面的代碼,它應該將默認數據庫複製到Documents目錄並打開它,以便我可以對其執行查詢。

它導致應用程序崩潰,沒有錯誤信息,對我要去哪裏錯誤的任何想法?

如果我在這裏使用saveToURL創建數據庫,我知道文件名將是persistentStore而不是Trailer.sqlite,如下所示,是否相關?

感謝

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 


// Get URL -> "<Documents Directory>/<TrailerDB>" 
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 

url = [url URLByAppendingPathComponent:@"TrailerDB"]; 

UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:url]; 

// Copy out default db to documents directory if it doesn't already exist 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath:[url path]]) { 
    NSString *defaultDB = [[NSBundle mainBundle] 
            pathForResource:@"trailerdatabase" ofType:@"sqlite"]; 
    if (defaultDB) { 

     [fileManager copyItemAtPath:defaultDB toPath:[url path] error:NULL]; 

    } 
} 

if (doc.documentState == UIDocumentStateClosed) { 

    // exists on disk, but we need to open it 
    [doc openWithCompletionHandler:^(BOOL success) 
    { 

     if (success) [self useDatabase:doc]; 

     if (!success) NSLog(@"couldn’t open document at %@", url); 


    }]; 

} else if (doc.documentState == UIDocumentStateNormal) 
{ 
    [self useDatabase:doc]; 
} 

} 
+0

步通過與調試每一行,看看哪一個導致崩潰 – jackslash 2012-02-19 00:36:29

+0

openWithCompletionHandler被調用,但崩潰... – Alan 2012-02-19 01:12:12

回答

1

Ive有另一個樣子,我不知道你在做什麼,但下面這段代碼是我做的,將回答你的問題。我檢查工作數據庫是否存在,以及它是否不從應用程序包移動到位,然後繼續加載它。我留下了蘋果模板中的股票評論,因爲我認爲他們最終可能會有所幫助。

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"workingDataBase.sqlite"]; 

    NSError *error = nil; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:[[self applicationDocumentsDirectoryString] stringByAppendingPathComponent: @"workingDataBase.sqlite"]]){ 
     //database not detected 
     NSLog(@"database not detected"); 
     NSURL * defaultDatabase = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"DefaultData" ofType:@"sqlite"]]; 
     NSError * error; 
     if (![[NSFileManager defaultManager] copyItemAtURL:defaultDatabase toURL:storeURL error:&error]){ 
      // Handle Error somehow! 
      NSLog(@"copy file error, %@", [error description]); 
     } 
    } 

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible; 
     * The schema for the persistent store is incompatible with current managed object model. 
     Check the error message to determine what the actual problem was. 


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     * Simply deleting the existing store: 
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 
+0

由於之前甚至不使其恢復到其完成塊,但我一直使用UIManagedDocument類,新到ios5,到目前爲止,並沒有需要任何弄亂應用程序委託,我認爲這是解決方案,即與[self managedObjectModel]需要。現在甚至沒有選擇勾選「使用核心數據」,因爲似乎不需要在應用程序委託中包含額外的代碼。 – Alan 2012-02-20 00:48:36

+0

夠公平的。這個代碼可以在你創建MOC的任何地方進行。他們將它保存在應用程序代理中,因爲這是從中創建多個MOC的更容易的地方。我還沒有看過UIManagedDocument,但仍然有一個選項可以打勾在Xcode中使用核心數據 – jackslash 2012-02-20 08:51:21

+0

是的,我沒有得到託管文檔的任何地方,你的代碼看起來工作正常,所以我們會用它所以!感謝那。沒有意識到你可以將它保留在應用程序代理之外,是的,你是對的,你仍然可以打勾核心數據,我看錯了地方。歡呼的幫助。僅供將來參考,如果我想將此代碼放入應用程序委託中,如何從中檢索托管對象上下文/模型?我見過應用程序委託啓動根控制器的例子,所以可以在那個時候設置它,但是我的視圖控制器正在出來一個故事板... – Alan 2012-02-21 11:49:53