0
我有一個UITableView有兩個部分,其中每個部分需要調用REST API的數據。我正在使用MKNetworkKit進行通話。我的問題是,我應該如何爲填充UITableView的NSMutableArray,以確保數據在任何時候都是正確的,即使在「Pull to refresh」之後。我只是覺得我對這一切都是錯誤的。鏈接MKNetworkKit API調用,並與如何填充與數據的NSMutableArray的問題
這是我的代碼,截至目前:
MKNetworkEngine子類(.H):
typedef void (^DualResponseBlock)(id ResponseJson, NSError *);
-(MKNetworkOperation*) RequestWithURI:(NSString *) URI
withHandler:(DualResponseBlock)ResponseBlock;
MKNetworkEngine子類(.M):
-(MKNetworkOperation *)RequestWithURI:(NSString *)URI
withHandler:(DualResponseBlock)ResponseBlock {
MKNetworkOperation *op = [self operationWithPath:HubAPI(URI)
params:nil
httpMethod:@"GET"];
if(![self isReachable]) {
DLog(@"Unable to connect to %@ - Reachability is %d", HubAPI(URI), [self isReachable]);
}
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
[completedOperation responseJSONWithCompletionHandler:^(id jsonObject) {
ResponseBlock(jsonObject, nil);
}];
} errorHandler:^(MKNetworkOperation *errorOp, NSError *error) {
[errorOp responseJSONWithCompletionHandler:^(id jsonObject) {
NSMutableDictionary *errorDetails = [NSMutableDictionary dictionary];
NSDictionary *errorResponse = [jsonObject objectForKey:@"error"];
[errorDetails setValue:[errorResponse objectForKey:@"message"] forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:kNSErrorDomain code:[[errorResponse objectForKey:@"code"] intValue] userInfo:errorDetails];
ResponseBlock(nil, error);
}];
}];
[self enqueueOperation:op];
return op;
}
視圖控制器具有的UITableView(.H ):
@interface WishlistViewController : UITableViewController <NSObject, UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate> {
NSMutableArray *wishlist;
}
@property (nonatomic, retain) IBOutlet UITableView *wishlistTableView;
視圖控制器具有的UITableView( - (無效)viewDidLoad中)(.M):
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[app.hubEngine RequestWithURI:@"wishlist" withHandler:^(id responseJson, NSError *responseError) {
if(responseJson != nil) {
wishlist = [[NSMutableArray alloc] initWithObjects:[responseJson mutableCopy], nil];
[app.hubEngine RequestWithURI:@"wishlist/granted" withHandler:^(id responseJson, NSError *responseError) {
if(responseJson != nil) {
[wishlist addObject:[responseJson mutableCopy]];
[_wishlistTableView reloadData];
}
else {
DLog(@"Error: %@", [responseError localizedDescription]);
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
else {
DLog(@"Error: %@", [responseError localizedDescription]);
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
}];