Xamarin Forms ListView CachingStrategy

I recently tested CachingStrategy for a ListView in a Xamarin Forms application with 1000 items in a list. List items are created from a ViewCell data ViewCell . I tried using the RecycleElement parameter for CachingStrategy .

When I was profiling using the Xamarin Profiler for an Android application deployed on the Xamarin Anroid Player (emulator), I noticed that when scrolling through the list, the memory allocation does not increase (on the allocation summary tab). But, when I was doing profiling for the iPhone App on the emulator, I noticed that the data is not displayed on the "Summary" tab. So I grabbed some snapshots while scrolling through the list and noticed that whenever I scroll through the list (up or down), the memory allocation continues to increase.

Why RecycleElement n't RecycleElement work for iOS (iPhone)?

I am using a Mac for development. Here are my tools:

=== Xamarin Studio ===

Version 5.10.1 (build 3) UUID installation: 7ae992a3-b710-4297-ba1d-0c519fbb2ea8 Runtime: Mono 4.2.1 (explicit / 6dd2d0d) GTK + 2.24.23 (Raleigh theme)

 Package version: 402010102 

=== Xamarin.Profiler ===

Version: 0.24.0.0 Location: / Applications / Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 7.1.1 (9081) Build 7B1005

=== Xamarin.iOS ===

Version: 9.2.1.54 (Enterprise Edition) Hash: eb4c1ef Branch: master Build date: 2015-12-01 02: 12: 30-0500

=== Xamarin.Android ===

Version: 6.0.0.34 (Enterprise Edition) Android SDK: / Users / Haider / Library / Developer / Xamarin / android-sdk-macosx Supported Android versions: 4.0.3 (API level 15) 4.4 (API level 19) 5.0 (API level 21) 5.1 (API level 22) 6.0 (API level 23)

SDK Tools Version: 24.4.1 SDK Platform Tools Version: 23.1 rc1 SDK Build Tools Version: 23.0.2

Java SDK: / usr java version "1.7.0_71" Java (TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot (TM) 64-bit server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5 Location: / Applications / Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.109 (starter version)

=== Assembly Information ===

Release ID: 510010003 Git version: f2021a209d66d49cbc0649a6d968b29040e57807 Build date: 2015-12-01 10: 43: 40-05 Xamarin add-ons: dfd4f5103e8951edbc8ac24480b53b53c55e04ff Build-lane-base:

=== operating system ===

Mac OS X 10.11.1 Darwin Haiders-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0 Sat Sep 19 15:53:46 PDT 2015 root: xnu-3247.10.11 ~ 1 / RELEASE_X86_64 x86_64

+6
source share
1 answer

Here are a few things to check.

  • In the Xamarin Profiler, make sure that you are looking only for your own ViewCell class and take a few snapshots to run the garbage collector. Perhaps something else causes a memory leak if the number of ViewCells does not increase. If the number of ViewCells increases, skip to sentences 2 and 3 below. Xamarin Profiler ViewCell Example

  • In the ViewCell code, be sure to override OnBindingContextChanged() and set the properties of the controls to OnBindingContextChanged() , and not in the ViewCell constructor. I have added the sample code below that shows how to implement the ListViewCachingStrategy.RecycleElement strategy using a custom ViewCell.

  • If you sign an event handler for ViewCell (to add a Context action , for example), be sure to sign an event handler in the OnAppearing() method of the OnAppearing() class and unsubscribe the event handler in the OnDisappearing() method of the OnDisappearing() class. I added a comment in the following ViewCell code example.

ListView using RecycleElement

 ListView = new ListView(ListViewCachingStrategy.RecycleElement) { DataTemplate(typeof(CustomViewCell)) }; 

ViewCell

 public class CustomViewCell : ViewCell { Label _myLabel; MenuItem _deleteAction; public CustomViewCell() { _myLabel = new Label(); View = _myLabel; } protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); _myLabel.Text = ""; var item = BindingContext as MyModel; if (item != null) { _myLabel.Text = item.Text; } } protected override void OnAppearing() { base.OnAppearing(); //Subscribe ViewCell Event Handlers _deleteAction.Clicked += HandleDeleteClicked; ContextActions.Add(_deleteAction); } protected override void OnDisappearing() { base.OnDisappearing(); //Unsubscribe ViewCell Event Handlers _deleteAction.Clicked -= HandleDeleteClicked; ContextActions.Remove(_deleteAction); } void HandleDeleteClicked(object sender, EventArgs e) { //Code to handle when the delete action is tapped } } 

ViewCell Model

 public class MyModel { [PrimaryKey] public int ID { get; set; } public string Text { get; set; } } 
+1
source

All Articles