Forum Xamarin.Android

ListView IsRefreshing UI being blocked by a Task

aga913aga913 Member ✭✭✭
edited January 16 in Xamarin.Android

Hi,
i'm using a Listview to display a list of items in my android app
My listview have Pull To Refresh property enabled
when i pull down the screen the PullToRefresh UI shows as expected after about a second it stops spinning and become in responsive
after following this bug around i had it clear that this is starting when the app reaches a Task...
i'm not sure if i'm not firing the Task correctly or this is how the Pull To Refresh is supposed to behave ?
here is a snippet from the code that i'm using...

    private string Sends(string link)
    {
        Task<HttpResponseMessage> responseTask = client.GetAsync(link);
        string result = string.Empty;
        Task continuation = responseTask.ContinueWith(x =>
        {
            return result = Prints(x);
        });
        continuation.Wait();
        return result;
    }

    private string Prints(Task<HttpResponseMessage> httpTask)
    {
        Task<string> task = httpTask.Result.Content.ReadAsStringAsync();
        string result = string.Empty;
        Task continuation = task.ContinueWith(t =>
        {
            result = t.Result;
        });
        continuation.Wait();
        return result;
    }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    If it is conveniet for you , could you please post a basic demo or some more code snippet so that we can help you better?

  • aga913aga913 Member ✭✭✭
    edited January 19

    @jezh
    sorry for the late respond...
    here is a link for the app i'm creating...

  • jezhjezh Member, Xamarin Team Xamurai
    edited January 20

    I tested your app and found that that the following code runs more than once when I pulled down the Listview.

       while (IsFirstTimeInLoop || Mylisto.Count - (5 * NewsLoadPerPage) < 5)
                    {
                        IsFirstTimeInLoop = false;
    
                        Day1_str = Day_1.Subtract(TimeSpan.FromDays(DateMulti)).ToString("yyyy-MM-dd");
                        Day2_str = Day_2.Subtract(TimeSpan.FromDays(DateMulti)).ToString("yyyy-MM-dd");
    
                        rawData = Sends(string.Format(HTTLink, Day1_str, Day2_str, Cat.ToString()));
    
          // other code
    
        }
    

    I don't understand your code logic, but it doesn't make sense to perform a refresh several times, especially if it's a time-consuming thread operation.You can recheck code:

     while (IsFirstTimeInLoop || Mylisto.Count - (5 * NewsLoadPerPage) < 5){
    
      }
    

    Besides, what's the function of following code,you can try to remove it.

     await Task.Delay(1000);
    
  • aga913aga913 Member ✭✭✭
    edited January 20

    @jezh
    the while loop is to ensure that we have at least 5 news items newly added to the list view
    since the parameters are so bad from the source and i cannot change them
    i move day by day and get the news added to MyListo and if the added news are less than 5 the while loop doesn't break and reorders more news from the next day and so on...
    the bool IsFirstTimeInLoop is to ensure it logs into the loop first time the method is called.
    and the
    await Task.Delay(1000);
    is to let the screen (selected tab) change with full animation before the app starts loading the news therefore lagging...

    the while loop is kinda must so i need to make it in a way it does not block the main thread..

  • jezhjezh Member, Xamarin Team Xamurai

    Hey, friend, sorry so delay to reply due to the long Chinese New Year. :p

    Have you resolved your question?

  • aga913aga913 Member ✭✭✭

    Hi, Hope you had fun :blush:
    Yes i did thanks a bunch for the help
    i changed the code a lot, let me know if you are interested on how it looks like now.

Sign In or Register to comment.