我的應用程序使用協議JSON-RPC與後端進行通信。我基本上想要做的是獲取對象並將它們直接存儲在Core Data中。我有多種類型的實體。我們舉個例子:活動和播客。我通過在兩個實體的POST請求下在同一終端點擊API來獲取它們。唯一的改變是PARAMS:取決於請求參數的地圖資源
對於事件:
{
id = 0;
jsonrpc = "2.0";
method = "Events.Event.list";
params = {
location = {
type = token;
value = haHssQWR0V8d;
};
sessionId = v1oCLGlfxIvqYxhaHssQWR0V8dkFeS1JUqlF;
week = "2014_42";
};
}
對於播客:
{
id = 1;
jsonrpc = "2.0";
method = "Podcasts.Podcast.list";
params = {
sessionId = v1oCLGlfxIvqYxhaHssQWR0V8dkFeS1JUqlF;
};
}
我目前正在創建響應描述符,就像每一個實體映射這個。
+ (void)configureAllObjectsMapping
{
[self mapEvent];
[self mapPodcast];
}
+ (RKEntityMapping *)mapEvent
{
if (_eventMapping) {
return _eventMapping;
}
_eventMapping = [self mappingForClass:[Event class]];
_eventMapping.identificationAttributes = @[CoreDataPrimaryKey];
[_eventMapping addAttributeMappingsFromDictionary:@{
@"token":@"token",
@"name":@"name",
@"urlWeb":@"urlWeb",
@"urlImage":@"urlImage",
@"startsAt":@"startsAt",
@"endsAt":@"endsAt",
@"costs":@"costs",
@"description":@"desc",
@"genres":@"genres",
@"artists.isSecret":@"hasSecretArtist",
@"hasGuestlist":@"hasGuestlist",
@"countGoings":@"countGoings"
}];
[_eventMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"venue"
toKeyPath:@"venue"
withMapping:[self mapVenue]]];
[_eventMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"artists.data"
toKeyPath:@"artists"
withMapping:[self mapArtist]]];
RKResponseDescriptor *list = [RKResponseDescriptor responseDescriptorWithMapping:_eventMapping
method:RKRequestMethodPOST
pathPattern:nil
keyPath:@"result.data"
statusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(RKStatusCodeClassSuccessful, 104)]];
[[APIManager sharedInstance].manager addResponseDescriptor:list];
return _eventMapping;
}
+ (RKEntityMapping *)mapPodcast
{
if (_podcastMapping) {
return _podcastMapping;
}
_podcastMapping = [self mappingForClass:[Podcast class]];
_podcastMapping.identificationAttributes = @[CoreDataPrimaryKey];
[_podcastMapping addAttributeMappingsFromDictionary:@{
@"token":@"token",
@"name":@"name",
@"urlWeb":@"urlWeb",
@"urlImage":@"urlImage",
@"description":@"desc",
@"duration":@"duration",
@"playCount":@"playCount"
}];
RKResponseDescriptor *list = [RKResponseDescriptor responseDescriptorWithMapping:_podcastMapping
method:RKRequestMethodPOST
pathPattern:nil
keyPath:@"result.data"
statusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(RKStatusCodeClassSuccessful, 104)]];
[[APIManager sharedInstance].manager addResponseDescriptor:list];
return _podcastMapping;
}
的問題是,無論對於播客和事件實體響應描述符是相同的,因爲pathPattern
是零。
所以我從後端接收一切目前被認爲是播客因爲mapPodcast
方法是mapEvent
方法後調用。
有誰知道一種方法來區分兩個響應並將每個請求的響應映射到正確的實體嗎?
UPDATE:響應
這是樣的反應,我得到了我的資源:
事件
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"count": 1,
"data": [
{
"token": "YAXDMJG17GRO",
"event_name": "Klubnacht | Fachwerk Nacht",
...
}
]
}
}
播客
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"count": 1,
"data": [
{
"token": "G17GROYAXDMJ",
"podcast_name": "Podcast #19",
...
}
]
}
}
所以沒有事件可以區分它們,除了一些參數名稱。
yhe響應中是否有任何內容告訴您數據類型? – Wain
不是真的(請參閱我的編輯)。唯一可以將不同的響應區分爲'data'數組中某些參數的名稱。 –