2014-01-22 40 views
0

與下一個方法我遇到一個問題的緩存同步連接

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

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

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

//actions with responseData. 
} 

我注意到,數據不會緩存這個請求。緩存保持空白。有什麼辦法用同步連接緩存響應數據,還是應該重新設計我接收異步連接數據的方式?

感謝

回答

0

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]; 
} 

參考更多信息

+0

[NSURLCache sharedURLCache]已經建立this博客。但看起來不是這是問題的原因 – NewObjective

+0

您是否嘗試過didFinishLaunchingWithOptions中的代碼? – suhit

+0

是的,它已經在didFinishLaunchingWithOptions中。 – NewObjective