Ios Coredata数据库预填充未填充

Ios Coredata数据库预填充未填充,ios,objective-c,sqlite,core-data,Ios,Objective C,Sqlite,Core Data,我试图从sqlite文件中预填充coredata数据库,但我不明白为什么数据库没有填充。我的sqlite文件正确地填充了我需要的所有数据,并添加到捆绑资源中 这里是AppDelegate中的persistentStoreCoordinator方法: - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator != nil) { retur

我试图从sqlite文件中预填充coredata数据库,但我不明白为什么数据库没有填充。我的sqlite文件正确地填充了我需要的所有数据,并添加到捆绑资源中

这里是AppDelegate中的persistentStoreCoordinator方法:

- (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;
}
在视图控制器中,我尝试获取条目,但似乎
在此处输入code
为空:

    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);

我可能错过了什么,但我找不到什么。有人能帮我吗

为了回答上述问题,我添加了一个应用程序的AppDelegate.m文件,其中包括一个预填充的sqlite数据库。为此,我在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

我有两个应用程序,它们都带有预填充的sqlite数据库。我在外部数据库管理器上创建了它们。如果您想查看数据库上发生了什么,您可以一直转到模拟器存储所有文件的文件夹,然后从那里获取数据库。使用和外部应用程序进行检查,您将看到其中的内容,独立于您的代码。然后你就知道你的问题在哪里了。谢谢你的回答。我用sqlite管理器打开了数据库,我看到了我的表以及我输入的所有条目,但我看到了我认为由CoreData以大写形式编写的其他表。例如,我有一个由我创建的带有所有值的表类型_textimage_4text,以及一个由CoreData创建的ZTYPE_textimage_4text空表。如何在core data中导入所有现有数据库?