不缓存特定的请求使用RestKit



我的问题

我不能缓存一个特定的请求,但使用相同库和服务器的其他请求是

http://server.com/lastEntities?authors=37&authors=125&authors=32&authors=36&authors=561&page=0&fromDate=&toDate=&pageSize=20

我已经要求后端人员更改此查询字符串由于重复的参数命名为"作者",但似乎现在是不可能的,这是我的候选人之一,是我的错误,但我不确定。

我使用Charles Proxy来查看客户端请求,只有上面的那个总是调用忽略服务器缓存策略,并且一次又一次地击败网络。

<标题>服务器响应h1>
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
 ];
}

不将响应存储在缓存中最明显的原因是响应的大小。在NSURLCache中有一个机制来决定它将缓存的最大文件大小。此大小与您的最大缓存大小相关。确切的关系不清楚(没有文档记录)尝试将缓存大小设置得比现在的10和20MB大得多。

相关内容

  • 没有找到相关文章

最新更新