[objective-c]MagicalRecord MR_importFromObject 不保存

标签: json ios Objective-C
发布时间: 2016/10/2 4:10:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有问题用先生导入 json。

我设置了 mappedKeyName 的名称的名称,顺序作为顺序

这是我的 json

[{"order":0,"name":"out and about"},
{"order":1,"name":"entertainment"},
{"order":2,"name":"get creative"},
{"order":3,"name":"chill"},
{"order":4,"name":"get active"},
{"order":5,"name":"get connected"},
{"order":6,"name":"fresh start"},
{"order":7,"name":"totally random"},
{"order":8,"name":"my favs"}]


-(void)importCategoryData
{
    NSError* err = nil;
    NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"category" ofType:@"json"];
    NSArray *preFillDataDict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                options:kNilOptions
                                                  error:&err];
    NSLog(@"preFillDataArray testArray: %@", preFillDataDict);

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
    {
        for (NSDictionary *catObj in preFillDataDict)
        {
            Planningcategory *planCat = [Planningcategory MR_importFromObject:catObj inContext:localContext];
            NSLog(@"asa");
        }
    }
    completion:^(BOOL success, NSError *error) {
        NSLog(@"Success %d error %@",success,error);
    }];
}

给出的日志︰

NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING
Success 0 error (null)

不胜感激任何意见或点。

欢呼

解决方法 1:

你有试过使用 MR_importFromArray 相反吗?

[MagicalRecord saveWithBlock: ^(NSManagedObjectContext *localContext) {
    [Planningcategory MR_importFromArray:preFillDataDict];
} completion: ^(BOOL success, NSError *error) {
    // Handle success/error
}];
赞助商