當前位置:首頁 » 文件管理 » ios緩存策略

ios緩存策略

發布時間: 2024-12-27 00:16:29

『壹』 ios開發數據緩存如何獲取服務端最新數據

方法一:一般將伺服器第一次返回的數據保存在沙盒裡面。這樣在手機斷網的情況下可以從本地讀取數據了。

1.保存到沙盒的代碼:

[plain] view
plain

+ (void)saveCache:(int)type andID:(int)_id andString:(NSString *)str;
{
NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
NSString * key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
[setting setObject:str forKey:key];
[setting synchronize];
}

2.讀取本地沙盒的代碼

讀取之前首先根據type和Id判斷本地是否有

[plain] view
plain

+ (NSString *)getCache:(int)type andID:(int)_id
{
NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
NSString *key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];

NSString *value = [settings objectForKey:key];
return value;
}

如果沙盒裡面有數據

[plain] view
plain

NSString *value = [Tool getCache:5 andID:self.QiuTime];
if (value) {
NSDictionary *backdict = [value JSONValue];
if ([backdict objectForKey:@"items"]) {
NSArray *array=[NSArray arrayWithArray:[backdict objectForKey:@"items"]];
for (NSDictionary *qiushi in array) {
QiuShi *qs=[[[QiuShi alloc]initWithDictionary:qiushi] autorelease];
[self.list addObject:qs];
}
}
[self.tableView reloadData];

}

[self.tableView :@"數據全部載入完了.."];
self.tableView.reachedTheEnd = YES;

方法二:使用ASIHTTPRequest和ASIDownloadCache實現本地緩存

1、設置全局的Cache

在AppDelegate.h中添加一個全局變數

[plain] view plain

@interface AppDelegate : UIResponder
{
ASIDownloadCache *myCache;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) ASIDownloadCache *myCache;

在AppDelegate.m中的- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代碼

[plain] view plain

//自定義緩存
ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
self.myCache = cache;
[cache release];

//設置緩存路徑
NSArray *paths = (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
[self.myCache setStoragePath:[documentDirectory :@"resource"]];
[self.myCache setDefaultCachePolicy:];

在AppDelegate.m中的dealloc方法中添加如下語句

[plain] view plain

[myCache release];

到這里為止,就完成了全局變數的聲明。

2、設置緩存策略

在實現ASIHTTPRequest請求的地方設置request的存儲方式,代碼如下

[plain] view plain

NSString *str = @"http://....../getPictureNews.aspx";
NSURL *url = [NSURL URLWithString:str];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//獲取全局變數
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//設置緩存方式
[request setDownloadCache:appDelegate.myCache];
//設置緩存數據存儲策略,這里採取的是如果無更新或無法聯網就讀取緩存數據
[request setCacheStoragePolicy:];
request.delegate = self;
[request startAsynchronous];

3、清理緩存數據

我在這里採用的是手動清理數據的方式,在適當的地方添加如下代碼,我將清理緩存放在了應用的設置模塊:

[plain] view plain

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myCache :];

這里清理的是這種存儲策略的緩存數據,如果更換其他的參數的話,即可清理對應存儲策略的緩存數據。

熱點內容
qq電腦聊天緩存不安全 發布:2025-09-19 03:43:38 瀏覽:361
大話2腳本製作 發布:2025-09-19 03:25:47 瀏覽:497
腳本精靈用的什麼語言 發布:2025-09-19 03:21:32 瀏覽:847
微型機常用的存儲器 發布:2025-09-19 03:18:17 瀏覽:469
迷你世界腳本編輯代碼在哪裡 發布:2025-09-19 03:17:40 瀏覽:374
我的世界110伺服器的天域組織 發布:2025-09-19 02:49:36 瀏覽:797
為什麼安卓手機使用久了會變卡 發布:2025-09-19 02:49:36 瀏覽:876
國家校時伺服器ip 發布:2025-09-19 02:45:18 瀏覽:922
安卓補幀軟體在哪裡下 發布:2025-09-19 02:45:17 瀏覽:33
安卓移機蘋果怎麼操作 發布:2025-09-19 01:58:55 瀏覽:164