【问题标题】:How to Cache NSDATA json in iOS如何在 iOS 中缓存 NSDATA json
【发布时间】:2014-03-15 04:24:24
【问题描述】:

我正在使用此代码仅使用 appID 从 appStore 获取应用程序的应用程序屏幕截图,

    NSString *numericIDStr = appID;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:@"results"];
NSDictionary *result = [results objectAtIndex:0];

myArray = [result objectForKey:@"screenshotUrls"];

我设法使用SDWebImage 缓存了屏幕截图,它运行良好,但我注意到每次调用此方法时它都会延迟大约 3-4 秒,而在 3G 上它会延迟更多,大约 10-15 秒。

以及我的应用在未连接到互联网时崩溃的其他问题,有没有办法缓存这种方法或类似的方法?

【问题讨论】:

    标签: ios cocoa-touch caching nsurl


    【解决方案1】:

    您可以使用以下库在磁盘或内存中保存您的响应(缓存)。

    TMCache

    EGOCache

    【讨论】:

      【解决方案2】:

      使用 AFNetworking。它将需要一行代码并为您进行缓存。

        [yourImage setImageWithURL:yourNSUrl];
      

      就是这样!它为您完成所有缓存。

      【讨论】:

      • 我不想缓存图片,我想缓存 NSURL 请求。
      猜你喜欢
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 2011-12-07
      • 2011-04-12
      • 2019-09-29
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多