在这里,我尝试在 GCD 中调用我的 ASIHTTP 请求。但是完成块和失败块没有执行
在这里,我尝试在 GCD 中调用我的 ASIHTTPRequest
。有时,完成块和失败块不会执行。我想要做的是,在这个请求完成后,我必须在另一个 ASIHTTPRequest
中使用返回的数据。那么如何改进这段代码:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setCompletionBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *_responseData = [request responseData];
NSString *response = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding] ;
self.albumDic = [response JSONValue];
[response release];
dispatch_async(dispatch_get_main_queue(), ^{
[self GetDictionary:self.albumDic];
});
});
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error : %@", error.localizedDescription);
}];
[request startSynchronous];
Here i am trying to call my ASIHTTPRequest
in a GCD. Sometimes the comletion blocks and failed blocks are not executing. What i want to do is, after this request finished, i have to use the returned data in a another ASIHTTPRequest
. So how to improve this code:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setCompletionBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *_responseData = [request responseData];
NSString *response = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding] ;
self.albumDic = [response JSONValue];
[response release];
dispatch_async(dispatch_get_main_queue(), ^{
[self GetDictionary:self.albumDic];
});
});
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error : %@", error.localizedDescription);
}];
[request startSynchronous];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
别走那条路。您正在线程上进行线程(GCD 使用线程,异步使用时 ASIHTTPRequest 也使用线程)。
请改用 ASINetworkQueue - 在此处阅读有关它的内容,
这是您可以使用它的简单方法:
ASINetworkQueue 提供了许多委托方法(大多数也是可自定义的),因此您可以在请求完成时更新 GUI 等。它是异步的,因此不需要 GCD。
Don't go that way. You're doing threading on threading (GCD uses threading and so does ASIHTTPRequest when used asynchronously).
Use ASINetworkQueue instead - read about it here
Here is a simple way you could use it:
ASINetworkQueue provides many delegate methods (most are also customizable), so you can update the GUI when a request is finished and so forth. It is asynchronous, so GCD is unnecessary.