AFNetworking 配合NSURLCache 本地缓存

AFNetworking, NSURLCache & iOS 6

NSURLCache can be used to define memory and disk caching for URL requests. If you make a connection while you are not connected to the internet with the cache policy set to NSURLRequestReturnCacheDataElseLoad, data should be returned from the cache if it exists. This works as expected on iOS 5 and cached data is always returned. On iOS 6.x, however, this doesn’t happen and the connection will fail returning an error code -1009 which indicates that you are offline. I ran into this recently while using AFNetworking and apparently this is a bug in iOS 6.x and has already been reported to Apple by multiple people.

To workaround this, I wrote a simple class that subclasses AFNetworking‘s AFHTTPClient (thanks tothis GitHub issue) and ensures that you always get cached data when it’s available regardless of whether you are connected to the internet or not.

What it does is that it gets the NSCachedURLResponse in the failure block of the request you are making and deserializes it using NSJSONSerialization, then calls the success block with the responseObject as if the connection didn’t fail.

I’ve added the code to a GitHub gist. Please fork it if you think there’s something that could be improved.

123456789101112
          
          
//
// HMHTTPClient.h
//
// Created by Hesham Abd-Elmegid on 4/10/13.
// Copyright (c) 2013 Startappz. All rights reserved.
//
 
#import "AFHTTPClient.h"
 
@interface HMHTTPClient : AFHTTPClient
 
@end
view raw HMHTTPClient.h hosted with ❤ by  GitHub
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
          
          
//
// HMHTTPClient.m
//
// Created by Hesham Abd-Elmegid on 4/10/13.
// Copyright (c) 2013 Startappz. All rights reserved.
//
 
#import "HMHTTPClient.h"
 
@implementation HMHTTPClient
 
- ( void ) getPath: ( NSString * ) path
parameters: ( NSDictionary * ) parameters
success: ( void ( ^ )( AFHTTPRequestOperation * operation , id responseObject )) success
failure: ( void ( ^ )( AFHTTPRequestOperation * operation , NSError * error )) failure
{
NSMutableURLRequest * request = [ self requestWithMethod : @"GET" path : path parameters : parameters ];
[ request setCachePolicy : NSURLRequestReturnCacheDataElseLoad ];
AFHTTPRequestOperation * operation = [ self HTTPRequestOperationWithRequest : request success : success failure : failure ];
[ self enqueueHTTPRequestOperation : operation ];
}
 
- ( void ) postPath: ( NSString * ) path
parameters: ( NSDictionary * ) parameters
success: ( void ( ^ )( AFHTTPRequestOperation * operation , id responseObject )) success
failure: ( void ( ^ )( AFHTTPRequestOperation * operation , NSError * error )) failure
{
NSMutableURLRequest * request = [ self requestWithMethod : @"POST" path : path parameters : parameters ];
[ request setCachePolicy : NSURLRequestReturnCacheDataElseLoad ];
AFHTTPRequestOperation * operation = [ self HTTPRequestOperationWithRequest : request success : success failure : failure ];
[ self enqueueHTTPRequestOperation : operation ];
}
 
- ( void ) putPath: ( NSString * ) path
parameters: ( NSDictionary * ) parameters
success: ( void ( ^ )( AFHTTPRequestOperation * operation , id responseObject )) success
failure: ( void ( ^ )( AFHTTPRequestOperation * operation , NSError * error )) failure
{
NSMutableURLRequest * request = [ self requestWithMethod : @"PUT" path : path parameters : parameters ];
[ request setCachePolicy : NSURLRequestReturnCacheDataElseLoad ];
AFHTTPRequestOperation * operation = [ self HTTPRequestOperationWithRequest : request success : success failure : failure ];
[ self enqueueHTTPRequestOperation : operation ];
}
 
- ( void ) deletePath: ( NSString * ) path
parameters: ( NSDictionary * ) parameters
success: ( void ( ^ )( AFHTTPRequestOperation * operation , id responseObject )) success
failure: ( void ( ^ )( AFHTTPRequestOperation * operation , NSError * error )) failure
{
NSMutableURLRequest * request = [ self requestWithMethod : @"DELETE" path : path parameters : parameters ];
[ request setCachePolicy : NSURLRequestReturnCacheDataElseLoad ];
AFHTTPRequestOperation * operation = [ self HTTPRequestOperationWithRequest : request success : success failure : failure ];
[ self enqueueHTTPRequestOperation : operation ];
}
 
- ( AFHTTPRequestOperation * ) HTTPRequestOperationWithRequest: ( NSURLRequest * ) urlRequest
success: ( void ( ^ )( AFHTTPRequestOperation * operation , id responseObject )) success
failure: ( void ( ^ )( AFHTTPRequestOperation * operation , NSError * error )) failure
{
AFHTTPRequestOperation * operation = [ super HTTPRequestOperationWithRequest : urlRequest success : success failure :^ ( AFHTTPRequestOperation * operation , NSError * error ) {
if ( error . code == kCFURLErrorNotConnectedToInternet ) {
NSCachedURLResponse * cachedResponse = [[ NSURLCache sharedURLCache ] cachedResponseForRequest : urlRequest ];
if ( cachedResponse != nil && [[ cachedResponse data ] length ] > 0 ) {
id JSON = [ NSJSONSerialization JSONObjectWithData : cachedResponse . data options : 0 error :& error ];
success ( operation , JSON );
} else {
failure ( operation , error );
}
} else {
failure ( operation , error );
}
}];
return operation ;
}
 
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cesium提供了本地缓存的功能,可以将地图数据缓存在本地,以便在没有网络连接时仍然能够加载和显示地图。这样可以提高地图加载速度和用户体验。 在Cesium中,本地缓存是通过使用Cesium ion服务来实现的。Cesium ion是一个基于云的平台,用于处理、优化和分发地理空间数据。通过Cesium ion,您可以将地图数据上传到云端,并生成一个唯一的资源ID。然后,您可以使用这个资源ID来访问和加载地图数据。 要使用本地缓存功能,您需要按照以下步骤进行操作: 1. 注册并登录Cesium ion账号。 2. 在Cesium ion平台上创建一个新的数据集,并将您的地图数据上传到该数据集中。 3. 获取数据集的资源ID。 4. 在Cesium的代码中,使用资源ID来加载地图数据,并设置本地缓存的相关参数。 以下是一个示例代码,演示如何在Cesium中使用本地缓存功能: ```javascript // 引入Cesium库 import * as Cesium from 'cesium'; // 创建Cesium Viewer const viewer = new Cesium.Viewer('cesiumContainer'); // 创建本地缓存配置 const cacheConfig = new Cesium.LocalCacheConfig({ enabled: true, // 启用本地缓存 directory: 'path/to/cache/directory', // 设置本地缓存目录 maximumCacheSize: 1024 * 1024 * 1024 // 设置最大缓存大小 }); // 设置本地缓存配置 viewer.scene.globe.localCacheConfig = cacheConfig; // 加载地图数据 const tileset = new Cesium.Cesium3DTileset({ url: 'https://api.cesium.com/v1/assets/your-resource-id/tileset.json' }); viewer.scene.primitives.add(tileset); ``` 请注意,上述代码中的`your-resource-id`应替换为您在Cesium ion平台上创建的数据集的资源ID。 通过以上步骤,您就可以在Cesium中使用本地缓存功能来加载和显示地图数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值