【问题标题】:Caching of synchronous connection同步连接的缓存
【发布时间】:2014-02-12 10:50:05
【问题描述】:

我遇到了下一个方法的下一个问题

+ (NSString *)sendRequest:(NSMutableURLRequest *)request {

NSHTTPURLResponse *response;
NSError *error;
NSData *responseData;
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];

responseData = [NSURLConnection sendSynchronousRequest:reqq returningResponse:&response error:&error];

//actions with responseData.
}

我注意到这个请求永远不会缓存数据。缓存仍然是空的。有没有办法通过同步连接缓存响应数据,或者我应该重新设计接收数据到异步连接的方式?

谢谢

【问题讨论】:

    标签: ios caching nsurlconnection synchronous


    【解决方案1】:

    NSURLCache 通过将 NSURLRequest 对象映射到 NSCachedURLResponse 对象来实现对 URL 加载请求的响应的缓存。 NSURLCache 为应用程序的 URL 请求提供了一种复合的内存和磁盘缓存机制。

    didFinishLaunchingWithOptions: 方法中使用此代码并设置 http 请求缓存策略

    - (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    {
      NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 
                                                           diskCapacity:20 * 1024 * 1024 
                                                               diskPath:nil];
      [NSURLCache setSharedURLCache:URLCache];
    }
    

    更多信息请参考this博客

    【讨论】:

    • [NSURLCache sharedURLCache] 已经设置好了。但看起来这不是问题的原因
    猜你喜欢
    • 2011-04-30
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 2015-05-07
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多