将JSON导入Core Data

时间:2013-11-24 15:46:05

标签: 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的任何帮助/提示? 非常感谢。

1 个答案:

答案 0 :(得分:1)

继续学习本教程。您将不得不遍历从JSON文件创建的对象,并将每个实例添加到Core Data对象图中,使用可用属性填充它,最后填充save上下文。

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