Forum Xamarin Xamarin.Forms

ForceUpdateSize slows UI

PekkachuPekkachu USMember ✭✭
edited March 2018 in Xamarin.Forms

Hi!

I have this strange issue, i have a ViewCell inside a ListView that expands when you tap inside, calling cell.ForceUpdateSize() , this works OK, but if you it repeated times (say, like 3 or 4 times) it starts to get slower every time, until it stops working (freezes)

 void Cell_OnTapped(object sender)
        {
            var cell = sender as InventoryCell;
            cell.Height = cell.Height == 40 ? cell.Height + 30 : cell.Height = 40;
            cell.ForceUpdateSize();

        }

Any info about it?... btw, using XF on iOS

Posts

  • JohnHJohnH GBMember ✭✭✭✭✭
    edited April 2018

    @Pekkachu said:
    Hi!

    I have this strange issue, i have a ViewCell inside a ListView that expands when you tap inside, calling cell.ForceUpdateSize() , this works OK, but if you it repeated times (say, like 3 or 4 times) it starts to get slower every time, until it stops working (freezes)

     void Cell_OnTapped(object sender)
            {
                var cell = sender as InventoryCell;
                cell.Height = cell.Height == 40 ? cell.Height + 30 : cell.Height = 40;
                cell.ForceUpdateSize();
                
            }
    

    Any info about it?... btw, using XF on iOS

    Interesting, this is the first time I have seen this issue. I have a grouped list view with cells that can expand/collapse when tapped, Im seeing the same behaviour. XF 2.5.1.444934. I haven't found a way to get it to work successfully, how about you?

    [EDIT] By 'work successfully' I mean the ForceUpdateSize can only be triggered 3 or 4 times before slowing down and locking the UI, exactly the same symptom as @Pekkachu

  • PekkachuPekkachu USMember ✭✭

    @JohnHair said:

    @Pekkachu said:
    Hi!

    I have this strange issue, i have a ViewCell inside a ListView that expands when you tap inside, calling cell.ForceUpdateSize() , this works OK, but if you it repeated times (say, like 3 or 4 times) it starts to get slower every time, until it stops working (freezes)

     void Cell_OnTapped(object sender)
            {
                var cell = sender as InventoryCell;
                cell.Height = cell.Height == 40 ? cell.Height + 30 : cell.Height = 40;
                cell.ForceUpdateSize();
                
            }
    

    Any info about it?... btw, using XF on iOS

    Interesting, this is the first time I have seen this issue. I have a grouped list view with cells that can expand/collapse when tapped, Im seeing the same behaviour. XF 2.5.1.444934. I haven't found a way to get it to work successfully, how about you?

    [EDIT] By 'work successfully' I mean the ForceUpdateSize can only be triggered 3 or 4 times before slowing down and locking the UI, exactly the same symptom as @Pekkachu

    I'm still struggling...

    In Android it works flawlessly, the problem occurs just in iOS

  • JohnHJohnH GBMember ✭✭✭✭✭

    I have looked at the XF source code for this, the iOS CellRenderer just calls ReloadRows on the cell.
    The XF Cell does however wrap that call in a task.delay for some reason...

    https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Cells/Cell.cs#L211-L218
    https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs#L57-L69

  • JohnHJohnH GBMember ✭✭✭✭✭

    @DavidOrtinau, I can see that this thread is mentioned on a git report for an issue with the task.delay that I referred to above. Are you able to look into this?

  • JohnHJohnH GBMember ✭✭✭✭✭

    @JohnHair said:
    @DavidOrtinau, I can see that this thread is mentioned on a git report for an issue with the task.delay that I referred to above. Are you able to look into this?

    In case you couldn't find the github issue: https://github.com/xamarin/Xamarin.Forms/issues/2039

    @DavidOrtinau any change you can get an update on this?

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    Well, it appears the related PR was abandoned.

    @JohnHair and @Pekkachu, please add your comments and a reproduction to that issue so these details aren't a step removed in a forum thread.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @DavidOrtinau said:
    Well, it appears the related PR was abandoned.

    @JohnHair and @Pekkachu, please add your comments and a reproduction to that issue so these details aren't a step removed in a forum thread.

    Sure, happy to help. contact me via PM to negotiate my day rate and where I will need to submit my invoice...

  • taterjuicetaterjuice Member ✭✭

    I, too, had this problem - only on iOS. The same code worked on Android with no issues. Basically I could only call viewCell.ForceUpdateSize() 4-6 times, each time the UI would get slower and slower, until my entire UI would freeze up and the app would eventually crash.

    I found the answer in the comments section of this post:
    (link removed, my forum profile is too young to post links - Sorry!)

    Turns out, there's a bug in the iOS ListView Caching Strategy. To fix/workaround this, set your ListView Caching Strategy to "RecycleElement", like

    <ListView
        CachingStrategy="RecycleElement">...</ListView>
    

    Now when you do viewCell.ForceUpdateSize(), it won't slow and freeze your UI, but it may take a little longer for your listview to display other changes to it's items, since we're using a different caching strategy.

    Hope this helps!

  • PekkachuPekkachu USMember ✭✭

    Thanks for the input taterjuice, but it doesn't work for me. Using RecycleElement now doesnt even extend my cell on Tapping

  • PekkachuPekkachu USMember ✭✭

    Any Updates...?

  • NickCullenNickCullen Member ✭✭

    Hi,

    For what it's worth - I am seeing this exact issue on iOS to!

    Did you come up with a solution? I find that my cell sometimes doesn't drop down/expand.

  • PekkachuPekkachu USMember ✭✭

    @NickCullen said:
    Hi,

    For what it's worth - I am seeing this exact issue on iOS to!

    Did you come up with a solution? I find that my cell sometimes doesn't drop down/expand.

    in fact, the problem was always present just on the iOS side. Android was working pretty well!

    I got the problem finally fixed, my approach was not a good one:

    In my CustomViewCell (ExpandableViewCell) XAML, i was creating my view like this:

    StackLayout (40 px)
    ...Stuff
    AnotherStackLayout (30px)
    ...Extra Stuff

    So at first, my ExpandableViewCell was 40 px, upon tapping it was resized (using ForceUpdateSize, adding the extra 30px)

    This was not the current approach, putting CachingStrategy="RecycleElement" in the ListView was not working either (as everyone told me this would fix the issue)

    The answer was to bind IsVisible Property of the Second Stack Layout inside my ViewCell to a property of my model (IsShowingDetails) and calling ForceUpdateSize on my Ontapped() event inside my ExpandableViewCell

    public class ExpandableViewCell : ViewCell { protected override void OnTapped() { base.OnTapped(); ForceUpdateSize(); } }

    So, wrapping up everything:

    1. Create a ExpandableViewCell control, as per the code above
    2. Inside your ListView ViewCell (using your newely created ExpandableViewCell), put everything you want to show upon tapping inside a StackLayout, this StackLayout IsVisible property must be binded to a property of your Model, and default value is false

    private bool isShowingDetails = false; public bool IsShowingDetails { get { return isShowingDetails; } set { SetProperty(ref isShowingDetails, value); } }

    1. Your ListView has to have CachingStrategy="RecycleElement" and HasUnevenRows="True"

    This now fully works and doesnt freeze the UI! (Working on iOS)

  • NickCullenNickCullen Member ✭✭

    Hey, thanks for the fast response.

    My setup is actually near identical to the way you described actually! It just seems a bit glitchy for me.

    For example, the element that hides in and out is simply a stacklayout with a single label inside of it. Sometimes when I press the button on the list item that toggles the stack layout visibility, I can only see a bit of the top of the label (as if the cell isn't expanding enough) and then sometimes it works as intended.... It just doesn't seem consistent.

    I have tested this on my iPhone to, to eliminate the possibility of it being just a rendering issue on the simulator. And indeed I see the same result as above.

  • PekkachuPekkachu USMember ✭✭

    Try playing with your StackLayout Padding values, i was facing similar issue before, it was a little bit "buggy", instants after i tapped the ViewCell it was flickering, but after playing with Spacing and Paddings it worked

Sign In or Register to comment.