异步缓存json

发布于 2024-11-09 20:38:54 字数 118 浏览 1 评论 0原文

我正在开发一个 iOS 应用程序,它从我的服务器的 json feed 获取数据。解析字符串没有问题...但是,我在异步下载字符串然后缓存它时遇到了问题。我找到了SDURLCache,但不知道如何实现它。最好的方法是什么?

I'm developing an iOS app which gets its data from a json feed of my server. Parsing the string is no problem... However, I am having trouble downloading the string asynchronously and then caching it. I found SDURLCache but do not know how to implement it. What is the best way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-11-16 20:38:54

您可以将文件下载到磁盘(同步):

NSURL * url = [NSURL URLWithString:@"www.yourprovider.com/your.json";
NSData *file = [NSData dataWithContentsOfURL:url];
[file writeToFile:<your file path> atomically:YES];

对于异步操作,您应该使用 NSURlConnection。打开连接后,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:<your NSURL>];

    [request setHTTPMethod:@"GET"];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];

您会在此回调中收到数据:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {

 //here you could write to a NSFileHandle ivar:
 if (file)  { 

        [file seekToEndOfFile];

    } [file writeData:_data];
}

You could download the file to disk (synchronously):

NSURL * url = [NSURL URLWithString:@"www.yourprovider.com/your.json";
NSData *file = [NSData dataWithContentsOfURL:url];
[file writeToFile:<your file path> atomically:YES];

For asynchronous operations, instead, you should use NSURlConnection. After opening the connection,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:<your NSURL>];

    [request setHTTPMethod:@"GET"];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];

you receive data in this call back:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {

 //here you could write to a NSFileHandle ivar:
 if (file)  { 

        [file seekToEndOfFile];

    } [file writeData:_data];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文