一個基于 AFNetworking 2 的靈活好用的 iOS 網絡庫:MGJRequestManager
MGJRequestManager 是什么
MGJRequestManager 是一款基于 AFNetwokring 2.0+ 的 iOS 網絡庫。
一般在做項目時,我們都會在 AFNetworking 的基礎上再封裝一層,加入一些業務邏輯,然后作為統一的 API 層。細分下來的話,功能點其實不多,且相對固定和通用,就索性把這些常用的功能統一處理,并提供靈活的使用方式,于是就有了 MGJRequestManager。
MGJRequestManager 的優勢
Eat your own dog food
MGJRequestManager 目前被用在「蘑菇街」,「小店」等 App 中,已穩定運行了一段時間。
強大
以「蘑菇街」為例,統計這塊會計算請求消耗的時間、每次請求都要發送的參數、據請求參數的值計算 token、緩存請求以便快速呈現等等,MGJRequestManager 都可以比較方便地搞定。
主要功能
- 緩存GET請求(可以方便地開啟/關閉)
- 設置 Builtin 參數(每次請求都會帶上的參數,如設備型號等)
- 符合某種(自定義)條件時,可以不發送請求(比如用戶多次點擊「喜歡」按鈕)
- 對請求結果做預處理(比如將服務端返回的數據包裝成統一的格式)
- 串行發送多個請求(比如 token 過期后,可以將請求新 token 和當前請求串起來)
- 并行發送多個請求,可以告知請求完成數,以及全部請求完成后調用某個 callback
- 發送請求時顯示 loading,發送完成后隱藏 loading
- 上傳圖片
- 取消正在發送的請求
靈活
因為需求經常會變,所以架構需要非常靈活才能方便應對新功能,比如「發送請求時顯示 loading,發送完成后隱藏 loading」這個只是利用了現成的接口實現的一個特性。一些常見的需求都可以通過對 MGJRequestManager 進行配置來實現。
思想
本著「everything should be as simple as possible, but not simpler」的原則,設計這個類庫時,希望使用起來和理解起來盡量方便,同時又足夠靈活,可以應付大多數的場景。
主要分為兩部分MGJRequestManagerConfiguration和MGJRequestManager,前者做一些自定義配置,比如請求發送前/后的預處理,baseURL,userInfo等。后者記錄下這些信息,在適當的時候消費它們,同時支持針對某些特殊請求做自定義配置。
That's It!
安裝
pod 'MGJRequestManager'
演示
使用
以「發送請求時顯示 loading,發送完成后隱藏 loading」為例
// 1 MGJRequestManagerConfiguration *configuration = [[MGJRequestManagerConfiguration alloc] init]; UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; indicatorView.frame = CGRectMake(self.view.frame.size.width / 2 - 8, self.view.frame.size.height / 2 - 8, 16, 16); [self.view addSubview:indicatorView]; // 2 configuration.requestHandler = ^(AFHTTPRequestOperation *operation, id userInfo, BOOL *shouldStopProcessing) { if (userInfo[@"showLoading"]) { [indicatorView startAnimating]; } }; // 3 configuration.responseHandler = ^(AFHTTPRequestOperation *operation, id userInfo, MGJResponse *response, BOOL *shouldStopProcessing) { if (userInfo[@"showLoading"]) { [indicatorView stopAnimating]; } }; [MGJRequestManager sharedInstance].configuration = configuration; // 4 [[MGJRequestManager sharedInstance] GET:@"http://httpbin.org/delay/2" parameters:nil startImmediately:YES // 5 configurationHandler:^(MGJRequestManagerConfiguration *configuration){ configuration.userInfo = @{@"showLoading": @YES}; // 6 } completionHandler:^(NSError *error, id<NSObject> result, BOOL isFromCache, AFHTTPRequestOperation *operation) { [self appendLog:result.description]; // 7 }];
- MGJRequestManager要配合MGJRequestManagerConfiguration才能發揮出威力。這個 configuration 可以配置baseURL,requestHandler,responseHandler等信息,可變部分一般是通過對MGJRequestManagerConfiguration進行配置來實現的。可以全局設置一個 configuration,然后單獨發送請求時還可以覆蓋默認設置。
- requestHandler這個 block 會在請求發送前被觸發,userInfo這個參數時configuration這個實例的一個屬性,方便起見,作為參數傳給了requestHandler。還有一個重要的參數時*shouldStopProcessing,如果在這個 block 里,它被設置為了YES,那么這個請求將不會被發送。
- responseHandler這個 block 會在收到服務端的響應后被觸發,response參數有error和result兩個屬性,服務端返回的數據都被放到了result屬性里,在這里可以對它進行解析,然后重新設置 response。如果*shouldStopProcessing被設置為了YES,那么completionHandler就不會被觸發。
- POST/PUT/DELETE 只需把 GET 換成對應的 HTTP Method 即可。
- startImmediately如果設為NO,那么這個請求不會被發送,可以將來通過調用-[MGJRequestManager startOperation:]來發送,或者也可以放到隊列里。
- 這里會傳入默認的 configuration,如果需要在這個請求做一些個性化設置,可以修改 configuration 里相應地配置。
- 這里的result就是-[MGJResponse result],error是-[MGJResponse error],isFromCache表示這個結果是否來自緩存。