以下使用 AFNetworking 2.0 的代码可有效通过 Internet 获取数据:
NSString *URLPath = @"http://www.raywenderlich.com/downloads/weather_sample/weather.php?format=json";
NSDictionary *parameters = nil;
[[AFHTTPRequestOperationManager manager] GET:URLPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure: %@", error);
}];
但我想在单元测试中同步测试这些请求。但是当像这样使用 GCD 信号量时它会被阻止:
// This code would be blocked.
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSString *URLPath = @"http://www.raywenderlich.com/downloads/weather_sample/weather.php?format=json";
NSDictionary *parameters = nil;
[[AFHTTPRequestOperationManager manager] GET:URLPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", responseObject);
dispatch_semaphore_signal(sema);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure: %@", error);
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release_ARC_compatible(sema);
如何使用 AFNetworking 2.0 库同步获取数据(在Kiwi中测试这些代码)?