iOS多線程異步處理數據

jopen 9年前發布 | 12K 次閱讀 IOS iOS開發 移動開發

NXOperationManager

多線程異步處理數據

FileCacheManager使用說明

  • 文件數據少則幾十k大則上百兆,文件緩存處理不好嚴重影響用戶體驗
  • FileCacheManager能夠異步處理多個文件而不影響界面交互

FileCacheManager

  • FileCacheManager
  • FileCacheOperation
  • FileCacheOperationManager

建立FileCacheModel對象

NSString * content = @"定風波 蘇軾\n(三月七日沙湖道中遇雨。雨具先去,同行皆狼狽,余獨不覺。已而遂晴,故作此 )。\n莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰怕? 一蓑煙雨任平生。\n料峭春風吹酒醒,微冷,山頭斜照卻相迎。回首向來蕭瑟處,歸去,也無風雨也無晴。";
FileCacheModel * fileCacheModel = [[FileCacheModel alloc] init];
fileCacheModel.fileName = @"定風波.蘇軾";
fileCacheModel.fileData = [content dataUsingEncoding:NSUTF8StringEncoding];
fileCacheModel.fileType = @"txt";

注:文件名、數據不能為空

建立FileCacheOperationManager對象

FileCacheOperationManager * fileCacheOperationManager =[[FileCacheOperationManager alloc] initWithSearchPathModel:NXSearchPathModelCache rootFilePath:@"宋詞.蘇軾"];
[fileCacheOperationManager cacheFileWith:fileCacheModel success:^(FileCacheOperation *operation, NSString *filePath) {
    NSLog(@"%@",filePath);
    dispatch_async(dispatch_get_main_queue(), ^{
        _filePathLabel.text = filePath;
    });
} failure:^(FileCacheOperation *operation, NSString *errorInfo) {
    NSLog(@"%@",errorInfo);
    dispatch_async(dispatch_get_main_queue(), ^{
        _filePathLabel.text = errorInfo;
    });
}];

注:可以進行二次封裝,類似于AFNetworking;rootFilePath是存儲目錄路徑,多層次包含用‘.’隔開,例如本例子中‘宋詞.蘇軾’轉化為存儲目錄為/宋詞/蘇軾

二次封裝說明

  • SongCiDataCacheConfiguration:來封裝存儲模式、根目錄
  • SongCiCacheOperationManager:繼承于FileCacheOperationManager,單例
    注:這里用一個例子來說明二次封裝
[[SongCiCacheOperationManager sharedSongciCacheOperationManager] cacheFileWith:fileCacheModel success:^(FileCacheOperation *operation, NSString *filePath) {
    NSLog(@"二次封裝----%@",filePath);
} failure:^(FileCacheOperation *operation, NSString *errorInfo) {
    NSLog(@"二次封裝----%@",errorInfo);
}];

項目主頁:http://www.baiduhome.net/lib/view/home/1449728211301

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!