I have a Xamarin forms project which consists of a ColectionView whose ItemSource is backed by a bindable property of type ObservableCollection in the ViewModel. Adding new items to the ObservableCollection is throwing NSInternalInconsistencyException, that too only on actual device whereas in the simulator it's working fine. Also if I create a new local parameter to hold the ItemSources values until all items are added to it and assigning this to the ItemSource bound property also works.
ObservableCollection<ExploreUIDataSet> _exploreDataList = new ObservableCollection<ExploreUIDataSet>();
public ObservableCollection<ExploreUIDataSet> ExploreDataList
{
get { return _exploreDataList; }
set { _exploreDataList = value; OnPropertyChanged(nameof(ExploreDataList)); }
}
ExploreDataList.Add() will trow NSInternalInconsistencyException. Creating a local variable and adding all values to it and assigning it back to ExploreDataList is working fine. In native ios projects, we used to handle such changes in source through batch updates. But in forms do we need to do that, especially when the CollectionView is backed by the observable collection. Also having a Stacklayout with BindabLayout.itemsource as the ExploreDataList works even when we add new items into the ObservableCollection.
ie the below code works
<StackLayout Spacing="{DynamicResource Spacing10}"
Padding="{DynamicResource ExploreMainStkPadding}"
BindableLayout.ItemsSource="{Binding ExploreDataList}"
BindableLayout.ItemTemplateSelector="{DynamicResource ExploreSelector}">
</StackLayout>
whereas the below CollectionView code fails
<CollectionView VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" ItemsSource="{Binding ExploreDataList}" ItemTemplate="{DynamicResource ExploreSelector}"/>