一对多关系不起作用(Restkit 0.20)addRelationshipMappingWithSourceKeyPath

时间:2013-07-01 15:06:54

标签: restkit restkit-0.20

我有一个简单的一对多关系我的JSON,并希望将其映射到对象(非核心数据实体)。映射产品工作正常。但是每个产品都有一系列未映射的曲目。

这是JSON:

enter image description here

以下是代码:

Product.h

#import <Foundation/Foundation.h>

@interface Product : NSObject

@property (nonatomic, strong) NSString* productid;
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) NSString* artist;
//...
@property (nonatomic, strong) NSMutableArray *tracks;


Track.h

#import <Foundation/Foundation.h>

@interface Track : NSObject

@property (nonatomic, strong) NSString *artist;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *mixversion;



这里是请求JSON并将响应映射到Objects

的代码
/// Create Mappers for Product and Track
RKObjectMapping *mappingForProduct = [RKObjectMapping mappingForClass:[Product class]];

[mappingForProduct addAttributeMappingsFromArray:@[@"productid", @"title", @"tracks"]];


RKObjectMapping *mappingForTrack = [RKObjectMapping mappingForClass:[Track class]];
[mappingForTrack addAttributeMappingsFromArray:@[@"artist",@"title",@"mixversion"]];

/// assign relationship product-track
[mappingForProduct addRelationshipMappingWithSourceKeyPath:@"tracks" mapping:mappingForTrack];

/// setup responseDescriptor
NSString *key_path = @"products";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingForProduct
                                                                                   pathPattern:nil
                                                                                       keyPath:key_path
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


/// Create Manager, connect responseDescriptor and execute
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"myRequestString"]];



[manager addResponseDescriptor:responseDescriptor];
[manager getObject:nil path:@"" parameters:nil
           success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
           {
               self.productsArray = [NSMutableArray arrayWithArray:mappingResult.array];
           }

           failure:^(RKObjectRequestOperation *operation, NSError *error)
           {
               NSLog(@"Error: %@", error);
           }


这会产生断言错误:

*** Assertion failure in -[RKObjectMapping addPropertyMapping:],     /Users/.../Pods/RestKit/Code/ObjectMapping/RKObjectMapping.m:235



神奇应该发生在这一行:

[mappingForProduct addRelationshipMappingWithSourceKeyPath:@"tracks" mapping:mappingForTrack];


......但由于某种原因,它没有。

我在SO和文档上搜索了很多来源,尝试了一些密钥路径的组合改变,但没有任何东西可以提供所需的结果。许多可用的示例代码是关于旧版本的RestKit或处理核心数据。我只想获得映射对象,没有核心数据实体。

提前感谢任何提示。

1 个答案:

答案 0 :(得分:1)

您只是尝试两次添加tracks的映射。它应该是关系映射,而不是属性映射。将mappingForProduct更改为:

[mappingForProduct addAttributeMappingsFromArray:@[@"productid", @"title"]];

应该删除异常的原因。