核心数据中的 Restkit 复杂对象映射,带有嵌套对象数组

发布于 2024-12-10 01:09:40 字数 2257 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-12-17 01:09:40

尝试添加:

[saleMapping mapKeyPath:@"brands" toRelationship:@"brands" withMapping:[BrandObject objectMapping]];

其中 [BrandObject objectMapping] 是 BrandObject 的映射 (具有 idname 属性的 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 with id and name 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 type NSSet:

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文