gpt4 book ai didi

ios - 如何在 objective-c 中使用缓存内存概念

转载 作者:行者123 更新时间:2023-11-29 13:00:20 24 4
gpt4 key购买 nike

//网络服务请求

        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 *) 连接这个方法谢谢...

最佳答案

根据这个documentation需要根据需要使用缓存: 每当你请求一个 NSMutableURLRequest...

例如:

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

更多详情请查看Documentation

关于ios - 如何在 objective-c 中使用缓存内存概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19926831/

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