ios - 将 JSON 导入核心数据

标签 ios sqlite core-data

我正在尝试按照本教程进行操作: http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated 在本教程中,展示了如何构建用于创建 sqlite 和从 json 导入数据的脚本。 我写了这个:

static NSManagedObjectModel *managedObjectModel()
{
    static NSManagedObjectModel *model = nil;

    if (model != nil) {
        return model;
    }
    NSString *path = @"AppChecker";
    path = [path stringByDeletingPathExtension];
    NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"mom"]];
    model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];        
    return model;
}

static NSManagedObjectContext *managedObjectContext()
{
    static NSManagedObjectContext *context = nil;

    if (context != nil) {
        return context;
    }
    @autoreleasepool {
        context = [[NSManagedObjectContext alloc] init];

        NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];
        [context setPersistentStoreCoordinator:coordinator];

        NSString *STORE_TYPE = NSSQLiteStoreType;

        NSString *path = [[NSProcessInfo processInfo] arguments][0];
        path = [path stringByDeletingPathExtension];
        NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];
        NSError *error;
        NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];
        if (newStore == nil) {
            NSLog(@"Store Configuration Failure %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
        }
    }
    return context;
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // Create the managed object context
        NSManagedObjectContext *context = managedObjectContext();

        // Custom code here...
        // Save the managed object context
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
            exit(1);
        }
        NSError* err = nil;
        NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"json"];
        NSArray* Brands = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                         options:kNilOptions
                                                           error:&err];
        NSLog(@"Imported Brands: %@", Brands);

        NSString* dataPath2 = [[NSBundle mainBundle] pathForResource:@"products" ofType:@"json"];
        NSArray* Products = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath2]
                                                          options:kNilOptions
                                                            error:&err];
        NSLog(@"Imported Products: %@", Products);

    }
    return 0;
}

问题是,它创建了 .sqlite 数据库(结构没问题),但是没有数据!!!

我的数据库是这样的:

http://f.cl.ly/items/470h0d0F2S3j1n2N2R35/screeen.png

例如,这是我的品牌 json:

[{
"id":"1",
"name":"TestBrand",
"description":"",
"website":"",
"email":"",
"address":"",
"phone":"",
"from_country_list":"CZ",
"created_at":"2013-11-24 11:51:17.363473",
"updated_at":"2013-11-24 11:51:17.363473"
}]

关于为什么不将数据导入 .sqlite db 的任何帮助/提示? 非常感谢。

最佳答案

继续教程。您将必须遍历从 JSON 文件创建的对象,并将每个实例添加到 Core Data 对象图中,用可用属性填充它,最后保存上下文。

只有在这最后一步之后,数据才会存储在 sqlite 数据库中。

关于ios - 将 JSON 导入核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176460/

相关文章:

ios - 无法使用CoreData配置RestKit

ios - XCode:无法使用 -arch arm64 指定 -Q

ios - 如何制作线轨动画 CALayer?

c++ - 在数据库中插入空行而不是传递值

c# - SQLite 连接策略

ios - 使用 CoreData 和 Swift 2.0 保存问题

ios - 添加 CADisplayLink 到 Cocos2D

ios - 授权 header 在获取中不起作用 - React Native

sqlite - 在 SQLite 数据库中插入当前日期和时间

swift - Core Data 多 NSManagedObjectContext 性能难题