核心数据中的 Restkit 复杂对象映射,带有嵌套对象数组
我正在使用 coredata 和 Restkit 来映射数据。
这是 getsales 调用的 json 响应,
{ "success":true,
"sales" : [
{
"brands" :[
{"id":"637", "name":"XYZ"},
{"id":"638", "name":"abc"}
]
"end_date" = "2011-10-14 12:00:00",
"id" = 3794,
"image" = "http://dummy.something.com.jpg",
"name" = "test",
},
{
"brands" =[
{"id":"640", "name":"abc"}
],
"end_date" = "2011-10-14 12:00:00",
"id" = 3766,
"image" = "http://dummy.something.com.jpg",
"name" = "text2",
},
{
"brands" =[
{"id":"641", "name":"XYZ"},
{"id":"642", "name":"abc"},
{"id":"643", "name":"def"}
],
"end_date" = "2011-11-06 12:00:00",
"id" = 3798,
"image" = "http://dummy.something.com.jpg",
"name" = "test3",
}
]
}
我
@interface Sale : NSManagedObject{
}
@property (nonatomic, retain) NSNumber * ID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSString * imageUrl;
@implementation Sale
@dynamic ID;
@dynamic name;
@dynamic startDate;
@dynamic endDate;
@dynamic imageUrl;
正在尝试按以下方式映射响应
- (void)getSales{
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://baseurl.com"];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"base.sqlite"];
objectManager.objectStore = objectStore;
RKManagedObjectMapping* saleMapping = [RKManagedObjectMapping mappingForClass:[Sale class]];
[saleMapping mapKeyPathsToAttributes:
@"id", @"ID",
@"name", @"name",
@"start_date",@"startDate",
@"end_date", @"endDate",
@"image", @"imageUrl",
nil];
saleMapping.primaryKeyAttribute = @"ID";
[[RKObjectManager sharedManager].mappingProvider setMapping:saleMapping forKeyPath:@"sales"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getSales/" objectMapping:saleMapping delegate:self];
}
,本质上我有嵌套的对象数组,映射这些对象的正确方法是什么? Sale 类应该有什么类型的属性来存储品牌列表?
感谢任何帮助,我已经浪费了太多时间来解决这个问题。
i am using coredata and restkit to map data.
here is the json response for getsales call
{ "success":true,
"sales" : [
{
"brands" :[
{"id":"637", "name":"XYZ"},
{"id":"638", "name":"abc"}
]
"end_date" = "2011-10-14 12:00:00",
"id" = 3794,
"image" = "http://dummy.something.com.jpg",
"name" = "test",
},
{
"brands" =[
{"id":"640", "name":"abc"}
],
"end_date" = "2011-10-14 12:00:00",
"id" = 3766,
"image" = "http://dummy.something.com.jpg",
"name" = "text2",
},
{
"brands" =[
{"id":"641", "name":"XYZ"},
{"id":"642", "name":"abc"},
{"id":"643", "name":"def"}
],
"end_date" = "2011-11-06 12:00:00",
"id" = 3798,
"image" = "http://dummy.something.com.jpg",
"name" = "test3",
}
]
}
i have
@interface Sale : NSManagedObject{
}
@property (nonatomic, retain) NSNumber * ID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSString * imageUrl;
@implementation Sale
@dynamic ID;
@dynamic name;
@dynamic startDate;
@dynamic endDate;
@dynamic imageUrl;
I am trying to map the response as following
- (void)getSales{
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://baseurl.com"];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"base.sqlite"];
objectManager.objectStore = objectStore;
RKManagedObjectMapping* saleMapping = [RKManagedObjectMapping mappingForClass:[Sale class]];
[saleMapping mapKeyPathsToAttributes:
@"id", @"ID",
@"name", @"name",
@"start_date",@"startDate",
@"end_date", @"endDate",
@"image", @"imageUrl",
nil];
saleMapping.primaryKeyAttribute = @"ID";
[[RKObjectManager sharedManager].mappingProvider setMapping:saleMapping forKeyPath:@"sales"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getSales/" objectMapping:saleMapping delegate:self];
}
essentially i have nested arrays of objects, what is the correct way to map these objects??
and what type of property should Sale class have to store the brands list??
Any help is appreciated, I already wasted too much time fixing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加:
[saleMapping mapKeyPath:@"brands" toRelationship:@"brands" withMapping:[BrandObject objectMapping]];
其中
[BrandObject objectMapping]
是 BrandObject 的映射 (具有id
和name
属性的 NSManagedObject)。本质上,您可以使用
mapKeyPath:toRelationship:withMapping:
来嵌套映射。您还需要向 Sale 对象添加一个NSSet
类型的属性:@property (nonatomic, keep) NSSet *brands;
最后,在实现销售对象,添加:
@dynamic Brands
以及其他 @dynamic 语句。希望这有帮助!谢谢。
Try adding:
[saleMapping mapKeyPath:@"brands" toRelationship:@"brands" withMapping:[BrandObject objectMapping]];
where
[BrandObject objectMapping]
is the mapping for BrandObject (an NSManagedObject withid
andname
properties).Essentially you can use
mapKeyPath:toRelationship:withMapping:
to nest mappings. You'll also need to add a property to your Sale object with the typeNSSet
:@property (nonatomic, retain) NSSet *brands;
Finally, in the implementation of the Sale object, add:
@dynamic brands
alongside the other @dynamic statements.Hope this helps! Thanks.