魔法记录不救 [英] Magical Record not saving

查看:138
本文介绍了魔法记录不救的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Magical Record:
https://github.com/magicalpanda/MagicalRecord

I am using Magical Record: https://github.com/magicalpanda/MagicalRecord

我试图使用Magical Record和Core Data将我从网站获得的记录保存到sqllite数据库,但我一直收到错误:

I am trying to save records that I get the from my website to the sqllite database using Magical Record and Core Data, but I keep getting the error:

MR_saveWithErrorCallback:](0xaaa6bd0) NO CHANGES IN CONTEXT <NSManagedObjectContext (0xaaa6bd0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD *** - NOT SAVING

这是代码:

             for(int i = 0; i < count; i += 1)
             {
                 // results  = array of all services from site
                 NSDictionary * result = [results objectAtIndex: i];

                 NSNumber * sid = @([[result objectForKey: @"id"] intValue]);
                 NSNumber * parent = @([[result objectForKey: @"parent"] intValue]);
                 Service * service  = [Service createEntity];

                 NSString * image = [NSString stringWithFormat: @"%@", [result objectForKey: @"image"]];

                 NSString * name  = [NSString stringWithFormat: @"%@", [result objectForKey: @"name"]];
                 NSString * machine_name  = [NSString stringWithFormat: @"%@", [result objectForKey: @"machine_name"]];


                 [service setDate: [NSDate date]];
                 [service setSid: sid];
                 [service setName: name];

                 [service setImage: image];
                 [service setParent: parent];
                 [service setMachine_name: machine_name];


                 [[NSManagedObjectContext defaultContext] saveNestedContexts];


推荐答案

问题是你没有保存正确的上下文。如果查看源代码, [Service createEntity] 在当前线程的上下文中创建ManagedObject。不是在defaultContext。

The issue is that you are not saving the right context. If you look at the source, [Service createEntity] creates the ManagedObject in the context for the current thread. Not in the defaultContext.

所以你需要做的是,而不是 [[NSManagedObjectContext defaultContext] saveNestedContexts] ,你应该保存当前线程的上下文(即创建ManagedObject的上下文)。所以代码应该是 [[NSManagedObjectContext MR_contextForCurrentThread]

So what you need to do is, instead of [[NSManagedObjectContext defaultContext] saveNestedContexts], you should be saving the context for the current thread (i.e the context which the ManagedObject was created in). So the code should be [[NSManagedObjectContext MR_contextForCurrentThread]

这篇关于魔法记录不救的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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