【问题标题】:HTTP call runs first time but then waits foreverHTTP 调用第一次运行,然后永远等待
【发布时间】:2014-06-12 10:07:49
【问题描述】:

我正在使用 SDWebImage 异步加载图像。我想在字典中组合 cmets 和照片,如下所示:

- (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;
}

但是 fetchPhotos 函数会进行一次 http 调用并一直等待,然后什么都没有返回。

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 执行如下 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;

知道为什么 fetchPhotos 只返回第一个数据并永远等待吗?

更新:

我意识到它在等待

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

然后我在 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"]];

          NSDictionary *parameters = @{ @"function":GET_COMMENT,
                                        @"photoId":requestedPhoto.photoID,
                                     }  
        NSArray *comments =  [[self.communicator sharedInstance] HTTPRequestWithParams:parameters]; 
    ....

它现在不会永远等待,但它不会进行 http 调用。

【问题讨论】:

  • 我意识到它正在等待dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

标签: ios objective-c afnetworking-2 sdwebimage


【解决方案1】:

我使用块回调而不是信号量

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);
        }

    }
     ];
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多