Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios HTTP调用第一次运行,但会一直等待_Ios_Objective C_Afnetworking 2_Sdwebimage - Fatal编程技术网

Ios HTTP调用第一次运行,但会一直等待

Ios HTTP调用第一次运行,但会一直等待,ios,objective-c,afnetworking-2,sdwebimage,Ios,Objective C,Afnetworking 2,Sdwebimage,我使用SDWebImage异步加载图像。我想将评论和照片合并到字典中,如下所示: - (NSArray *) fetchPhotos:(NSArray *) requestedPhotoArray { NSMutableArray *photos; UIImageView *imageview; for (requestedPhoto in requestedPhotoArray) { [imageview setImageWithURL:[NSURL

我使用SDWebImage异步加载图像。我想将评论和照片合并到字典中,如下所示:

- (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的名称如下(简化版):

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] 占位符图像:[UIImage ImageName:@“placeholder.png”]


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

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

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

    }
     ];
});

我意识到它在
dispatch\u semaphore\u wait中等待(semaphore,dispatch\u TIME\u ever)
          NSDictionary *parameters = @{ @"function":GET_COMMENT,
                                        @"photoId":requestedPhoto.photoID,
                                     }  
        NSArray *comments =  [[self.communicator sharedInstance] HTTPRequestWithParams:parameters]; 
    ....
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);
        }

    }
     ];
});