【发布时间】:2014-07-26 13:13:19
【问题描述】:
以下是使用 RKObjectRequestOperation 获取方法的函数,如何使用 RKObjectManager 编写相同的方法
NSString *hostName = [NSString stringWithFormat:@"%@",[self getHostName]];
NSString *urls = [[NSString alloc] initWithFormat:@"%@/randomcollection/%@/0/%li",hostName,profileId,(long)noOfItems];
RKObjectMapping *responseMapping = [MappingProvider collectionMapping ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:urls];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly timeoutInterval:90.0f];
[request setValue:[GameManager sharedInstance].authenticatedUser.appTokenId forHTTPHeaderField:@"tokenId"];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success) {
RKLogInfo(@"Load collection: %@", mappingResult.array);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (failure) {
failure(operation, error);
}
}];
[objectRequestOperation start];
编辑:这是我尝试过的,它给了我 * 断言失败的错误 -[RKRouter URLWithRoute:object:]
NSString *hostName = [NSString stringWithFormat:@"%@",[self getHostName]];
NSString* urls = [[NSString alloc] initWithFormat:@"%@/userprofile/selectbyid/%@",hostName,profileId];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urls]];
[manager.HTTPClient setDefaultHeader:@"tokenId" value:[GameManager sharedInstance].authenticatedUser.appTokenId];
RKObjectMapping *responseMapping = [MappingProvider userProfileRespMapping ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];
UserProfile * modelObject = [[UserProfile alloc] init];
[modelObject setProfileId:profileId];
[[RKObjectManager sharedManager] getObject:modelObject
path:nil
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success) {
RKLogInfo(@"Load collection of Userprofiles: %@", mappingResult.array);
NSLog(@"Mapping Results %@",[mappingResult.array firstObject]);
UserProfile *currentUserProfile = (UserProfile *)[mappingResult.array firstObject];
success(currentUserProfile);
}
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (failure) {
RKLogError(@"Operation failed with error: %@", error);
failure(operation, error);
}
}];
【问题讨论】:
-
尝试编写对象管理器版本,如果它不起作用,请显示您尝试过的内容并解释它做错了什么
-
@ok wain 我会写下编辑问题
-
@Wain : 请检查上面我写的函数,它在路由函数中给了我错误
标签: ios objective-c restkit restkit-0.20