必须从主线程调用Facebook GraphRequest.Start()吗?

时间:2015-10-03 13:07:25

标签: c# facebook xamarin async-await facebook-ios-sdk

我正在尝试按照此处提供的入门示例:https://components.xamarin.com/gettingstarted/facebookios

以下GraphRequest在主线程中执行时工作正常:

var request = new GraphRequest ("/me?fields=name", null, AccessToken.CurrentAccessToken.TokenString, null, "GET");
request.Start ((connection, result, error) => {
    // Handle if something went wrong with the request
    if (error != null) {
        new UIAlertView ("Error...", error.Description, null, "Ok", null).Show ();
        return;
    }

    // Get your profile name
    var userInfo = result as NSDictionary;
    nameLabel.Text = userInfo ["name"].ToString ();
});

但是,当我使用 Task.Run 从后台线程运行完全相同的代码时,似乎永远不会调用 request.Start 中的完成处理程序委托。 / p>

GraphRequest是否仅支持从主线程执行?

1 个答案:

答案 0 :(得分:4)

您可以使用:

       static void Main(string[] args)
        {
            Dictionary<int, string> test = new Dictionary<int, string>();
            test.Add(1, "1");
            test.Add(3, "3");
            test.Add(4, "4");
            test.Add(5, "5");
            test.Add(2, "2");
            JoinTest(test);

        }

        private static void JoinTest(Dictionary<int, string> test)
        {
            var keys = string.Join(",", test.Keys);
            var values = string.Join(",", test.Values);

        }