iOS-将sqlite复制到CoreData的捆绑包 [英] iOS - Copy sqlite to bundle for CoreData

查看:49
本文介绍了iOS-将sqlite复制到CoreData的捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所做的:使用应用的填充版本创建了sqlite文件.将该文件复制到我的应用程序包中,确保设置了目标并且该文件存在于复制包资源"中

Here's what I've done: Created the sqlite file with a populated version of the app. Copied that file into my app bundle, making sure that the target is set and that it is present in "Copy Bundle Resources"

然后我尝试像这样填充核心数据:

I then try to populate Core Data like this:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{  

if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestModel.sqlite"];


if( ![[NSFileManager defaultManager]
      fileExistsAtPath:[storeURL path]] ) {
    // If there’s no Data Store present (which is the case when the app first launches), identify the sqlite file we added in the Bundle Resources, copy it into the Documents directory, and make it the Data Store.
    NSString *sqlitePath = [[NSBundle mainBundle]
                            pathForResource:@"TestModel" ofType:@"sqlite"
                            inDirectory:nil];
    NSError *anyError = nil;
    BOOL success = [[NSFileManager defaultManager]
                    copyItemAtPath:sqlitePath toPath:[storeURL path] error:&anyError];

    if(success){
        NSLog(@"sucess, loading into store");
        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();
        }else{
            NSLog(@"error with sqlite file");
        }
    }
}

return _persistentStoreCoordinator;

}

但是我得到这个错误:

NSFileManager copyItemAtPath:toPath:error:]: source path is nil

我做错了什么?是不是说它在捆绑包中找不到sqlite文件?

What am I doing wrong? Is it saying that it can't find the sqlite file in the bundle?

推荐答案

我认为发生这种情况的原因是,当我从Finder将文件导入Xcode时,文件名为"TestModel",而不是"TestModel.sqlite".

I think this happened because when I imported the file from Finder into Xcode, it was named "TestModel" instead of "TestModel.sqlite".

这篇关于iOS-将sqlite复制到CoreData的捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆