我的问题
我无法缓存特定请求,但其他使用相同库和服务器的请求是
http://server.com/lastEntities?authors=37&authors=125&authors=32&authors=36&authors=561&page=0&fromDate=&toDate=&pageSize=20
由于名为“作者”的重复参数,我已经要求后端人员更改此查询字符串,但现在似乎不可能,这是我的 bug 候选人之一,但我不是当然。
我正在使用 Charles Proxy 来查看客户端请求,只有上面的那个总是调用忽略服务器缓存策略并一次又一次地击败网络。
服务器响应
缓存控制:
no-transform, max-age=300
内容类型:
application/json;charset=utf-8
代码
RestKit ~> 0.20.0
设置 NSURL 共享缓存
在 AppDelegate 中:
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
其他盲目的缓存
在我的“RKObjectManager”子类中,我这样做是为了尝试强制缓存,但如果我没有错RestKit 对开发人员透明地使用 NSURLCache,唯一需要的是服务器返回适当的“缓存控制” header 。
- (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
];
}
最佳答案
不在缓存中存储响应的最明显原因是响应的大小。在 NSURLCache 中,有一种机制可以决定它将缓存的最大文件大小。此大小与您的最大缓存大小有关。确切的关系尚不清楚(没有记录)尝试将缓存大小设置为比现在的 10 和 20MB 大得多。
关于ios - 不使用 RestKit 缓存特定请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914630/