2011-10-29 32 views
0

我有我複製到我的項目預填充孔sqlite的文件(文件夾中,並進入Xcode項目窗口)sqlite文件不會填充表視圖 - 儘管它使用簡單的數據 - ? (sqlite的,核心數據,預填充數據)

當我檢查sqlite的文件與終端,它工作正常,我的sqlite文件中有正確的數據。

但是,然後我嘗試填充我的tableView與來自sqlite文件的數據,但tableView仍然是空的。

你能告訴我我應該看哪裏?

我先用一些數據,試圖和它的作品(什麼是applicationDidFinish ...評論),但我的sqlite的文件,這是行不通的:

這裏是我的代碼:(和這裏的教程網址: http://www.raywenderlich.com/980/core-data-tutorial-how-to-preloadimport-existing-data

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    NSManagedObjectContext *context = [self managedObjectContext]; 

    /* 
    THIS WORKS : 
    FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" 
                    inManagedObjectContext:context]; 
    failedBankInfo.name = @"the name"; 
    failedBankInfo.city = @"the city"; 
    failedBankInfo.state = @"the state"; 

    FailedBankDetails *failedBankDetails = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankDetails" 
                     inManagedObjectContext:context]; 

    failedBankDetails.closeDate = [NSDate date]; 
    failedBankDetails.updatedDate = [NSDate date]; 
    failedBankDetails.zip = [NSNumber numberWithInt:12345]; 

    failedBankInfo.details = failedBankDetails; 
    failedBankDetails.info = failedBankInfo; 

    NSError *error; 
    if (![context save:&error]){ 
     NSLog(@"%@,", [error localizedDescription]); 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" 
               inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    for (FailedBankInfo *info in fetchedObjects){ 
     NSLog(@"name : %@", info.name); 
     FailedBankDetails *details = info.details; 
     NSLog(@"zip : %@", details.zip); 
    } 
    [fetchRequest release];*/ 

    FailedBanksListViewController *root = (FailedBanksListViewController *)[_navController topViewController]; 
    root.context = [self managedObjectContext]; 
    [window addSubview:_navController.view]; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

/** Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"FailedBanksCD.sqlite"]; 
    /*NSString *storePath = [[self applicationDocumentsDirectory] 
          stringByAppendingPathComponent: @"FailedBanksCD.sqlite"]; 
    */ 
    //NSURL *storeURL = [NSURL fileURLWithPath:storePath]; 

    // Put down default db if it doesn't already exist 
    NSString *storePath = [NSString stringWithFormat:@"%@", storeURL]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] 
             pathForResource:@"FailedBanksCD" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 
    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_; 
} 

非常感謝

回答

2

對我來說,它看起來好像數據庫未複製到文檔-DIR。如果塊可以移動這部分

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

超出了:

if (![fileManager fileExistsAtPath:storePath]) { 
    ... 
} 

您必須再次嘗試之前從DOC-DIR刪除當前空DB(在模擬器中刪除,從設備上刪除應用程序,重新安裝)。

+0

感謝ott--,這並不是刷新表視圖不幸。如果我用'select * from ZFAILEDBANKINFO limit 3'查看doc-dir中包含的複製sqlite,它不會在終端中顯示任何內容,也許這是因爲它已被加密。無論如何,你能看看我的代碼嗎?我已將包含sqlite和xcode項目的zip文件放在:www.pondb.com(我的個人網站)上。 – Paul

+0

您是否在使用錯誤的數據庫?您嘗試提取的數據位於banklist.sqlite3中,而您複製了DB FailedBanksCD.sqlite。 –

+0

感謝ott--,我想我混合了它時,我複製了失敗的銀行sqlite文件,它一定是因爲我一遍又一遍地嘗試,並最終它的作品。感謝您的幫助,乾杯 – Paul

相關問題