首页 文章

如何在objective-c中使用Cache Memory概念

提问于
浏览
1

// Web服务请求

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
        NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
        [request setHTTPMethod: @"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody: requestData];

        NSError *respError = nil;
        NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];

  //returndata is response of webservice
 NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];


        NSDictionary *results = [responseString JSONValue] ;
        NSLog(@"chat data- %@",results);

        NSString *strResults = [results objectForKey:@"d"];
        NSLog(@"result string is-%@",strResults);

这是我用于从Web服务获取数据的代码 . 但是每次来到这个页面时我都必须这样做 . 这是任何可以将我的数据存储在Cache内存中的方法,所以我不需要每次都请求 .

我在用

NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

但我不知道如何使用 - (NSCachedURLResponse *)连接此方法,谢谢...

1 回答

  • 1

    您需要根据需要使用缓存documentation:每当您请求 NSMutableURLRequest 时......

    对于Ex:

    [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:60];
    

    详情请咨询Documentation

相关问题