MagicalRecord MR_importFromObject 未保存

Posted

技术标签:

【中文标题】MagicalRecord MR_importFromObject 未保存【英文标题】:MagicalRecord MR_importFromObject not saved 【发布时间】:2014-09-29 09:09:55 【问题描述】:

我在使用 MR 导入 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)

非常感谢任何cmets或积分。

干杯

【问题讨论】:

【参考方案1】:

您是否尝试过改用MR_importFromArray

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

【讨论】:

【参考方案2】:

你必须保存上下文。

if (![localContext save:&error]) 
    // ...

【讨论】:

以上是关于MagicalRecord MR_importFromObject 未保存的主要内容,如果未能解决你的问题,请参考以下文章

MagicalRecord:后台保存

未找到 MagicalRecord/MagicalRecordDeprecationMacros.h 文件

WatchKit 和 MagicalRecord - 这可能吗?

MagicalRecord 3.0 设置

MagicalRecord 日期解析

Simperium和MagicalRecord