iOS上的RestKit - 后续API调用嵌套对象?

时间:2012-07-05 14:09:03

标签: ios xcode restkit

我使用RestKit将数据从Foursquare API提取到我的iphone应用程序中,但是对嵌套对象的后续API调用有问题。

具体做法是:

  1. 我打电话给Venues搜索API(https://developer.foursquare.com/docs/venues/search)来检索场地列表。每个Venue都有一个唯一的ID,包含在响应中。我在ViewController中执行以下操作:

    [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self]; 
    
  2. 然后我执行RestKit映射等并将Venues存储在数据数组中。一切正常到此为止。

  3. 此时我遍历数据数组中的Venues,并且必须进行后续API调用以检索有关每个Venue的更多详细信息。对于每个Venue,我使用唯一ID并调用Venue detail API(https://developer.foursquare.com/docs/venues/venues)。这是困扰我的部分。我试图从第二次API调用返回一个NSArray的Photo对象。到目前为止,我尝试过各种变体:

    for (id venue in self.data){
        Venue *myvenue = (Venue *)venue;
        RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[NSString stringWithFormat:@"/venues/%@", myvenue.venueid]  queryParameters:queryParams];
        [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self];
    }
    

    并在我的映射中:

    RKObjectMapping *photosMapping = [RKObjectMapping mappingForClass:[Photo class]];
    [photosMapping mapKeyPathsToAttributes:@"url", @"imageURL", nil];  
    [venueMapping mapRelationship:@"photos" withMapping:photosMapping];
    [objectManager.mappingProvider setMapping:photosMapping forKeyPath:@"response.photos"];  // not sure if this keypath is for the second API call
    

    我的Venue课程有:

    @property (strong, nonatomic) NSArray *photos;
    
  4. Venue.photos总是返回空白。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

根据您发布的代码,您无法将回复链接到场地。我不确定foursquare返回的确切JSON,但您需要设想一种使用具有嵌套照片对象的场地映射进行映射的方法。如果foursquare没有在其响应中返回对原始Venue对象的一些引用,则可以尝试将RKObjectLoader的targetObject属性设置为Venue(代码中的myVenue)

答案 1 :(得分:0)

不确定这是否是“最佳”方式,但我最终得到了它。 Paul de Lange关于没有正确映射场地和照片的评论正确地让我走上了正确的道路。

foursquare响应不是我在我的应用程序中所需的格式,因此我必须使用RKObjectLoader的willMapData在映射操作的其余部分之前稍微修改响应。

这是为了以后它可以帮助任何人:

- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableData {
    NSArray* origPhotosArray = [*mappableData valueForKeyPath:@"response.venue.photos.groups"];
    NSMutableArray* newPhotosArray =  [[NSMutableArray alloc] init];  

    // <snip> Some extra code goes here where I discarded photos I did not want....  </snip>

    // Create copies of objects in the format that I want
    NSMutableDictionary *myphoto = [[NSMutableDictionary alloc] initWithDictionary:Photo];
    NSString *venueID = [*mappableData valueForKeyPath:@"response.venue.id"];
    [myphoto setObject:venueID forKey:@"assignedVenueid"];
    [newPhotosArray addObject:myphoto];

    // Replace the new objects instead of the response objects
    [*mappableData removeObjectForKey:@"response.venue.photos.groups"];      
    [*mappableData setObject:newPhotosArray forKey:@"response.venue.photos.groups"]; 

}

在我的地图中:

RKManagedObjectMapping *photosMapping = [RKManagedObjectMapping mappingForClass:[Photo class] inManagedObjectStore:objectStore];
[photosMapping mapKeyPath:@"url" toAttribute:@"imageURL"];
[photosMapping mapKeyPath:@"assignedVenueid" toAttribute:@"assignedVenueid"];
[venueMapping mapRelationship:@"photos" withMapping:photosMapping];
[objectManager.mappingProvider setMapping:photosMapping forKeyPath:@"response.venue.photos.groups"]; 
[photosMapping hasOne:@"assignedVenue" withMapping:venueMapping];
[photosMapping connectRelationship:@"assignedVenue" withObjectForPrimaryKeyAttribute:@"assignedVenueid"];