相关疑难解决方法(0)

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

我有一个使用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.
}
Run Code Online (Sandbox Code Playgroud)

synchronous ios objective-c-blocks afnetworking

46
推荐指数
4
解决办法
4万
查看次数