Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [iOS] CollectionView iOS inner crash while adding items to group, items aren't displayed #13268

Open
bondarenkod opened this issue Jan 2, 2021 · 45 comments · Fixed by #14341
Labels
blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often i/regression p/iOS 🍎 p/1 t/bug 🐛
Milestone

Comments

@bondarenkod
Copy link
Contributor

bondarenkod commented Jan 2, 2021

Description

I was testing the latest build of XamarinForms when I realized that there is an issue related to a group's item manipulation on the iOS platform. I can't reproduce this error on the Android platform.
I've also tried to use ObservableRangeCollection without any noticeable difference from the default ObservableCollection.
There are also a few lines of iOS inner exception message:

2021-01-02 14:32:21.361034+0200 xf_cv_range.iOS[3615:42647] *** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:], UICollectionView.m:7294
[0:] Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of sections.  The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).

Full output log is here
02_01_2021__14_40_13__e09b3d8b-4a6c-4d31-b7c0-9f047d3c1dfa.txt

I will add any additional info if needed.

Steps to Reproduce

  1. Use Xamarin.Forms 4.8.0.1821
  2. Add empty group to the ObservableCollection, then add items to the group:
var group = new Group();
Items.Add(group);
foreach (var item in items)
    group.Add(item);
  1. Run the app.

If you are using my demo project:

  1. Run the app on any iOS device/simulator.
  2. Switch to the second tab (Browse)
  3. Apply pull-to-refresh on the CV.
  4. You can downgrade XF version to 4.8.0.1687 and repeat the test to see that now it's working.

Expected Behavior

Added items are presented (visible) on the screen.

Actual Behavior

Added items aren't presented (visible) on the screen. You can see the exception in the VS's output window.

Basic Information

  • Version with issue: Xamarin.Forms 4.8.0.1821 (latest for today)
  • Last known good version: Xamarin.Forms 4.8.0.1687
  • Platform Target Frameworks:
    • iOS: 14.2
    • Android: No issue on this platform

Environment

not applicable

Build Logs

not applicable

Screenshots

Good result (XF 4.8.0.1687):
https://user-images.githubusercontent.com/3184414/103457346-2ec98280-4d07-11eb-81e1-caabdf11bce0.mp4
Bad result (XF 4.8.0.1821):
https://user-images.githubusercontent.com/3184414/103457371-6df7d380-4d07-11eb-8916-0615bedaae21.mp4

Reproduction Link

https://github.com/bondarenkod/xf_cv_group_add_range_bug

Workaround

Downgrade to Xamarin.Forms 4.8.0.1687

@bondarenkod bondarenkod added s/unverified New report that has yet to be verified t/bug 🐛 labels Jan 2, 2021
@samhouts samhouts added this to New in Triage Jan 2, 2021
@mphill
Copy link

mphill commented Jan 5, 2021

This is happening in Xamarin Forms 5.0.0.1829-pre6 as well. Code was previously working on older versions of Xamarin <= 4.8.0.1687.

This is only triggered with IsGrouped="True"

@hartez hartez self-assigned this Jan 6, 2021
@hartez hartez added i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often i/regression p/iOS 🍎 and removed s/unverified New report that has yet to be verified labels Jan 6, 2021
@hartez hartez added this to Backlog in CollectionView via automation Jan 6, 2021
@hartez hartez removed this from New in Triage Jan 6, 2021
@hartez hartez added this to the 5.0.0 milestone Jan 6, 2021
@hartez hartez added the blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. label Jan 6, 2021
@hartez hartez modified the milestones: 5.0.0, 5.0.1 Jan 6, 2021
@hartez hartez moved this from Review Backlog to Backlog in CollectionView Feb 7, 2021
@AdrianoBinhara
Copy link

Bind your list after the for statement. If you add itens to your list as you bind it to your collectionView the app is going to crash

@kgouraw
Copy link

kgouraw commented Mar 18, 2021

I think issue with ObservableCollection. Generic List works on iOS on XF>4.8.0.1687, but it doesn't bind CollectionView IsGrouped on Android. ObservableCollection is important part of MVVM, it should be fixed. Downgraded XF 4.8.1687 for working on both platforms for now.

@bondarenkod
Copy link
Contributor Author

@kgouraw nope, the problem inside the synchronizer.

@vniehues

This comment was marked as off-topic.

@RenatGaliew

This comment was marked as off-topic.

@vniehues
Copy link

I was actually able to work around this issue by using the ObservableRangeCollection and using it's AddRange() method.
It might have been a different underlying issue though.

@RenatGaliew
Copy link

I was actually able to work around this issue by using the ObservableRangeCollection and using it's AddRange() method.
It might have been a different underlying issue though.

Let's try by AddRange. I will write how the results will be.

@RenatGaliew
Copy link

I was actually able to work around this issue by using the ObservableRangeCollection and using it's AddRange() method.
It might have been a different underlying issue though.

The result is negative. Did not help. Let's try to think of another solution.

@bondarenkod
Copy link
Contributor Author

@RenatGaliew try to use dynamic data

@RenatGaliew
Copy link

@bondarenkod my data is already dynamic, once the data is loaded everything is fine, then after switching to another page and returning to the current one, an error crashes and nothing more will be added, it crashes on the add method

@PCDK
Copy link

PCDK commented Apr 20, 2021

I am seeing the same thing, and I am also using the Dynamic Data package to operate the collection.

@bondarenkod
Copy link
Contributor Author

@RenatGaliew oh, sorry, I meant this https://github.com/reactivemarbles/DynamicData

@kalprajkakani

This comment was marked as off-topic.

@jfversluis
Copy link
Member

Seems like some people reported this as still not working unfortunately.

@jfversluis jfversluis reopened this Jun 23, 2022
CollectionView automation moved this from Done to Review Backlog Jun 23, 2022
@jfversluis
Copy link
Member

Everyone still experiencing this and watching this issue, a PR for this is open now, would you be able to grab the NuGet as described here and let us know if this fixes this issue? That will greatly speed up the review process. Make sure to take the exact version number that is listed on the PR.

Besides verifying if this particular issue is fixed also be sure to check other scenarios in the same area to make sure that this fix doesn't accidentally has side-effects 🙂

Thanks!

@jfversluis
Copy link
Member

Anyone able to test this?

@BenBtg
Copy link

BenBtg commented Jul 14, 2022

@jfversluis I've been working with a customer who is still seeing this issue. They have helped create a repro sample app which consistently crashes
ReproSample

@bondarenkod
Copy link
Contributor Author

bondarenkod commented Jul 14, 2022

I'm sorry guys, I'm currently closing release, so I'm not able to pay any attention to this issue nor this or the next week.

@hartez hartez removed their assignment Jul 28, 2022
@SEngelsKTS
Copy link

Anyone able to test this?

@jfversluis I have tested the PR. My problems with the CollectionView were solved with it!

@kazutsugu
Copy link

@jfversluis ?? Where to find the NuGet? Nothing in the DevOps artifact. Sorry. I am newbie, may be making a stupid mistake.

@SEngelsKTS
Copy link

@jfversluis ?? Where to find the NuGet? Nothing in the DevOps artifact. Sorry. I am newbie, may be making a stupid mistake.

You have to add an additional package source (https://aka.ms/forms-prs/index.json), then the NuGet will be found.

@kazutsugu
Copy link

@jfversluis Super! Your PR appears fixing my issue too. Another quick advice please. What can I do, in order to be notified when this particular PR is merged into an official release?

@ikettansinha
Copy link

ikettansinha commented Aug 29, 2022

I have downgraded the Xamarin Forms version to 5.0.0.7396 as advised Gerald by using given package source (https://aka.ms/forms-prs/index.json) but still my issue is occurring. below is exception

image
image

Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). Collection view: <UICollectionView: 0x7f82c23fe200; frame = (0 0; 388 452); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600002fdefd0>; layer = <CALayer: 0x6000067ef700>; contentOffset: {0, 0}; contentSize: {388, 204.99999999999994}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7f82c17d7070>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7f82c3837810>>

Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). Collection view: <UICollectionView: 0x7f82c23fe200; frame = (0 0; 388 452); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600002fdefd0>; layer = <CALayer: 0x6000067ef700>; contentOffset: {0, 0}; contentSize: {388, 204.99999999999994}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7f82c17d7070>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7f82c3837810>>
Native stack trace:
0 CoreFoundation 0x0000000111121ba4 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x000000011fa12be7 objc_exception_throw + 48
2 Foundation 0x0000000119523aa2 _userInfoForFileAndLine + 0
3 UIKitCore 0x0000000138f7ab6f -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:] + 14108
4 UIKitCore 0x0000000138f75f90 -[UICollectionView _updateSections:updateAction:] + 435
5 UIKitCore 0x0000000138f7605a -[UICollectionView insertSections:] + 64
6 UIKit 0x0000000166aa9537 -[UICollectionViewAccessibility insertSections:] + 42
7 AppName 0x000000010edd8239 xamarin_dyn_objc_msgSend + 217
8 ??? 0x00000001676c1730 0x0 + 6030104368

@SEngelsKTS
Copy link

@ikettansinha The correct version of Gerald is XF 5.0.0.8268.

Version 5.0.0.7396 is only chosen as an example in the description. The correct version can be found in the linked pull request #15439.

@ikettansinha
Copy link

ikettansinha commented Aug 29, 2022

@SEngelsKTS It solved the issue thanks you very much for the prompt response.
@jfversluis Thanks gerald versluis

@kazutsugu
Copy link

@jfversluis Super! Your PR appears fixing my issue too. Another quick advice please. What can I do, in order to be notified when this particular PR is merged into an official release?

It stops crushing indeed. But the scrolling gets very slow, actually unusable. So I went back to official latest package, abandoned ObservableObjects, and have List instead.

@ikettansinha
Copy link

Hi @kazutsugu So now you are using simple list as item source for your collection view?

@ikettansinha
Copy link

Same Happening now. Scrolling is not usable now

@kazutsugu
Copy link

Hi @kazutsugu So now you are using simple list as item source for your collection view?

Yes. If I understand correctly, this issues comes in the combination of CollectionView grouping enabled and ObservableObjects. I saw in a sample code provided by MS that for CollectionView grouping enabled, it is actually List is used.

@reydev-sourcecoder
Copy link

Bind your list after the for statement. If you add itens to your list as you bind it to your collectionView the app is going to crash

This solves the issue in my case. Thanks.

@Markus2807
Copy link

Hi I ran into the same issue.

Here is a great workaround for this problem.
Instead of using an ObservableCollection use DynamicData.

https://github.com/RolandPheasant/DynamicData.Snippets/blob/e294b28247e41a032105828b6ca663ca16104690/DynamicData.Snippets/Group/XamarinFormsGrouping.cs

This one worked for me and got some benefits sorting the list

@samhouts samhouts added the p/1 label Jan 6, 2023
@mrobraven

This comment was marked as off-topic.

@draganDBL
Copy link

draganDBL commented Aug 30, 2023

do we know what is causing the issue ?
on android it is ok but on ios still not working.
I'm using dynamic data SourceCache

` var subject = new SourceCache<Subject, long>(s => s.Id);
subject.Edit(innerCashe =>
{
innerCashe.Clear();
innerCashe.AddOrUpdate(dbSubjects);
});

        var dataConnection = subject.Connect(); 
        dataConnection
                .Where(x => x != null)
                .RefCount()
                .Filter(queryFilter)
                .Transform(s=>new SubjectCellViewModel(s))
                .Group(GroupByGroupName)
                .Transform(subj => new ObservableGroupedCollection<string, SubjectCellViewModel, long>(subj))
                .Sort(SortExpressionComparer<ObservableGroupedCollection<string, SubjectCellViewModel, long>>.Ascending(a => a.Key))
                .ObserveOn(MainThreadScheduler)
                .Bind(out _subjects)
                .DisposeMany()
                .Subscribe();
                
                 private string  GroupByGroupName(SubjectCellViewModel subjectCellViewModel) {
                                      return subjectCellViewModel.GroupName;}                    
                `

@bondarenkod
Copy link
Contributor Author

@draganDBL
try these methods:

.Batch(TimeSpan.FromMilliseconds(100))
.ObserveOn(RxApp.MainThreadScheduler)
.SubscribeOn(RxApp.MainThreadScheduler)

I heavily rely on DynamicData for some fresh features in my projects.
I decided not to use native grouping feature in my project long time ago, instead I do group using this:

    public interface IRxViewModel
    {
        string Handle { get; }
        IRxOrderMetadata Rx { get; }
    }
    
 public interface IRxOrderMetadata
{
    int RxGroup { get; set; }
    DateTimeOffset RxOrderDate { get; set; }
    ulong RxOrder { get; set; }
}

This gives me possibility to have all the grouping features except native features like sticky scrolling, etc.

@draganDBL
Copy link

Hello @bondarenkod still the same same exception happens.

@draganDBL
Copy link

draganDBL commented Sep 1, 2023

Hello @bondarenkod I have added
.Batch(TimeSpan.FromMilliseconds(100))
after

dataConnection
                .Where(x => x != null)
                .Batch(TimeSpan.FromMilliseconds(100)) 

and this fix my issued when I have tested on real device and no more exception. Thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often i/regression p/iOS 🍎 p/1 t/bug 🐛
Projects
CollectionView
  
Review Backlog