0

我现在正在忍受它几个小时。

我正在尝试使用for iOS从Facebook API检索数据。Facebook SDK 3.5当我使用该方法发出请求时,startWithCompletionHandler()它似乎没有调度新线程,因为它等待整个函数结束,然后才执行块内容。所以我无法读取班级其他区域的 userData 内容。

我尝试过使用信号量,但它似乎总是陷入僵局。

userData是全局的。

FBRequest *request = [FBRequest requestForMe];        
    // Send request to Facebook
    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            // result is a dictionary with the user's Facebook data
            userData = (NSDictionary *)result;

            NSLog(@"Block return: %@",userData[@"id"]);

        } else{
            NSLog(@"Facebook Initialization error: %@", error);
        }
    }
     ];
4

3 回答 3

0

尝试这个:

[[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
    if (error) {
    }
    else {

        [self StopSpinner];

        txtID.text = user.id;
        txtUserName.text = user.username;
        txtName.text = user.name;
        txtEmail.text = [user objectForKey:@"email"];
    }
}];
于 2013-06-29T06:59:12.637 回答
-1
FBRequest *request = [FBRequest requestForMe];        
    // Send request to Facebook
    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            // result is a dictionary with the user's Facebook data
            userData = (NSDictionary *)result;
            // do what you want to do or call any other methods.    



        } else{
            NSLog(@"Facebook Initialization error: %@", error);
        }
    }
     ];

我不确定还有其他方法可以做到这一点。

于 2013-06-29T13:16:23.650 回答
-3
FBRequest *request = [FBRequest requestForMe];        
    // Send request to Facebook
    [request startWithCompletionHandler:^(FBRequestConnection *connection,  NSDictionary * result, NSError *error) {
        if (!error) {
            // result is a dictionary with the user's Facebook data
            userData = result;

            NSLog(@"Block result: %@",[userData objectForkey:@"Your key"]);

        } else{
            NSLog(@"Facebook Initialization error: %@", error);
        }
    }
     ];

代替 id 结果,您可以使用 NSDictionary * 结果并根据您的字典键获取块结果,确保您的 userData 字典已分配。

于 2013-06-29T04:29:39.983 回答