User/Lookup Query returning no results on Windows Phone

Oct 21, 2013 at 4:07 AM
Hello,
I'm trying to download the data for all of a user's followers using the User/Lookup query. I first download the list of UserIds, then separate them into strings of 100 UserIds separated by commas to properly use them in the User/Lookup query. I implement it as so:

            if (resp.Status != TwitterErrorStatus.Success)
                MessageBox.Show("error getting social graph");
            else
            {
                //groups into sections of 100 ids
                string[] idLists = resp.State.FirstOrDefault().IDs.Group();

                foreach (string idList in idLists)
                {
                    var result = (from user in AppData.TwitterCtx.User
                                  where user.Type == UserType.Lookup &&
                                  user.UserID == idList
                                  select user).ToList();
                    Dispatcher.BeginInvoke(() =>
                        {
                            result.ForEach((a) =>
                            {
                                Followers.Add(new UserDisplay()
                                {
                                    ScreenName = a.ScreenName,
                                    ActualName = a.Name,
                                    Following = a.Following,
                                    ProfilePictureSource = a.ProfileBackgroundImageUrl
                                });
                                lstFollowers.ItemsSource = Followers;
                            });
                            SystemTray.ProgressIndicator = null;
                        });
                }
            }
where string.Group() is an extension defined as so:

        public static string[] Group(this List<string> data)
        {
            if (data != null)
            {
                string[] value = new string[(data.Count / 100) + 1];
                value[0] = data[0] + ",";
                int index = 0;

                for (int i = 1; i < data.Count; i++)
                {
                    if (i % 100 == 0)
                    {
                        value[index] = value[index].Remove(value[index].Length - 1);
                        index++;
                    }
                    value[index] += data[i] + ",";
                }
                value[value.Count() - 1] = value[value.Count() - 1].Remove(value[value.Count() - 1].Length - 1);

                return value;
            }
            else return null;
        }
When the code is ran, result contains 0 users, and so nothing happens.
Any help is greatly appreciated!
Thanks!
Elliott
Coordinator
Oct 21, 2013 at 6:05 AM
Hi Elliott,

If you're writing a Silverlight, WP, or Windows Store app, you should use MaterialzedAsyncCallback on your queries instead of ToList.

@JoeMayo
Oct 22, 2013 at 12:42 AM
Hello Joe,
I was using MaterializedAsyncCallback, but that didn't work either, so I figured I'd try ToList.
When using MaterializedAsyncCallback, I implemented it as so:
                    foreach (string idList in idLists)
                    {
                        (from user in AppData.TwitterCtx.User
                         where user.Type == UserType.Lookup &&
                         user.UserID == idList
                         select user).MaterializedAsyncCallback(UsersLoaded);
                    }
where UsersLoaded is defined as
        private void UsersLoaded(TwitterAsyncResponse<IEnumerable<User>> resp)
        {
            if (resp.Status != TwitterErrorStatus.Success)
                MessageBox.Show("error getting users");
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    foreach (User userData in resp.State)
                    {
                        Followers.Add(new UserData()
                        {
                            ScreenName = userData.ScreenName,
                            ActualName = userData.Name,
                            Following = userData.Following,
                            ProfilePictureSourceString = userData.ProfileBackgroundImageUrl
                        });
                        lstFollowers.ItemsSource = Followers;
                        HideLoading(); //hides progress bar
                    }
                });
            }
        }
When a breakpoint is inserted in the first line of code in UsersLoaded, it is never reached. The information is queried, but no response ever returns. So I tried ToList, which didn't throw an exception as it usually does, so I continued using it. However, neither method works.
Do you have any suggestions or possible solutions?
Thanks!
Elliott
Coordinator
Oct 22, 2013 at 4:18 AM
You might want to use a separate TwitterContext instance for each iteration to eliminate the opportunity for race conditions between multiple threads on the same instance. You can use the same authorizer, since it doesn't change state once it's initialized.

I'm currently working on a PCL version of LINQ to Twitter that uses C# 5.0 async. It's much simpler. I'm still working on it, but you're welcome to download the source code and try it out.

@JoeMayo
Oct 22, 2013 at 5:57 AM
Hello Joe,
Thank you for your help! That fixed it!
Elliott