问题描述
我有一个与iOS Restkit相关的问题。我有来自远程服务器的父子关系数据,并使用Restkit将这些对象映射到 NSManagedObject
对象。我目前遇到的问题是每次向服务器发出的请求都会消除子关系并将其替换为来自服务器的新数据。有没有办法避免这些并追加新的孩子?
I have a iOS Restkit related question. I have a parent-child relationship data coming from a remote server and map those object to a NSManagedObject
object with Restkit. The problem that I am currently having is every request to the server always wipe out the "child" relationship and replace it with the new data coming from the server. Is there a way to avoid those and append the new child instead?
例如:我有一个经典的Category - > Products关系。
For example: I have a classic Category --> Products relationship.
{"categories": [
{
"cat_id": "1",
"cat_title": "category 1",
"cat_tag": 1,
"product": [
{
"prod_id": "1",
"prod_name": "product 1",
"prod_tag": 1
},
{
"prod_id": "2",
"prod_name": "product 2",
"prod_tag": 1
}
]
}
] }
这样可以正常使用CoreData上的关系保存所有内容。但是如果我向服务器发出另一个请求并有新的回复:
And that works fine and everything is saved properly with the relationship on the CoreData. But if I make another request to the server and have a new response:
{"categories": [
{
"cat_id": "1",
"cat_title": "category 1",
"cat_tag": 1,
"product": [
{
"prod_id": "3",
"prod_name": "product 3",
"prod_tag": 1
},
{
"prod_id": "4",
"prod_name": "product 4",
"prod_tag": 1
}
]
}
] }
我将有产品3和产品4更换数据库中的产品1和产品2。我确信我正确设置了所有的关系和主键。 ( cat_id
和 prod_id
都设置为主键。)
I will have product 3 and product 4 replace product 1 and product 2 on the database. I am sure I setup all the relationship and primary key correctly. (Both cat_id
and prod_id
are set as a primary key).
通过RestKit的内部框架进行调查后,我注意到 RKObjectMappingOperation
类中的第576行
附近
Having investigated through the RestKit's internal framework, I noticed that around line 576
in the RKObjectMappingOperation
class, there is
RKLogTrace(@"Mapped NSSet relationship object from keyPath '%@' to
'%@'. Value: %@", relationshipMapping.sourceKeyPath,
relationshipMapping.destinationKeyPath, destinationObject);
NSMutableSet *destinationSet = [self.destinationObject
mutableSetValueForKey:relationshipMapping.destinationKeyPath];
[destinationSet setSet:destinationObject];
所以我想这很容易改变
[destinationSet setSet:destinationObject];
to
[destinationSet addObjectsFromArray:[destinationObject allObjects]]
但是我想知道是否有更好的方法吗?
But I was wondering whether there is a better way to do it?
干杯,
推荐答案
感谢<$ c的支持$ C> RESKIT 。它现在由 RKRelationshipMapping
支持。()
Thanks for the support from Reskit
. It is now supported by RKRelationshipMapping
.(https://github.com/RestKit/RestKit/issues/989)
只需设置 assignmentPolicy
值 RKRelationshipMapping
实例到 RKUnionAssignmentPolicy
这篇关于Restkit to-many关系附加到set而不是设置新set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!