2014-04-14 84 views
0

我想從sqlite文件預填充coredata數據庫,但我不明白爲什麼數據庫未填充。我的sqlite文件正確地填充了我需要的所有數據並添加到捆綁包資源中。Coredata數據庫預填充未填充

這裏persistentStoreCoordinator方法的int的AppDelegate:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (persistentStoreCoordinator != nil) 
    { 
     return persistentStoreCoordinator; 
    } 
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingString:@"MyApp.db"]; 
    NSError  *error = nil; 

    NSFileManager *fm = [NSFileManager defaultManager]; 

    if (![fm fileExistsAtPath:storePath]) 
    { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"db"]; 

     if (defaultStorePath) 
     { 
      [fm copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

    NSURL   *storeUrl = [NSURL fileURLWithPath:storePath]; 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

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

    return persistentStoreCoordinator; 
} 

在視圖控制器,我嘗試獲取的條目,但它似乎enter code here空:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
initWithEntityName:@"Type_textimage_4text"]; 
    NSFetchRequest   *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription  *description = [NSEntityDescription entityForName:@"Type_textimage_4text" inManagedObjectContext:managedObjectContext]; 

    [fetchRequest setEntity:description]; 


    NSMutableArray *questions = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    NSLog(@"question : %@", questions); 

我可能錯過了一些東西,但我不找不到什麼。任何人都可以幫助我嗎?

+0

我有2個應用程序與預填充sqlite數據庫交付。我在外部數據庫管理器上創建了它們。如果你想看看DB上發生了什麼,你可以通過模擬器存儲所有文件的文件夾並從那裏獲取數據庫。檢查它與外部應用程序,你會看到裏面有什麼,獨立於你的代碼。然後你會知道你的問題在哪裏。 – Marcal

+0

感謝您的回答。我用sqlite manager打開了數據庫,並且看到了我放入的所有條目的表格,但是我看到了其他我認爲由CoreData創建的表格以大寫形式寫入。例如,我有一個由我創建的表type_textimage_4text,其中所有的值和由CoreData創建的ZTYPE_TEXTIMAGE_4TEXT表爲空。 – Nonouf

+0

如何將我現有的所有數據庫導入到核心數據中? – Nonouf

回答

0

回答上面的問題,我添加了一個包含預填充的sqlite數據庫的App的AppDelegate.m文件。爲此,我在xcode上創建項目時選擇了「使用核心數據」。雖然它適用於我,但我相信現在使用NSDocument來實現這種應用更好。其中一個優點是該項目已經與iCloud兼容。我還沒有這樣做,所以我只是從我讀過/看過的內容開始講。

// 
// AppDelegate.m 
// 
// 
// 

#import "AppDelegate.h" 
#import "MainView.h" 


@implementation AppDelegate 

@synthesize window = _window; 

@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UINavigationController *navViewController = (UINavigationController *)self.window.rootViewController; 


     MainView *main=[[navViewController viewControllers]objectAtIndex:0]; 
     main.managedObjectContext=self.managedObjectContext; 


    return YES; 

} 

- (void)applicationWillResignActive:(UIApplication *)application{ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 

} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 

} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 

} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 

} 



- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 

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

#pragma mark - Core Data stack 








- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (__managedObjectContext != nil) { 
     return __managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     __managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [__managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return __managedObjectContext; 
} 






// Returns the managed object model for the application. 
// If the model doesn't already exist, it is created from the application's model. 
- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (__managedObjectModel != nil) { 
     return __managedObjectModel; 
    } 

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Solvents" withExtension:@"momd"]; 



    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    return __managedObjectModel; 
} 



- (NSString *)applicationDocumentsDirectoryString { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    return basePath; 
} 






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

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

    NSString *storePath = [[self applicationDocumentsDirectoryString] stringByAppendingPathComponent: @"Solvents.sqlite"]; 


    //NSLog(@"store path %@", storePath); 
    //NSLog(@"store URL%@", storeURL); 



    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:storePath ]) { 



     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Solvents" 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; 
} 






#pragma mark - Application's Documents directory 

// Returns the URL to the application's Documents directory. 
- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 




@end