RestKit RKRelationshipMapping不起作用

时间:2014-11-04 22:42:21

标签: ios objective-c restkit

这是我的JSON

{
    "pages": 8,
    "salads": [
        {
            "id": "392",
            "img": "http://salatiki.com.ua/images/mini/20120114_285.jpg",
            "ingredCount": 5,
            "ingredients": "картофель, свекла, салат, яйцо, сливки",
            "name": "Шведский картофельный",
            "rating": 4
        },
.......
     ]
}

我得到了"页面",但是我的"沙拉"是零(见日志)。

我的NSManagedObjects

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class SSaladsBody;

@interface SSaladPage : NSManagedObject

@property (nonatomic, retain) NSNumber * sPages;
@property (nonatomic, retain) SSaladsBody *salads;

@end

关键&#34;沙拉&#34;

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface SSaladsBody : NSManagedObject

@property (nonatomic, retain) NSNumber * sId;
@property (nonatomic, retain) NSString * sImage;
@property (nonatomic, retain) NSString * sName;
@property (nonatomic, retain) NSNumber * sRating;
@property (nonatomic, retain) NSString * sIngredients;
@property (nonatomic, retain) NSNumber * sIngredientsCount;

@end

在我的添加代理

RKEntityMapping *saladPageMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SSaladPage class]) inManagedObjectStore:managedObjectStore];
[saladPageMapping addAttributeMappingsFromDictionary:@{ @"pages" : @"sPages", }];
saladPageMapping.identificationAttributes = @[@"sPages"];

RKEntityMapping *saladsBodyMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SSaladsBody class]) inManagedObjectStore:managedObjectStore];
[saladsBodyMapping addAttributeMappingsFromDictionary:@{ @"id" : @"sId",
                                                     @"img" : @"sImage",
                                                     @"name" : @"sName",
                                                     @"rating" : @"sRating",
                                                     @"ingredients" : @"sIngredients",
                                                     @"ingredCount" : @"sIngredientsCount" }];
saladsBodyMapping.identificationAttributes = @[@"sId"];

[saladPageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"salads"
                                                                               toKeyPath:@"salads"
                                                                             withMapping:saladsBodyMapping]];

RKResponseDescriptor *saladPageDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:saladPageMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[manager addResponseDescriptorsFromArray: @[saladPageDescriptor]];

[[RKObjectManager sharedManager] getObjectsAtPath:@"api/get.php?getByCat=1&page=1" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Mapping salads OK! %@", mappingResult.array);
}  failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@",error);
}];

并记录

2014-11-04 23:57:49.641 Salatiki[10213:f03] I restkit.network:RKObjectRequestOperation.m:250 GET 'http://salatiki.com.ua/api/get.php?getByCat=1&page=1' (200 OK / 1 objects) [request=0.0989s mapping=0.0117s total=0.1613s]
2014-11-04 23:57:49.641 Salatiki[10213:607] Mapping salads OK! (
    "<SSaladPage: 0x7b03b970> (entity: SSaladPage; id: 0x7b267d90 <x-coredata://8CC2FD2A-9D05-4863-99AA-30B318EEC57C/SSaladPage/p42> ; data: {\n    sPages = 8;\n    salads = nil;\n})"
)

我做错了什么?我尝试添加一个responseDeskriptor,用于&#34;沙拉&#34;它可以工作但是他把数组放到main {};不要&#34;沙拉&#34; = [];谢谢。

1 个答案:

答案 0 :(得分:0)

属性

@property (nonatomic, retain) SSaladsBody *salads;

实际上应该是一种关系,因此它应该是NSSet,而不是SSaladsBody

除此之外,事情看起来还不错。如果仍有问题,则应启用跟踪日志记录,并查看关系内容的处理过程。