Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when adding to ObservableCollection #21374

Open
mooola opened this issue Mar 21, 2024 · 13 comments
Open

Error when adding to ObservableCollection #21374

mooola opened this issue Mar 21, 2024 · 13 comments
Assignees
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView i/regression This issue described a confirmed regression on a currently supported version p/2 Work that is important, but is currently not scheduled for release partner Issue or Request from a partner team platform/iOS 🍎 s/triaged Issue has been reviewed t/bug Something isn't working

Comments

@mooola
Copy link

mooola commented Mar 21, 2024

Description

Immediately after upgrading to Maui Controls 8.0.0.10, we have been getting an error when adding items to ObservableCollection

Steps to Reproduce

  1. Open Repro project
  2. Add breakpoint to line 73 of MainPageViewModel
  3. Click "Populate Person" button
  4. Should see the error message: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: The invalidation context ((null)) sent to -[UICollectionViewFlowLayout invalidateLayoutWithContext:] is not an instance of type UICollectionViewFlowLayoutInvalidationContext or a subclass. Collection view: <Microsoft_Maui_Controls_Handlers_Items_MauiCollectionView: 0x11776b600; baseClass = UICollectionView; frame = (0 0; 0 1048.5); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x28075e250>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x284913720>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x102fdda30>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x102fd6be0>>

Link to public reproduction project repository

https://github.com/mooola/MauiIssues

Version with bug

8.0.10 SR3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.7 SR2

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

Changing line 40 in MainPage.xaml to Auto "fixes" the issue.

Relevant log output

No response

@mooola mooola added the t/bug Something isn't working label Mar 21, 2024
@PureWeen PureWeen added i/regression This issue described a confirmed regression on a currently supported version platform/iOS 🍎 labels Mar 21, 2024
@PureWeen PureWeen added this to the .NET 8 SR4 milestone Mar 21, 2024
@Zhanglirong-Winnie Zhanglirong-Winnie added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Mar 22, 2024
@Zhanglirong-Winnie
Copy link

Verified this issue with Visual Studio 17.10.0 Preview 2&17.6.10 build 428. Not repro on iOS platform with sample project.
Maui Controls: 8.0.10/ 8.0.14/8.0.20-nightly.10336+sha.d03a2c66aa-azdo.9265184
https://github.com/mooola/MauiIssues
image

@adrian-ovidiu
Copy link

adrian-ovidiu commented Mar 22, 2024

I get this error in 8.0.10 and in 8.0.14 too. Worked well in 8.0.7. The workaround is not working for me in my project (changing * to Auto).

@mooola
Copy link
Author

mooola commented Mar 22, 2024

Issue is still present in 8.0.20-nightly.10336+sha.d03a2c66aa-azdo.9265184

@dotnet-policy-service dotnet-policy-service bot removed the s/try-latest-version Please try to reproduce the potential issue on the latest public version label Mar 22, 2024
@PureWeen PureWeen added the p/0 Work that we can't release without label Mar 22, 2024
@PureWeen
Copy link
Member

@mooola on your sample why do you have the WidthRequest on the border set to 0.5?

https://github.com/mooola/MauiIssues/blob/main/MauiApp3/MainPage.xaml#L36

If I set that border to something like 100 then I don't get the exception.

@mooola
Copy link
Author

mooola commented Mar 25, 2024

@PureWeen - I created the sample exactly how we found it in our app. I don't have an explanation as to why it is set to 0.5, but in our project when we removed that property entirely, we didn't get the exception. When we tried removing the property in our sample, we still got the exception so I didn't mention it. We are seeing this issue on multiple CollectionViews in our app. One that is contained in grids, doesn't have a border with a WidthRequests.

@mooola
Copy link
Author

mooola commented Mar 25, 2024

Something else we found was changing how the ObservableCollection was populated didn't cause the error.

private void PopulatePerson(object parameter)
{
    IsPersonVisible = true;

    try
    {
        List<Person> people = new List<Person>();

        for (int j = 0; j < 10; j++)
        {
            people.Add(new Person { Name = "Person " + j });
        }
        People = new ObservableCollection<Person>(people);
    }
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.ToString());
    }
}

@jsuarezruiz
Copy link
Contributor

More info, cannot reproduce it on Catalyst.

@PureWeen PureWeen self-assigned this Apr 2, 2024
@jsuarezruiz jsuarezruiz self-assigned this Apr 3, 2024
@jsuarezruiz
Copy link
Contributor

2024-04-03 08:58:43.796 Xamarin.PreBuilt.iOS[4227:1117230] ObjCRuntime.ObjCException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: The invalidation context ((null)) sent to -[UICollectionViewFlowLayout invalidateLayoutWithContext:] is not an instance of type UICollectionViewFlowLayoutInvalidationContext or a subclass. Collection view: <Microsoft_Maui_Controls_Handlers_Items_MauiCollectionView: 0x1153bae00; baseClass = UICollectionView; frame = (0 0; 0 710); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x302e957a0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x30296b9e0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x10a59ec40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x104f8dbb0>>

@bronteq
Copy link

bronteq commented Apr 3, 2024

Something else we found was changing how the ObservableCollection was populated didn't cause the error.

private void PopulatePerson(object parameter)
{
    IsPersonVisible = true;

    try
    {
        List<Person> people = new List<Person>();

        for (int j = 0; j < 10; j++)
        {
            people.Add(new Person { Name = "Person " + j });
        }
        People = new ObservableCollection<Person>(people);
    }
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.ToString());
    }
}

This workaround seems the same of these similar issues, maybe the problem affects more platforms?
#20037
#18481

@PureWeen
Copy link
Member

PureWeen commented Apr 3, 2024

Something else we found was changing how the ObservableCollection was populated didn't cause the error.

private void PopulatePerson(object parameter)
{
    IsPersonVisible = true;

    try
    {
        List<Person> people = new List<Person>();

        for (int j = 0; j < 10; j++)
        {
            people.Add(new Person { Name = "Person " + j });
        }
        People = new ObservableCollection<Person>(people);
    }
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.ToString());
    }
}

This workaround seems the same of these similar issues, maybe the problem affects more platforms? #20037 #18481

It's fairly platform independent why this would be causing issues.

@PureWeen
Copy link
Member

PureWeen commented Apr 3, 2024

@PureWeen - I created the sample exactly how we found it in our app. I don't have an explanation as to why it is set to 0.5, but in our project when we removed that property entirely, we didn't get the exception. When we tried removing the property in our sample, we still got the exception so I didn't mention it. We are seeing this issue on multiple CollectionViews in our app. One that is contained in grids, doesn't have a border with a WidthRequests.

Can you include a sample where this is crashing, and you aren't setting the Width to zero?

As far as we can tell this exception is fairly limited to scenarios where the CoillectionView is measuring to a zero width which is causing a strange invalidation to happen.

We could throw a couple things in that seem to avoid the crash, but I worry those are only fixing symptoms and you'll still get crashes for your other scenarios.

@PureWeen PureWeen modified the milestones: .NET 8 SR4, .NET 8 SR5 Apr 9, 2024
@PureWeen PureWeen modified the milestones: .NET 8 SR5, Backlog Apr 25, 2024
@PureWeen PureWeen added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label May 15, 2024
@DrJShoff
Copy link

For me, setting ItemSizingStrategy="MeasureFirstItem" on the CollectionView resolves the issue. Just as mentioned in this old bug report for Xamarin: xamarin/Xamarin.Forms#13323

@samhouts samhouts removed the s/triaged Issue has been reviewed label Jul 3, 2024
@samhouts samhouts added the s/triaged Issue has been reviewed label Jul 10, 2024
@PureWeen PureWeen modified the milestones: Backlog, 9.0-rc1 Aug 6, 2024
@PureWeen PureWeen assigned Redth and rmarinho and unassigned PureWeen and jsuarezruiz Aug 6, 2024
@PureWeen
Copy link
Member

PureWeen commented Aug 6, 2024

@Redth lets see if this is resolved by the new CV Handlers

#23928

@PureWeen PureWeen added p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint and removed p/0 Work that we can't release without labels Aug 6, 2024
@PureWeen PureWeen added p/2 Work that is important, but is currently not scheduled for release and removed p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint labels Aug 29, 2024
@PureWeen PureWeen added the partner Issue or Request from a partner team label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView i/regression This issue described a confirmed regression on a currently supported version p/2 Work that is important, but is currently not scheduled for release partner Issue or Request from a partner team platform/iOS 🍎 s/triaged Issue has been reviewed t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants