2

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

具体来说:

  1. 我调用场地搜索 API (https://developer.foursquare.com/docs/venues/search) 来检索场地列表。每个 Venue 都有一个唯一的 ID,该 ID 包含在响应中。我在 ViewController 中使用以下内容执行此操作:

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

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

    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
    

    我的场地课有这个:

    @property (strong, nonatomic) NSArray *photos;
    

Venue.photos 总是返回空。有什么建议么?

4

2 回答 2

0

Not sure if this is the 'best' way to do it, but I got it working in the end. Paul de Lange's comment about not mapping the venue and the photos correctly set me off on the correct path.

The foursquare response was not in the format that I required in my app, so I had to use RKObjectLoader's willMapData to modify the response slightly before the rest of the mapping operation.

Here it is in case it helps anybody in the future:

- (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"]; 

}

And in my mapping:

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"];    
于 2012-07-10T07:15:02.013 回答
0

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

于 2012-07-06T06:36:24.773 回答