gpt4 book ai didi

ios - ASIHTTPRequest 缓存不工作

转载 作者:行者123 更新时间:2023-11-28 20:38:38 25 4
gpt4 key购买 nike

我没有使用 ASIHTTPRequest 缓存功能获取缓存结果。我按照文档中的说明进行操作。当我可以访问互联网时,以下内容完美运行。届时,我将打开飞行模式并关闭应用程序的多任务处理。当我这样做时,我没有收到对 requestFinished 的异步调用。我已经尝试了一堆缓存策略,但似乎并没有这样做。有人有什么想法吗?

目标:能够在没有互联网连接的情况下获得缓存的 JSON 响应(从互联网成功加载一次后)。

- (void)viewDidLoad
{
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
[networkQueue reset];
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)];
[networkQueue setShowAccurateProgress:true];
[networkQueue setDelegate:self];

//Turn on default caching
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];

[self loadData];
[super viewDidLoad];
}

-(IBAction)loadData
{
NSURL *url = [NSURL URLWithString:@"http://SOME_JSON_URL"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([request didUseCachedResponse])
{
NSLog(@"Did use cache!");
}

NSString *responseString = [request responseString];
NSLog(@"%@", responseString);
}

最佳答案

这看起来像是 ASIHTTPRequest 认为您的数据已过期。您可以尝试覆盖过期设置并查看其工作原理:

[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];

当然这不是最优的,因为有充分的理由使用缓存过期。但是,它可能会引导您走上正确的轨道。如果确实是过期问题,也许您可​​以确保调整服务器端的缓存设置。

好的缓存策略可能是文档中所述的策略:

[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];

关于ios - ASIHTTPRequest 缓存不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9515649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com