Cursors on Windows Phone

Nov 5, 2013 at 5:33 AM
Hello,
I was wondering if someone could help me implement cursors in a Windows Phone application.
I want to download the friends and followers of the user, and to do so, I must use cursors if the user has over 5000 followers. However, Windows Phone requires the use of async queries, so the next cursor cannot be found until the MaterializedAsyncCallback has completed. Therefore, you cannot create a while loop to implement cursors on Windows Phone like the samples show to do.
Thanks for your help
Coordinator
Nov 5, 2013 at 6:03 PM
Hi,

You'll have to synchronize the code inside the callback with the loop outside that's managing the cursor.

@JoeMayo
Nov 5, 2013 at 9:39 PM
Thank you for the response!
However, I'm not quite sure how to go about doing this. Could you perhaps provide me with a code sample?
Thanks!
Nov 7, 2013 at 1:22 AM
Hello,
I looked into it and I think I came up with a working way to download both friends and followers implementing cursors. However, I don't have an account with 5000 followers to test it on, so could someone run over it and see if it looks like it'll work?
        private void DownloadUsers(Action<bool> finishedAction)
        {
            bool isDone = false;
            bool successful = true;
            friends = new UserIDCollection();
            followers = new UserIDCollection();

            Action<TwitterAsyncResponse<System.Collections.Generic.IEnumerable<SocialGraph>>> friendsAct = resp => { }; //must be set before

            friendsAct = resp =>
            {
                if (resp != null)
                {
                    if (resp.Status == TwitterErrorStatus.Success)
                        friends.AddList(resp.State.FirstOrDefault().IDs.AsUserIDCollection());
                    else
                        successful = false;
                }

                //query again if there is a next cursor
                string nextCursor = resp == null ? "-1" : resp.State.FirstOrDefault().CursorMovement.Next;
                if (!string.IsNullOrEmpty(nextCursor) && nextCursor != "0")
                    (from user in TwitterCtx.SocialGraph
                     where user.Type == SocialGraphType.Friends &&
                     user.UserID == ulong.Parse(ThisUser.Identifier.UserID) &&
                     user.Cursor == nextCursor
                     select user).MaterializedAsyncCallback(friendsAct);
                else //no next cursor
                {
                    if (isDone)
                        Dispatcher.BeginInvoke(() => finishedAction.Invoke(successful));
                    isDone = true;
                }
            };

            Action<TwitterAsyncResponse<System.Collections.Generic.IEnumerable<SocialGraph>>> followersAct = resp => { }; //must be set before or else error

            followersAct = resp =>
            {
                if (resp != null)
                {
                    if (resp.Status == TwitterErrorStatus.Success)
                        followers.AddList(resp.State.FirstOrDefault().IDs.AsUserIDCollection());
                    else
                        successful = false;
                }

                //query again if there is a next cursor
                string nextCursor = resp == null ? "-1" : resp.State.FirstOrDefault().CursorMovement.Next;
                if (!string.IsNullOrEmpty(nextCursor) && nextCursor != "0")
                    (from user in TwitterCtx.SocialGraph
                     where user.Type == SocialGraphType.Followers &&
                     user.UserID == ulong.Parse(ThisUser.Identifier.UserID) &&
                     user.Cursor == nextCursor
                     select user).MaterializedAsyncCallback(followersAct);
                else
                {
                    if (isDone)
                        Dispatcher.BeginInvoke(() => finishedAction.Invoke(successful));
                    isDone = true;
                }
            };

            friendsAct.Invoke(null);
            followersAct.Invoke(null);
        }
Thanks!