Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSURLRequest未使用AFNetworking在磁盘上缓存_Ios_Json_Caching_Afnetworking - Fatal编程技术网

Ios NSURLRequest未使用AFNetworking在磁盘上缓存

Ios NSURLRequest未使用AFNetworking在磁盘上缓存,ios,json,caching,afnetworking,Ios,Json,Caching,Afnetworking,我正在开发一个通过json从web下载数据的应用程序。我将缓存策略设置为使用缓存,正如我在教程和帖子中看到的那样。当应用程序启动时,它会下载一次数据,如果我关闭视图并在飞行模式下重新打开,数据就会从缓存中加载。但是,如果我关闭应用程序,然后重新启动它,请不要从缓存加载并再次请求服务器(或者在飞行模式打开时显示错误)。我需要在磁盘上缓存数据以供脱机使用。。。请帮忙 这是我的密码: AppDelegate.m NSURLCache *sharedCache = [[NSURLCache alloc]

我正在开发一个通过json从web下载数据的应用程序。我将缓存策略设置为使用缓存,正如我在教程和帖子中看到的那样。当应用程序启动时,它会下载一次数据,如果我关闭视图并在飞行模式下重新打开,数据就会从缓存中加载。但是,如果我关闭应用程序,然后重新启动它,请不要从缓存加载并再次请求服务器(或者在飞行模式打开时显示错误)。我需要在磁盘上缓存数据以供脱机使用。。。请帮忙

这是我的密码:

AppDelegate.m

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024
                                                        diskCapacity:100 * 1024 * 1024
                                                            diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
以及请求:

NSString *string = @"http://********.es/api/get_posts/?post_type=listing";
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
    NSCachedURLResponse * newCachedResponse = [[NSCachedURLResponse alloc]
                                               initWithResponse:cachedResponse.response
                                               data:cachedResponse.data
                                               userInfo:cachedResponse.userInfo
                                               storagePolicy:NSURLCacheStorageAllowed];

    return newCachedResponse;
}];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


    _lista = responseObject[@"posts"];
    [self.tableView reloadData];
    //NSLog(@"%@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Data"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}];


[operation start];

我在网上尝试了很多解决方案,但没有人能像我想的那样工作,所以我认为问题出在我的代码中

我认为您需要的功能将需要使用核心数据或使用NSFileManager编写自己的缓存。找出应用程序的最佳方法。马特·汤普森(Mattt Thompson)有一篇关于NSFileManager的非常酷的文章,那么ios系统缓存不工作了?我找不到一个明确的答案来选择一条路,非常感谢,最后,我尝试了这个例子,效果很好