Load offline cached JSON using AFNetworking

前端 未结 1 1973
眼角桃花
眼角桃花 2021-01-16 18:22

I\'ve been trying to implement caching using AFNetworking so my cached JSON will be loaded offline. i tried my best in different ways i couldn\'t make the cache work at all.

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 18:39

    Man, i don't know swift so well but as long as i know you will have to do is:

    first of all, capture the returned data from your get and save then into NSCachedURLResponse. in objective-c will be something like that:

    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:operation.response data:operation.responseData];
        [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:urlRequest];
    

    After that, if your requisition throws error, you will recuperate you previous data saved for that URL.

    NSData *data = [[[NSURLCache sharedURLCache] cachedResponseForRequest:urlRequest] data];
    

    I hope that helps you.

    0 讨论(0)
提交回复
热议问题