0

我正在使用 AFHTTPClient 进行测试以测试后端响应。

__block id testedResponseObject = nil;
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    testedResponseObject = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    testedResponseObject = nil;
}];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(testedResponseObject, @"");

这样做的问题是它等待所有操作完成但它不执行成功块,因为它被调度到 dispatch_get_main_queue()。有没有办法告诉 dispatch_get_main_queue() 从主队列完成它的块?

4

1 回答 1

1

completionBlock您可以直接访问responseData(或任何属性) ,而不是依赖于:

NSURLRequest *request = [client requestWithMethod:@"GET" path:path parameters:nil];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[client enqueuHTTPRequestOperation:operation];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(operation.responseData, @"");
于 2013-05-29T16:25:51.620 回答