[英]RestKit Router - How to implement routes with nested variables?
過去,我為每個RKResponseDescriptor以及每個RKObjectManager的get / post / patch / delete方法指定了路徑參數。 這行得通,但似乎RKRouter似乎是更好的,更模塊化的方法。
我在設置包含嵌套變量的動態路由時遇到了麻煩。 例如:
添加路線
RKRoute *locationsRoute = [RKRoute routeWithClass:[Location class] pathPattern:@"users/:userID/locations/:locationID" method:RKRequestMethodAny];
[[RKObjectManager sharedManager].router.routeSet addRoutes:@[locationsRoute]];
設置響應描述符
RKResponseDescriptor *locationResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationResponseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
進行對象管理器調用
[[RKObjectManager sharedManager] getObject:[Location class] path:nil parameters:nil success:nil failure:nil];
在使用路由器之前,我將在對象管理器調用路徑中包含userID。 (例如: path:[NSString stringWithFormat:@"users/%@/locations/%@", [self getCurrentUser].userID, location.locationID]
)。 但是,當使用路由器時,我似乎無法弄清楚如何指定它。 正確的方法是什么? 我希望不必將userID硬編碼到路由路徑中。
重要的是要注意,我的所有映射均已正確設置,並且在嘗試實現路由之前,所有操作均正常進行。 任何幫助表示贊賞!
您的路線很好,這是錯誤的:
[[RKObjectManager sharedManager] getObject:[Location class] ...
因為您需要傳遞Location
類的一個實例(而不是類對象),並且該實例需要設置userID
和locationID
屬性,以便可以將它們注入到路由路徑模式中。
牢記韋恩的答案,我最終需要為單個對象使用多種不同的路線。 索引是基於關系的路由。 例如:
指數
RKRoute *locationIndexRoute = [RKRoute routeWithRelationshipName:@"locations" objectClass:[User class] pathPattern:@"users/:userID/locations" method:RKRequestMethodGET];
[[RKObjectManager sharedManager] getObjectsAtPathForRelationship:@"locations" ofObject:[self getCurrentUser] parameters:nil success:nil failure:nil];
更新
RKRoute *locationUpdateRoute = [RKRoute routeWithClass:[Location class] pathPattern:@"users/:userID/locations/:locationID" method:RKRequestMethodPATCH];
[[RKObjectManager sharedManager] patchObject:location path:nil parameters:nil success:nil failure:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.