简体   繁体   中英

HTTP call runs first time but then waits forever

I'm using SDWebImage to load images asynchronously. 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 is called like below (simplified version):

  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

...
__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?

UPDATE:

I realised that it waits in

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

then I add a async dispatch queue in 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]; 
    ....

It's not waiting forever now but it doesn't make a http call.

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

    }
     ];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM