Anh thấy chú có install AFNetworking rồi tạo môt cái APIEngine kế thừa AFHTTPRequestOperationManager
sử dụng shareIntance tiếp nhé. Ở đây sẽ implement các function request API.
Thêm một file
Blockconfig quản lý các block định nghĩa cho anh 2 block:
typedef void (^ResultWithCacheBlock)(id object, BOOL isCache);
typedef void (^ResultErrorBlock)(NSError* error);
APIConfig và define API URL:
Trong APIEngine: chú define một request, request này sẽ là base cho tất cả các request sau này (cả post/ get/ nếu cần làm thêm các method khác nữa nhưng hiện tại cứ làm 2 cái post/get thôi). Config HEADER với content-type: application/json
Sau khi làm xong cái APIEngine tạo một category của nó implement function
-(void)getTimelineABCXYZ:(ResultWithCacheBlock)completeBlock
onError:(ResultErrorBlock)onError
Cache lại API sau khi request. Chỉ cache lại API mới nhất. Định danh theo URL & param (Nghĩa lại mỗi curl đều được cache lại) Khi request mỗi API thì check trong cache (theo url & param) nếu có cache thi trả về cache trước sau đó request success thì trả về data mới trong ResultWithCacheBlock với key isCache còn sau này show data lên list thì tính sau :3
define API_ROOT_URL @"http://tienpm.pythonanywhere.com"
define API_ROOT_URL @"/mathoiden/api/getNewsFeed"
Trong APIEngine: chú define một request, request này sẽ là base cho tất cả các request sau này (cả post/ get/ nếu cần làm thêm các method khác nữa nhưng hiện tại cứ làm 2 cái post/get thôi). Config HEADER với content-type: application/json
Sau khi làm xong cái APIEngine tạo một category của nó implement function -(void)getTimelineABCXYZ:(ResultWithCacheBlock)completeBlock onError:(ResultErrorBlock)onError