核心数据插入具有现有关系的新数据 [英] Core data insert new data with existing relationship

查看:61
本文介绍了核心数据插入具有现有关系的新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对核心数据有疑问,我有一个完全像这样的表

I got problem with core data, I had a table that exactly look like this

用户(与RackItem的关系)

User (relationship with) Rack (relationship with) RackItem

我应该如何将新对象插入到具有用户中现有项目对象和现有Rack对象的rackItem中

How should I insert new object to rackItem with existing item object in User and existing Rack object

我尝试获取现有对象

predicate = 
        [NSPredicate predicateWithFormat:@"userID = %@ AND rack.rackID = %@", 
                                        _userID, 
                                        @"1"];
entityName = @"RackItem";
NSArray *result = [self fetchDataWithEntity:entityName predicate:predicate];
id object = [result last object];

然后在setRackItem部分上,我创建一个新的EntityDescription并使用此功能将其保存到核心数据中

then on the setRackItem part I create a new EntityDescription and save it to core data using this function

- (id)objectInManagedObjectContextForDictionary:(NSDictionary *)dictionary 
                                     entity:(NSString *)entityName 
                              managedObject:(id)object 
                                   insertKey:(NSArray *)keys 
                                   relation:(BOOL)isRelation {
// Recursive method
for (id key in dictionary) {
    id value = [dictionary objectForKey:key];
    NSString *camelCase = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) 
                                                       withString:[[key substringToIndex:1] uppercaseString]];
    if ([value isKindOfClass:[NSDictionary class]] && isRelation) {   
        if ([keys containsObject:camelCase] && keys != nil) {
            id newObject = 
                [NSEntityDescription insertNewObjectForEntityForName:camelCase 
                                              inManagedObjectContext:_managedObjectContext];
                value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key] 
                                                                 entity:camelCase 
                                                          managedObject:newObject
                                                              insertKey:keys 
                                                               relation:isRelation];
        } else {
            SEL selector = NSSelectorFromString(key);
            id newObject = [object performSelector:selector];
            value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key] 
                                                             entity:camelCase 
                                                      managedObject:newObject
                                                          insertKey:keys 
                                                           relation:isRelation];
        }
    }
    NSString *methodName = [NSString stringWithFormat:@"set%@:", camelCase];
    SEL selector = NSSelectorFromString(methodName);
    [object performSelector:selector withObject:value];
}
return object;
}

结果很好,数据已成功插入。但是现有对象已被修改。
我认为这是
id object = [result lastObject];的问题。
需要保留。有人可以帮助我吗?

And the result is good, data was inserted successfully. But the existing object got modified. I think this is the problem of id object = [result lastObject]; need to be retain. Anyone can help me?

某些更新
插入后的结果

Some Update The result after followed insert

插入之前

 1|9|2|0|0||||248|52|||1|||||||
 2|9|2|0|0|||||||||||||||
 3|9|2|0|0|||||||||||||||
 4|9|2|0|0|||||||||||||||
 5|9|1|0|0|1||||||||||||||
 sqlite> select *from zrackitem;

插入后

 1|9|2|0|0||||248|52|||1|||||||
 2|9|2|0|0|||||||||||||||
 3|9|2|0|0|||||||||||||||
 4|9|2|0|0|||||||||||||||
 5|9|2|0|0|||||||||||||||
 6|9|1|0|0|1||||||||||||||
 sqlite> select *from zrackitem; 

它表明每次我插入zrack变量的现有行都消失了。

it shows that every time I insert the existing row of zrack variable is gone.

推荐答案

这似乎有点令人困惑...
首先,您当然应该拥有,因为NeverBe说了Rack之间的一对多关系和RackItem

This seems a little confusing ... first of all you should have of course as NeverBe said a to-many relationship between Rack and RackItem

然后在我看来,您在这里遇到了一个概念性问题。
如果要插入与先前RackItem相同的Rack相关的新对象...只需获取该Rack对象,然后将其RackItem设置为刚获取的Rack,即可插入新的RackItem。

Then it seems to me that you're having a conceptual issue here. If you want to insert a new object related to the same Rack than a previous RackItem ... just fetch that Rack Object and insert your new RackItem by setting it's rack property to the Rack you just fetched.

您真的应该保持简单,否则您将有很多Coredata不稳定问题
希望这会有所帮助

You really should keep it simple or you're just gonna have a lot of Coredata inconstancy issues Hope that this will help

这篇关于核心数据插入具有现有关系的新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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