2012-07-20 57 views
3

我試圖使用對象加載器從API中獲取對象,需要傳入一個類型作爲查詢參數(例如http://example.com/api/households/:householdId/checklist_items?kind=todo),但不管我嘗試註冊資源路徑的方式似乎不是想要找到我的映射。如何將RestKit資源路徑映射與查詢字符串一起使用?

我設置了這樣的映射:

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RMChecklistItem class]]; 
[mapping mapKeyPath:@"id" toAttribute:@"checklistItemId"]; 
[mapping mapKeyPath:@"kind" toAttribute:@"kind"]; 
[mapping mapKeyPath:@"title" toAttribute:@"title"]; 
[mapping mapKeyPath:@"completed" toAttribute:@"completed"]; 
[mapping mapKeyPath:@"abilities" toAttribute:@"abilities"]; 
[mapping mapKeyPath:@"comments" toRelationship:@"comments" 
     withMapping:[provider objectMappingForClass:[RMComment class]]]; 

而且使用的基本路徑和查詢字符串版本與對象映射提供商註冊它:

[provider addObjectMapping:mapping]; 
[provider setObjectMapping:mapping forResourcePathPattern:@"/api/households/:householdId/checklist_items"]; 
[provider setObjectMapping:mapping forResourcePathPattern:@"/api/households/:householdId/checklist_items?kind=:kind"]; 

但是,當我嘗試使用它:

NSDictionary *params [NSDictionary dictionaryWithObject:@"todo" forKey:@"kind"]; 
NSString *path = [NSString stringWithFormat:@"/api/households/%i/checklist_items", householdId.intValue]; 
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[path stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) { 
    loader.onDidLoadObjects = ^(NSArray *objects) { 
     NSLog(@"%@", objects); 
    }; 
    loader.onDidFailWithError = ^(NSError *error) { 
     NSLog(@"%@", error); 
    }; 
    loader.onDidFailLoadWithError = ^(NSError *error) { 
     NSLog(@"%@", error); 
    }; 
}]; 

它去不好:

2012-07-20 12:13:18.533 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:336 Performing object mapping sourceObject: (
     { 
     abilities =   { 
      destroy = 1; 
     }; 
     comments =   (
     ); 
     completed = 0; 
     id = 44; 
     kind = todo; 
     title = "and do the dishes"; 
    }, 
     { 
     abilities =   { 
      destroy = 1; 
     }; 
     comments =   (
         { 
       body = "your!"; 
       "created_at" = "2012-07-18T22:20:08-05:00"; 
       "creator_id" = 16; 
       id = 70; 
      } 
     ); 
     completed = 0; 
     id = 43; 
     kind = todo; 
     title = "pick you shit up"; 
    }, 
     { 
     abilities =   { 
      destroy = 1; 
     }; 
     comments =   (
         { 
       body = "did it"; 
       "created_at" = "2012-07-18T22:19:56-05:00"; 
       "creator_id" = 16; 
       id = 69; 
      } 
     ); 
     completed = 0; 
     id = 40; 
     kind = todo; 
     title = "Pay Rent"; 
    } 
) 
and targetObject: (null) 
2012-07-20 12:13:18.539 roommates[22399:13d03] T restkit.object_mapping:RKObjectMapper.m:293 Examining keyPath 'users' for mappable content... 
2012-07-20 12:13:18.539 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:120 Found a collection containing only NSNull values, considering the collection unmappable... 
2012-07-20 12:27:47.702 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:303 Found unmappable value at keyPath: users 
2012-07-20 12:27:47.705 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:367 The following operations are in the queue: (
) 
2012-07-20 12:27:51.065 roommates[22399:13d03] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: '' 
2012-07-20 12:27:51.066 roommates[22399:13d03] E restkit.network:RKObjectLoader.m:234 Encountered errors during mapping: Could not find an object mapping for keyPath: '' 

回答

2

所以看在調試器中的流動,我碰到RKObjectMappingProvider的mappingForPatternMatchingString:context:這就要求RKPathMatcher的matchesPath:string tokenizeQueryStrings:NO parsedArguments:nil所以我想這是由設計。

我真的只有兩個可能的查詢參數'todo'和'shopping'的值,所以註冊兩個都不是不合理的。

更新:我提出,改變了這種查詢字符串來解析令牌的拉請求:https://github.com/RestKit/RestKit/pull/871