【问题标题】:How to Use Cache Memory concept in objective-c如何在objective-c中使用缓存内存概念
【发布时间】:2013-11-24 11:07:26
【问题描述】:

// 网络服务请求

        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 服务获取数据的代码。但是每次访问此页面时,我都必须这样做。 是否有任何方法可以将我的数据存储在缓存中,所以我不需要每次都请求。

我正在使用

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

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

【问题讨论】:

  • 例如:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];

标签: ios iphone objective-c web-services caching


【解决方案1】:

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

例如:

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

更多详情请关注Documentation

【讨论】:

    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多