[英]HTTP call runs first time but then waits forever
I'm using SDWebImage to load images asynchronously. 我正在使用SDWebImage异步加载图像。 I want to combine comments and photos in a dictionary like below:
我想将评论和照片组合在如下字典中:
- (NSArray *) fetchPhotos:(NSArray *) requestedPhotoArray
{
NSMutableArray *photos;
UIImageView *imageview;
for (requestedPhoto in requestedPhotoArray) {
[imageview setImageWithURL:[NSURL URLWithString: requestedPhoto.url]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
NSDictionary *parameters = @{ @"function":GET_COMMENT,
@"photoId":requestedPhoto.photoID,
}
NSArray *comments = [[self.communicator sharedInstance] HTTPRequestWithParams:parameters];
Photo *photo = [Photo photoWithDictionary:
@{@"imageView": imageview,
@"comments" : comments,
}];
[photos addObject:photo];
}
return photos;
}
But fetchPhotos function makes one http call and waits forever and then nothing returns. 但是fetchPhotos函数进行一次http调用并永远等待,然后什么也没有返回。
fetchPhotos is called like below (simplified version): fetchPhotos的调用方式如下(简化版):
NSDictionary *parameters = @{@"function":GET_PHOTO_INFO_ARRAY,
@"userid":3,
@"pageid":99,
}
dispatch_async(dispatch_get_global_queue(0, 0), ^{
requestedPhotosInfoArray = [[self.communicator sharedInstance] HTTPRequestWithParams:parameters];
dispatch_async(dispatch_get_main_queue(), ^{
[self fetchPhotos: requestedPhotosInfoArray];
}
communicator:HTTPRequestWithParams do a HTTP request like below communicator:HTTPRequestWithParams发出如下的HTTP请求
...
__block id result;
dispatch_queue_t queue = dispatch_queue_create("my_queue", 0);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async( queue, ^{
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
result = responseObject;
dispatch_semaphore_signal(semaphore);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
result = error;
dispatch_semaphore_signal(semaphore);
}
];
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return result;
Any idea why fetchPhotos only returns first data and waits forever? 知道为什么fetchPhotos只返回第一个数据并永远等待吗?
UPDATE: 更新:
I realised that it waits in 我意识到它在等待
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
then I add a async dispatch queue in fetchPhotos 然后在fetchPhotos中添加一个异步调度队列
for (requestedPhoto in requestedPhotoArray) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[imageview setImageWithURL:[NSURL URLWithString: requestedPhoto.url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; [imageview setImageWithURL:[NSURL URLWithString:requestedPhoto.url] placeholderImage:[UIImage imageNamed:@“ placeholder.png”]];
NSDictionary *parameters = @{ @"function":GET_COMMENT,
@"photoId":requestedPhoto.photoID,
}
NSArray *comments = [[self.communicator sharedInstance] HTTPRequestWithParams:parameters];
....
It's not waiting forever now but it doesn't make a http call. 它不会永远等待,但不会发出http调用。
I used block callback instead of semaphores 我使用了块回调而不是信号量
dispatch_async( queue, ^{
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}
];
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.