【问题标题】:MagicalRecord import data for already existing NSManagedObject已经存在的 NSManagedObject 的 MagicalRecord 导入数据
【发布时间】:2014-03-04 19:45:43
【问题描述】:

我发现了一个类似我的问题here,但我没有在答案中看到任何代码。

接下来是工作流程:

我使用下一个代码创建 NSManagedObject

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playerID == %@", responseData[@"player"][@"playerID"]];

Player *player = [Player MR_findFirstWithPredicate:predicate];

// on this line above player attributes are still the same at first time created. the player name is Alex if I po player.firstname in console.  

// on this line below i import new name for player - Greg.
// so I print player.firstname and the console shows me Greg instead of Alex.
// I think it has to update my value but it does't when I try to print our it in the success block.

[player MR_importValuesForKeysWithObject:responseData];

} completion:^{

// here I want to get my players assuming that there is only one Player just for testing.
NSArray *arr = [Player MR_findAll]; // print arr.count = 1 (just test if i work with one and the same entity)

for (Player *p in arr) {
NSLog(@"%@", p.firstname); // type first name and it is Alex, but has to be Greg, because I have print out it after import line and player first name was Greg. But now it's still Alex. What the problem is? 
}

}];

下一步我需要使用导入功能更新这个 NSManagedObject。我该怎么做?

【问题讨论】:

    标签: objective-c magicalrecord


    【解决方案1】:

    看看这个blog post。它解释了使用导入功能的所有非代码相关选项。

    【讨论】:

    • 首先感谢您有时间帮助我解决所有问题,MagicalRecord 是我见过的非常好的东西 =) 你能检查我编辑过的代码吗?也许还有另一种方法可以处理已经导入和存在的记录,但我做了一个小变通方法,找到带有谓词的记录,然后尝试使用 MR_importValuesForKeysWithObject 方法修改对象。所以也许看起来不太好,但似乎必须努力。你怎么看?我已将 cmets 添加到我修改或打印对象数据的每一行。你能看到我更新的问题吗?非常感谢。
    • 我发现我的问题导入适用于版本 2.0.8 我在我的代码中错过了 localContext。如果我使用 Player *player = [Player MR_findFirstWithPredicate:predicate inContext:localContext];一切对我都很好......
    • 我们是否需要将此问题保存给其他可能与我有相同问题的人?你怎么看?
    • 还有一个关于谓词的问题,我想我们可以在没有谓词的情况下导入数据以存在对象?
    猜你喜欢
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2019-11-11
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多