2014-04-17 62 views
0

一個SQLite文件在此之後教程http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated,我創建使用核心數據種子我隨後移動一個數據庫中的空的命令行應用程序(通過取quizgeodata.sqlite文件)切換到一個IOS應用我正在建設中。唯一的其他步驟,使其工作教程名單是這個代碼的應用程序代理添加到persistantStoreCoordinator複製從一個項目到另一個

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) { 
     NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"quizgeodata" ofType:@"sqlite"]]; 
     NSError* err = nil; 

     if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) { 
      NSLog(@"Oops, could copy preloaded data"); 
     } 
    } 

(上一頁措施包括服用類文件(對應於實體)和... xcdatamodel .d文件放入空白的命令行應用程序中,以確保模式相同)。當我運行在空的命令行應用程序的代碼,它表明我在日誌報表進口所取得的數據,所以我通過複製quizgeodata.sqlite回iOS應用程序並添加假設持久存儲協調的一個代碼位,然後將以下代碼(在viewDidLoad中)應取從核心數據的數據,但日誌報表顯示,被檢索沒有數據的。下面的代碼中的錯誤的日誌說「空」(意思是沒有錯誤我承擔),當我問的Xcode打印的SQL語句就說明這個控制檯

2014-04-17 16:11:57.477 qbgeo[767:a0b] CoreData: annotation: total fetch execution time: 0.0026s for 0 rows. 

所以很明顯有一個在沒有數據我複製過的sqlite文件。你能解釋我可能需要做些什麼才能使它工作嗎?

請注意,我跑項目>清潔幾次,這樣是不是問題

從viewDidLoad中 ID委託= [[UIApplication的sharedApplication]代表]; self.managedObjectContext = [委託managedObjectContext];

NSError *error; 
if (![self.managedObjectContext save:&error]) { 
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
} 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription 
           entityForName:@"Sportsquiz" inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 
NSLog(@"error %@", [error description]); 
NSArray *fuckedUpObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
for (Sportsquiz *info in fuckedUpObjects) { 
    NSLog(@"correctAnswer %@", [info valueForKey:@"question"]); 
    NSLog(@"correctAnswer %@", [info valueForKey:@"correctAnswer"]); 

} 
+0

從一個項目複製一個SQLite文件到另一個複製它。然後您需要使用Xcode的接口將其添加到目標項目中。然後你需要告訴Xcode它將被包含在構建中。然後,您需要添加啓動邏輯,以便在首次啓動時從該軟件包複製文件,以便在設備上讀取/寫入存儲。 –

+0

你見過這樣的:http://stackoverflow.com/questions/23128921/ios-7-pre-filled-core-data-database-does-not-contain-all-data/23129277#23129277 – Volker

回答

1

讓我們嘗試:

原始項目:

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

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

NSError *error = nil; 
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary]; 

/*ATTETION: disable WAL mode*/ 
[pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"]; 
NSNumber *optionYes = [NSNumber numberWithBool:YES]; 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         pragmaOptions, NSSQLitePragmasOption, 
         optionYes,NSMigratePersistentStoresAutomaticallyOption ,nil]; 
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { 
abort(); 
} 
return _persistentStoreCoordinator; 
} 

複製源碼文件到新的項目和獲取。我認爲它會起作用。

相關問題