3

假设在我的数据模型中,我有Pages,其中有很多Comments

我想连接模型中的关系,该关系从评论返回到它所属的页面,但页面对象没有嵌套在响应中,也没有任何可以识别响应中存在的父页面的主键.

在我调用的时候loadObjectsAtResourcePath,所有Comments加载的都应该属于一个固定的、已知的Page对象。我可以建立这种关系的一种方法是:

loader.onDidLoadObjects = ^(NSArray* objs) {
    for (Comment* comment in objs) comment.page = self.page;
    ...
}

但我希望有更好的方法。请注意,我不能使用connectRelationship方法系列,因为响应中没有主键可以让我将每个方法都Comment连接到Page.

4

1 回答 1

2

您可以使用委托方法- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableData在映射步骤之前注入额外的参数。如果您使用核心数据,它可以确保 RestKit 正确保存对象和关系(请注意,您上面提供的解决方案不会保存关系)。

或者,查看这个答案,我在其中展示了如何覆盖RKObjectLoader以从 URL 本身检索页面信息。

编辑:这是我在评论中提到的类别:

。H

#import <RestKit/RestKit.h>

typedef void(^RKObjectLoaderWillMapDataBlock)(id* mappableData);

@interface RKObjectLoader (Extended)

@property (nonatomic, copy) RKObjectLoaderWillMapDataBlock onWillMapData;

@end

还有他们:

#import <objc/runtime.h>

NSString* kOnWillMapDataKey = @"onWillMapData";

@implementation RKObjectLoader (Extended)

- (RKObjectLoaderWillMapDataBlock) onWillMapData {
    return objc_getAssociatedObject(self, &kOnWillMapDataKey);
}

- (void) setOnWillMapData:(RKObjectLoaderWillMapDataBlock) block {
    objc_setAssociatedObject(self, &kOnWillMapDataKey, block, OBJC_ASSOCIATION_COPY);
}

- (RKObjectMappingResult*)mapResponseWithMappingProvider:(RKObjectMappingProvider*)mappingProvider toObject:(id)targetObject inContext:(RKObjectMappingProviderContext)context error:(NSError**)error {
    id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:self.response.MIMEType];
    NSAssert1(parser, @"Cannot perform object load without a parser for MIME Type '%@'", self.response.MIMEType);

    // Check that there is actually content in the response body for mapping. It is possible to get back a 200 response
    // with the appropriate MIME Type with no content (such as for a successful PUT or DELETE). Make sure we don't generate an error
    // in these cases
    id bodyAsString = [self.response bodyAsString];
    RKLogTrace(@"bodyAsString: %@", bodyAsString);
    if (bodyAsString == nil || [[bodyAsString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        RKLogDebug(@"Mapping attempted on empty response body...");
        if (self.targetObject) {
            return [RKObjectMappingResult mappingResultWithDictionary:[NSDictionary dictionaryWithObject:self.targetObject forKey:@""]];
        }

        return [RKObjectMappingResult mappingResultWithDictionary:[NSDictionary dictionary]];
    }

    id parsedData = [parser objectFromString:bodyAsString error:error];
    if (parsedData == nil && error) {
        return nil;
    }

    // Allow the delegate to manipulate the data
    if ([self.delegate respondsToSelector:@selector(objectLoader:willMapData:)]) {
        parsedData = [parsedData mutableCopy];
        [(NSObject<RKObjectLoaderDelegate>*)self.delegate objectLoader:self willMapData:&parsedData];
    }

    if( self.onWillMapData ) {
        parsedData = [parsedData mutableCopy];
        self.onWillMapData(&parsedData);
    }

    RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
    mapper.targetObject = targetObject;
    mapper.delegate = (id<RKObjectMapperDelegate>)self;
    mapper.context = context;
    RKObjectMappingResult* result = [mapper performMapping];

    // Log any mapping errors
    if (mapper.errorCount > 0) {
        RKLogError(@"Encountered errors during mapping: %@", [[mapper.errors valueForKey:@"localizedDescription"] componentsJoinedByString:@", "]);
    }

    // The object mapper will return a nil result if mapping failed
    if (nil == result) {
        // TODO: Construct a composite error that wraps up all the other errors. Should probably make it performMapping:&error when we have this?
        if (error) *error = [mapper.errors lastObject];
        return nil;
    }

    return result;
}

@end
于 2012-08-03T06:53:20.363 回答