【问题标题】:Not caching specific requests using RestKit不使用 RestKit 缓存特定请求
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 可能是正确的答案!不是第一个调用的web服务,之前缓存了很多小图。但是我不能在这个项目上花更多的时间,所以到现在我还不能尝试。还是非常感谢!
  • 我刚刚尝试解决这个问题,你说得对,这只是关于缓存大小。
猜你喜欢
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2017-10-11
相关资源
最近更新 更多