Rv kamboj
Rv kamboj

Reputation: 1

Object retain in memory when click on listview headertemplate in iOS device

I am developing an enterprise application using Xamarin.Forms. I am facing a memory leak issue in iOS specifically (working fine in Android).

Its regarding click over listview item header, I implemented gestures, then a transparent button as well, but it causes a memory leak - even if I comment out its functionality.

If I comment testbutton then its objects are disposing fine. What can I do to fix this issue?

 ```
    <ListView
                    Style="{DynamicResource BaseListViewStyle}"
                    IsGroupingEnabled="True"
                    ItemsSource="{Binding Deliveries.ExpandedDeliveryItemModels}"
                    IsPullToRefreshEnabled="True"
                    IsRefreshing="{Binding Deliveries.IsRefreshing}"
                    RefreshCommand="{Binding RefreshCommand}"
                    SelectionMode="None">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell AutomationId="headerCell">
                                <StackLayout AutomationId="headerLayout" Orientation="Horizontal">
                                    <Label  Text="{Binding HeaderTitle}"
                                            AutomationId="DeliveriesToday"
                                            HorizontalOptions="StartAndExpand">
                                    </Label>
                                    <Button x:Name="testbutton"
                                            HorizontalOptions="End"
                                            Command="{Binding Source={x:Reference deliveriesPage}, Path=BindingContext.HeaderClickCommand}"
                                            CommandParameter="{Binding .}">
                                    </Button>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell AutomationId="deliveriesCell">
                                <Label Text="Testing"></Label>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    ```

Upvotes: 0

Views: 160

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

Welcome to SO !

You can have a try with modifying CachingStrategy of ListView , there are three types of CachingStrategy value.

  • RecycleElement 1 Indicates that unneeded cells will have their binding contexts updated to that of a cell that is needed.

  • RecycleElementAndDataTemplate 3 Indicates that, in addition to the behavior specified by RecycleElement, DataTemplate objects that are selected by a DataTemplateSelector are cached by the data template type.

  • RetainElement 0 Indicates that for every item in the List View's ItemsSource property, a single unique element will be constructed from the DataTemplate.

The deflault value is RetainElement, the behavior is to retain item data in their generated cells when they are not needed.

For performance reasons, it is likely that the default behavior will be changed to RecycleElement in a future release. In the meantime, for memory and performance reasons, app developers should specify RecycleElement when constructing a new List View.

Therefore , you can modify code as follow :

<ListView CachingStrategy="RecycleElement" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <!-- ... -->
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 0

Related Questions