为了账号安全,请及时绑定邮箱和手机立即绑定

AFNetworking可以同步(在块内)返回数据吗?

AFNetworking可以同步(在块内)返回数据吗?

iOS
月关宝盒 2019-10-09 10:27:06
我有一个使用AFJSONRequestOperation的函数,我希望仅在成功后返回结果。你能指出我正确的方向吗?我对块和AFNetworking还是一无所知。-(id)someFunction{    __block id data;    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json){            data = json;            return data; // won't work        }        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){        }];    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    [queue addOperation: operation];    return data; // will return nil since the block doesn't "lock" the app.}
查看完整描述

3 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

要在操作完成之前阻止主线程的执行,可以在将主线程[operation waitUntilFinished]添加到操作队列之后执行。在这种情况下,您不需要return在代码块中;设置__block变量就足够了。

也就是说,我强烈建议不要将异步操作强制为同步方法。有时候动不动就很棘手,但是如果有任何方法可以将其构造为异步的,那几乎肯定是要走的路。


查看完整回答
反对 回复 2019-10-09
?
噜噜哒

TA贡献1784条经验 获得超7个赞

我正在使用信号量来解决此问题。此代码在继承自的我自己的类中实现AFHTTPClient。


__block id result = nil;

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSURLRequest *req = [self requestWithMethod:@"GET"

                                       path:@"someURL"

                                 parameters:nil];

AFHTTPRequestOperation *reqOp = [self HTTPRequestOperationWithRequest:req

                                                              success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                                                  result = responseObject;                                                                          

                                                                  dispatch_semaphore_signal(semaphore);

                                                              }

                                                              failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                                                  dispatch_semaphore_signal(semaphore);

                                                              }];

reqOp.failureCallbackQueue = queue;

reqOp.successCallbackQueue = queue;

[self enqueueHTTPRequestOperation:reqOp];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

dispatch_release(semaphore);

return result;


查看完整回答
反对 回复 2019-10-09
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

我建议您不要使用AFNetworking(或通常使用块)来创建同步方法。一个好的方法是您使用另一种方法,并将成功块中的json数据用作参数。


- (void)methodUsingJsonFromSuccessBlock:(id)json {

    // use the json

    NSLog(@"json from the block : %@", json); 

}


- (void)someFunction {

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json){

            // use the json not as return data, but pass it along to another method as an argument

            [self methodUsingJsonFromSuccessBlock:json];

        }

        failure:nil];


    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [queue addOperation: operation];

}


查看完整回答
反对 回复 2019-10-09
  • 3 回答
  • 0 关注
  • 381 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信