研究了3天的block使用,今天终于写出了本身的第一个block接口请求,完整的接口调用和定义, 服务器
首先在interface中用dispatch——queue来处理请求,而后经过block进行回调, async
再在调用的时候处理block回调回来的数据就好了,很是的简洁,代码量减小不少,且效率极高,嘿嘿。 ui
第一步定义要回调的block url
typedef void (^getCommentList)(id data,BOOL succeed); spa
data就是回调时传过去的服务器数据,succeed表示是否成功,固然也能够定义其它参数了第二步用dispatch来建立队列请求数据 线程
dispatch_queue_t queue = dispatch_queue_create("GetCommentList", NULL); orm
dispatch_async(queue, ^(){ 接口
NSURL *url=[NSURL URLWithString:URL_GetComments]; 队列
ASIFormDataRequest *request=[[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; get
[request setPostValue:uid forKey:UnloginView_id];
[request start];
NSMutableArray *content =[[request responseString] JSONValue];
block(content,YES);
});
dispatch_release(queue);
content就是请求回来的数据,通过解析后用block();回调。第三步在须要建立请求的类里面实现这个方法
[MP_InterfaceShare getCommentList:str_mid block:^(id data, BOOL succeed) {
self.arr_objects = (NSMutableArray *)data;
dispatch_async(dispatch_get_main_queue(), ^(){
[self initWithControl];
[self stopTheHoldFire];
});
}];
这里的data就是刚才的content,若是须要处理UI更新的话就使用dispatch_get_main_queue回到主线程。这样就完整了来了一次请求和接受数据。
哇 哦。