【发布时间】:2014-07-23 15:25:51
【问题描述】:
我的问题
我无法缓存特定请求,但其他使用相同库和服务器的请求是的
http://server.com/lastEntities?authors=37&authors=125&authors=32&authors=36&authors=561&page=0&fromDate=&toDate=&pageSize=20
由于名为“authors”的重复参数,我已要求后端人员更改此查询字符串,但现在似乎不可能,这是我的错误候选之一,但我不是当然。
我正在使用 Charles Proxy 来查看客户端请求,只有上面那个总是调用忽略服务器缓存策略并一次又一次地击败网络。
服务器响应
缓存控制:
no-transform, max-age=300
内容类型:
application/json;charset=utf-8
代码
RestKit ~> 0.20.0
设置 NSURL sharedCache
在 AppDelegate 中:
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
其他盲目的缓存尝试
在我的“RKObjectManager”子类中,我这样做是为了尝试强制缓存,但如果我没记错RestKit 为开发人员透明地使用 NSURLCache,唯一需要的是服务器返回适当的“缓存控制”标头。
- (NSMutableURLRequest *)requestWithObject:(id)object method:(RKRequestMethod)method path:(NSString *)path parameters:(NSDictionary *)parameters
{
NSMutableURLRequest *request = [super requestWithObject:object method:method path:path parameters:parameters];
request.cachePolicy = NSURLRequestUseProtocolCachePolicy ;
return request;
}
执行请求
- (void) lastEntities:(NSDictionary *)params
onSuccess:(void (^)(NSArray *entities, int numEntities)) success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure{
NSString *path = @"entity";
NSMutableDictionary *requestParams = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *urlParams = [NSString stringWithFormat:@"%@?",path];
//Compose the URL with query params
[sharedManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:[MVEntityModel responseMapping]
method:RKRequestMethodGET
pathPattern:path
keyPath:@"entities"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromDictionary:@{@"numEntities": @"numEntities"}];
[sharedManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodGET
pathPattern:path
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[self getObjectsAtPath:urlParams parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success) {
success([mappingResult.dictionary objectForKey:@"entities"], (int)[[[mappingResult.dictionary objectForKey:[NSNull null]] objectForKey:@"numEntities"] integerValue]);
}
} failure:nil
];
}
【问题讨论】:
-
您的意思是对于相同的 URL 不使用缓存,或者每个请求中的参数都会发生一些变化?
-
被缓存的其他请求是同一服务器的不同资源,有的使用类似的查询字符串,有的没有使用查询字符串。
标签: ios caching restkit-0.20 request.querystring