We have migrated our app into MAUI.
In Android devices or even in emulator release mode if I use the application for some time it starts getting slow and eventually freezes or crashes. It generally happens when I click on a button, and some operation happens and redirects to another screen.
I tried GC.Collect which prevents application crash but make it app slow.
private async Task ProcesRecord()
{
Stopwatch watch = new Stopwatch();
try
{
watch.Start();
if (_qtyRemain > 0)
{
Reset();
return;
}
IsBusy = true;
var dtSave = new DataTable("item_loc");
dtSave.Columns.Add("id", typeof(string));
if (_isInd)
{
dtSave.Columns.Add("code", typeof(string));
}
var dtHold = new List<Test>();
if (!_isInd)
{
foreach (var lvItem in Results)
{
if (!(lvItem.qty > 0)) continue;
var dr = dtSave.NewRow();
dr["id"] = _itemId;
dtSave.Rows.Add(dr);
}
}
else
{
var results = (string.IsNullOrEmpty(_Id)) ? Results.ToList() : ItemList;
foreach (var lvItem in results ?? [])
{
if (string.IsNullOrEmpty(Id) && !(lvItem.qty_assigned > 0)) continue;
var dr = dtSave.NewRow();
dr["id"] = lvItem.item_id.Trim();
}
var dsSave = new DataSet("item_loc");
dsSave.Tables.Add(dtSave);
dsSave.AcceptChanges();
dsSave.Dispose();
dtSave.Dispose();
dsSave = null;
dtSave = null;
var msg = await new ApiController().UpdateItem(model);
if (msg?.ToLowerInvariant() != "true")
{
string? errorMesg = msg?.Substring(msg.IndexOf("Message:")).Replace("Message:", "");
if (errorMesg != null)
await DisplayAlert("Item", errorMesg.Trim(), "Ok");
}
else
{
switch (_itemType)
{
case Type.Item:
{
var frm = new ItemTabbedPage(_itemType);
await GoTo(frm);
break;
}
case Type.Accepted:
{
if (_parentForm != null && _parentForm == "Receive")
{
var frm = new ReceivePage();
await GoTo(frm);
}
else
{
var navigation = Application.Current?.MainPage?.Navigation;
if (navigation?.NavigationStack[navigation.NavigationStack.Count - 2] is AssignPage previousPage)
{
navigation.RemovePage(previousPage);
}
var tabbedPage = new CVAwayPage();
if (tabbedPage.Children[0] is CVReadyPage cvReadyPage)
{
if (_sendSeaBack)
{
tabbedPage.Children[0] = new CVReadyPage(_itemId);
}
else
{
tabbedPage.Children[0] = new CVReadyPage();
}
}
await Application.Current!.MainPage!.Navigation.PushAsync(tabbedPage);
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
}
catch (Exception ex)
{
await DisplayErrorAlert(ex.Message);
ReportError("Items: Failed.", ex);
}
finally
{
if (watch.IsRunning)
{
watch.Stop();
string paramvalue = "Test";
string count = Results != null ? Results.Count.ToString() : "0";
int success = await InsertExecutionLogDetails("Items", paramvalue, count, watch.ElapsedMilliseconds.ToString());
}
}
}
And OnAppearing of the next page where it should redirects having this below code
protected override void OnAppearing()
{
try
{
base.OnAppearing();
ViewModel?.OnAppearing();
SetupListeners();
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
ReportError("AssignItemPage.OnAppearing: Failed.", ex);
DisplayErrorAlert(ex.Message);
}
}
Data grid usage in screen
<StackLayout Grid.Row="7" Orientation="Vertical" Margin="0,0,0,0">
<dg:DataGrid x:Name="Grid" ItemsSource="{Binding Results}" HeightRequest="230">
<dg:DataGrid.NoDataView>
<Label Text="No item locations found, please setup new item location(s)." TextColor="Crimson" HorizontalTextAlignment="Center" />
</dg:DataGrid.NoDataView>
<dg:DataGrid.Columns>
<helpers:DataGridColumn Title="Location" Width="100" PropertyName="location_label"/>
<helpers:DataGridColumn Title="Cur.Qty" Width="80" PropertyName="total_qty" />
<helpers:DataGridColumn Title="Asnd." Width="80" PropertyName="qty_assigned"/>
<helpers:DataGridColumn Title="Status" Width="100" PropertyName="inventory_loc_status"/>
<helpers:DataGridColumn Title="MaxQty" Width="80" PropertyName="item_loc_max_qty" />
<helpers:DataGridColumn Title="Type" Width="160" PropertyName="location_type"/>
<helpers:DataGridColumn Title="Size" Width="110" PropertyName="location_size"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
</StackLayout>
DataGrid.xaml file
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollView Orientation="Horizontal" Grid.Row="0" HorizontalScrollBarVisibility="Never">
<Grid x:Name="HeaderView" RowSpacing="0" Padding="0,0,0,0">
<Grid.Resources>
<ResourceDictionary>
<!--Default Header Style-->
<Style x:Key="HeaderDefaultStyle" TargetType="Label">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontAttributes" Value="Bold"/>
<Setter Property="HorizontalOptions" Value="Start"/>
<Setter Property="VerticalOptions" Value="Center"/>
<Setter Property="HorizontalTextAlignment" Value="Start"/>
<Setter Property="LineBreakMode" Value="TailTruncation"/>
</Style>
<Style TargetType="Grid">
<Setter Property="BackgroundColor" Value="{Binding HeaderBackground,Source={x:Reference Self}}"/>
</Style>
<Style x:Key="ImageStyleBase" TargetType="Image">
<Setter Property="Aspect" Value="AspectFill"/>
<Setter Property="VerticalOptions" Value="Center"/>
<Setter Property="HorizontalOptions" Value="Center"/>
<Setter Property="HeightRequest" Value="5"/>
<Setter Property="WidthRequest" Value="9"/>
<!--<Setter Property="Margin" Value="0,0,4,0"/>-->
</Style>
<Style x:Key="AscendingIconStyle" TargetType="Image" BasedOn="{StaticResource ImageStyleBase}">
<Setter Property="Source" Value="{Binding AscendingIcon, Source={x:Reference Self}}"/>
</Style>
<Style x:Key="DescendingIconStyle" TargetType="Image" BasedOn="{StaticResource ImageStyleBase}">
<Setter Property="Source" Value="{Binding DescendingIcon, Source={x:Reference Self}}"/>
</Style>
</ResourceDictionary>
</Grid.Resources>
</Grid>
</ScrollView>
<AbsoluteLayout Grid.Row="1">
<ScrollView x:Name="Sv" Orientation="Horizontal" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<helpers:DataGridListView x:Name="ListView" HeightRequest="190" VerticalOptions="Start"></helpers:DataGridListView>
</ScrollView>
<helpers:DataGridVertScrollBar IsVisible="{Binding EnableCustomScroll}" VerticalOptions="Start" HorizontalOptions="End" x:Name="ssv" HeightRequest="190" WidthRequest="10" AbsoluteLayout.LayoutFlags="HeightProportional,YProportional,XProportional"
AbsoluteLayout.LayoutBounds="1,1,8,1"></helpers:DataGridVertScrollBar>
</AbsoluteLayout>
<ContentView x:Name="NoDataViewElement" Grid.Row="0" Grid.RowSpan="2" VerticalOptions="Center" IsVisible="False"/>
</Grid>
checked in Appcenter also but it does not capture this crash or freeze
Can some help to fix
Thanks
21