Hi to, I am new on Xamarin and on this Forum. Sorry if I dont write English perfect, my English is not very good hehe.
Im having some troubles with an application I'm developing. First of all I'll expose what I am trying to do:
- There is a ListView which loads data from a binded class.
- When ListView arrive's to the last item, it loads more items, like an infinite scroll.
- I load data with an a Task: await Task.Run(() => AddElementos());
The problem is the APP crashes when more data is being loaded on the ListView, and then I click on any element of the ListView before it finishes the load. If I click on another element out of the ListView I can go to another window with no problem, it only crash when I click on an item of the ListView.
I also tried to don't do anything when an item is clicked, with any result, it also crashes.
Im going crazy, I tried a lot of possibilities and also searched a lot on Google, with no luck =(
Now I'll add some code fragments showing how Im doing this:
The ListView attributes:
x:Name="ItemsListView"
ItemsSource="{Binding Elementos}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding ReCargarElementos}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RetainElement"
ItemAppearing="OnItemAppearing"
Page's class (first items load when page loaded)
async protected override void OnAppearing()
{
base.OnAppearing();
if (viewModel.Elementos.Count == 0)
{
viewModel.CargarElementos.Execute(ItemsListView);
}
}
Page's class (Load more items when ListView arrives to the last element)
async void OnItemAppearing(object Sender, ItemVisibilityEventArgs e)
{
Elementos aux1 = (Elementos)e.Item;
Elementos aux2 = (Elementos)(viewModel.Elementos[viewModel.Elementos.Count - 1]);
if (aux1.id == aux2.id)
{
try
{
viewModel.CargarElementos.Execute(ItemsListView);
}
catch(Exception ex)
{
string error = ex.Message.ToString();
}
}
}
The ViewModel
public CatalogoViewModel()
{
Title = "Catalog";
Elementos = new ObservableRangeCollection<Elementos>();
CargarElementos = new Command(async () => await ExecuteCargarElementos());
ReCargarElementos = new Command(async () => await ExecuteReCargarElementos());
}
The ViewModel (The task which marks the window as "busy" and executes a task in a thread to load items)
async Task ExecuteCargarElementos()
{
if (IsBusy) return;
IsBusy = true;
try
{
await Task.Run(() => AddElementos());
}
catch (Exception ex)
{
}
finally { IsBusy = false; }
}
The ViewModel (finally the task which obtains the data)
async Task AddElementos()
{
int qty = Elementos.Count;
string URL_FINAL = "https://mydataapi.com/catalog?key=THEKEY&npos=" + qty + "etc...";
string json = await new WebClient().DownloadStringTaskAsync(new Uri(URL_FINAL));
JArray newElem = (JArray)JsonConvert.DeserializeObject(json);
foreach (var elem in newElem)
{
Elementos.Add(new Elementos(pass the variables to create element));
}
}
And there is the debug log when it crashes:
07-05 11:26:00.670 W/Mono ( 2830): The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
07-05 11:26:00.678 D/Mono ( 2830): Unloading image System.Core.dll [0x9c64a200].
07-05 11:26:00.678 D/Mono ( 2830): Image addref System.Core[0x9c404a00] -> System.Core.dll[0x9f1ea800]: 62
07-05 11:26:00.679 D/Mono ( 2830): Config attempting to parse: 'System.Core.dll.config'.
07-05 11:26:00.679 D/Mono ( 2830): Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Core/System.Core.config'.
07-05 11:26:00.701 D/Mono ( 2830): [0x9d01d930] hill climbing, change max number of threads 1
07-05 11:26:00.712 I/Choreographer( 2830): Skipped 40 frames! The application may be doing too much work on its main thread.
07-05 11:26:01.373 D/Mono ( 2830): GC_BRIDGE waiting for bridge processing to finish
07-05 11:26:01.412 I/art ( 2830): Starting a blocking GC Explicit
07-05 11:26:01.663 I/art ( 2830): Explicit concurrent mark sweep GC freed 5521(258KB) AllocSpace objects, 1(160KB) LOS objects, 40% free, 4MB/7MB, paused 1.980ms total 231.134ms
07-05 11:26:01.669 D/Mono ( 2830): GC_TAR_BRIDGE bridges 185 objects 931 opaque 254 colors 181 colors-bridged 181 colors-visible 181 xref 4 cache-hit 0 cache-semihit 0 cache-miss 0 setup 5.00ms tarjan 3.60ms scc-setup 0.34ms gather-xref 0.14ms xref-setup 0.03ms cleanup 0.25ms
07-05 11:26:01.669 D/Mono ( 2830): GC_BRIDGE: Complete, was running for 291.49ms
07-05 11:26:01.669 D/Mono ( 2830): GC_MAJOR: (LOS overflow) time 205.55ms, stw 271.67ms los size: 5120K in use: 347K
07-05 11:26:01.669 D/Mono ( 2830): GC_MAJOR_SWEEP: major size: 3968K in use: 1865K
InspectorDebugSession(74): HandleTargetEvent: UnhandledException
Thread finished: #12
InspectorDebugSession(74): HandleTargetEvent: ThreadStopped
El subproceso 'Unknown' (0xc) terminó con código 0 (0x0).
Thread finished: #9
InspectorDebugSession(74): HandleTargetEvent: ThreadStopped
El subproceso 'Unknown' (0x9) terminó con código 0 (0x0).
07-05 11:26:10.183 D/Mono ( 2830): [0x9d01d930] worker finishing
Thanks A LOT