2

我正在尝试使用 MKNetworkEngine 但标题谈论缓存。这对我的应用程序来说是完全不利的,因为它需要下载货币汇率 JSON 文件并且缓存是不行的。

有没有办法为整个 MKNetworkEngine 实例关闭缓存?

4

1 回答 1

6

首先要做的事情:你真的确定你的缓存用例吗?大多数提供 Web 服务的股票都将其 Cache-Pragma/ETags 标头设置为合理的值。如果他们这样做了,MKNetworkKit 将做正确的事情,并且只会以缓存命中响应您的代码,如果这对您的请求有效。

尽管如此,您可以通过两种方法控制缓存的利用率。来自MKNetworkEngine.h

    /*!
 *  @abstract Enqueues your operation into the shared queue.
 *  
 *  @discussion
 *  The operation you created is enqueued to the shared queue. 
 *  When forceReload is NO, this method behaves like enqueueOperation:
 *  When forceReload is YES, No cached data will be returned even if cached data is available.
 *  @seealso
 *  enqueueOperation:
 */
-(void) enqueueOperation:(MKNetworkOperation*) operation forceReload:(BOOL) forceReload;

enqeueOperation:forceReload:将forceReload 设置为调用YES就可以了。像这样:

-(MKNetworkOperation *)myNetworkOperation onCompletion:(MYComplectionBlock)completionBlock onError:(MKNKErrorBlock)errorBlock {

    MKNetworkOperation *op = [self operationWithPath:kURLPath params:nil httpMethod:@"GET" ssl:NO];
    [op onCompletion:^(MKNetworkOperation *completedOperation) {
        // handle the response
        completionBlock(...)
    } onError:^(NSError *error) {
        errorBlock(error);
    }];

    [self enqueueOperation:op forceReload:YES];
    return op;  
}

此外,您可以在子类内部调用[self emptyCache]before显式清空缓存(注意这意味着清空所有请求的缓存) 。[self enqueueOperation:op]MKNetworkEngine

-(MKNetworkOperation *)myNetworkOperation onCompletion:(MYComplectionBlock)completionBlock onError:(MKNKErrorBlock)errorBlock {

    MKNetworkOperation *op = [self operationWithPath:kURLPath params:nil httpMethod:@"GET" ssl:NO];
    [op onCompletion:^(MKNetworkOperation *completedOperation) {
        // handle the response
        completionBlock(...)
    } onError:^(NSError *error) {
        errorBlock(error);
    }];

    [self emptyCache];
    [self enqueueOperation:op];
    return op;  
}
于 2012-09-21T14:43:41.560 回答