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

[Bug][iOS] Concurrent issue leading to crash in SemaphoreSlim.Release in ObservableItemsSource #11853

Closed
softlion opened this issue Aug 19, 2020 · 36 comments · Fixed by #13119
Closed
Assignees
Labels
a/collectionview e/5 🕔 5 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎 t/bug 🐛

Comments

@softlion
Copy link
Contributor

softlion commented Aug 19, 2020

Description

Calls to ObservableItemsSource.BatchUpdate should be synchronized to prevent a crash in _batchUpdating.Release(), because this last one can be called 2 times in a row and will fail the second time.

https://github.com/xamarin/Xamarin.Forms/blob/main/Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs

Steps to Reproduce

I am able to reproduce this issue easily in my customer's app.
I've already checked that all calls are done on the main thread.

It happens nearly everytime when updating the bound ObservableCollection 3 times in a row:
1st deleting some items, 2nd inserting some new items, 3rd moving some existing items (it's an algorithm that compute the changes between an ObservableCollection and a "new" list, and apply the 3 above actions so after the ObservableCollection becomes equal to the list).

I've already checked that all 3 calls are done on the main thread.

Expected Behavior

don't crash

Actual Behavior

crash when releasing the SemaphoreSlim because its CurrentCount is already 1.

Stack trace:

SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count.

  at System.Threading.SemaphoreSlim.Release (System.Int32 releaseCount) [0x0004c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:795 
  at System.Threading.SemaphoreSlim.Release () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:760 
  at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Reload () [0x00098] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:146 
  at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Add (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00037] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:165 
  at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00061] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:117 
  at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x000b7] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:108 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/Foundation/NSAction.cs:178 
--- End of stack trace from previous location where exception was thrown ---

Basic Information

  • Version with issue: 4.8
  • Last known good version: 4.5
  • IDE: VS Windows
  • Platform Target Frameworks:
    • iOS: latest stable
  • Affected Devices: iOS simulator

Screenshots

Reproduction Link

Repro app: https://github.com/softlion/ReproFormsCollectionViewBug
Run the app on an iOS simulator or device.

Tested on iOS 12.4 / iPhone 6 simulator.
But happens on all iOS OS and devices.

Does not happen on Android / UWP.

This app will trigger one of the 3 above crashes, which comes from different synchronization bugs.
The key is the change of the IsVisible property just before or after items have been added to the collectionviewsource.

Workaround

None

Other infos

I've checked the xamarin forms source code.

I'm pretty sure you can fix it easily by making BatchUpdate return a Task, then start BatchUpdate() with await _batchUpdating.WaitAsync(); instead of putting _batchUpdating.Wait() inside the PerformBatchUpdates action parameter.

@softlion softlion added s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 19, 2020
@jsuarezruiz
Copy link
Contributor

@softlion Thanks for the feedback. Do you have a small example where reproduce the issue?.

@jsuarezruiz
Copy link
Contributor

@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Aug 25, 2020
@softlion
Copy link
Contributor Author

I'm unable to reproduce the issue in a demo project.
So i'll double check that all calls are done on the main thread.

@acuntex
Copy link
Contributor

acuntex commented Aug 30, 2020

I can also confirm that I have days where it does not happen at all. But suddenly when my system is under load I sometimes get these exceptions in the iOS simulator.

I was using the ObservableRangeCollection by @jamesmontemagno and did add about 25 elements when the error occurs regularly.

It happens during the ItemThresholdCommand of a CollectionView, so my guess was that it's on the main thread anyway.

@softlion
Copy link
Contributor Author

softlion commented Aug 31, 2020

Ok I have a repro project ! I'll post it here.

Btw there are 3 different issues triggered by the same bug.
1

{System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index at System.Collections.Generic.List1[T].get_Item (System.Int32 index) [0x00009] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:161 at System.Collections.ObjectModel.Collection1[T].System.Collections.IList.get_Item (System.Int32 index) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:266 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.ElementAt (System.Int32 index) [0x0000f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:254 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.get_Item (System.Int32 index) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:40 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.get_Item (Foundation.NSIndexPath indexPath) [0x00019] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:96 at Xamarin.Forms.Platform.iOS.ItemsViewController1[TItemsView].UpdateTemplatedCell (Xamarin.Forms.Platform.iOS.TemplatedCell cell, Foundation.NSIndexPath indexPath) [0x00012] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:199 at Xamarin.Forms.Platform.iOS.ItemsViewController1[TItemsView].GetCell (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:84 at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/UIKit/UIApplication.cs:86 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/UIKit/UIApplication.cs:65 at ReproApp.iOS.Application.Main (System.String[] args) [0x00001] in D:\repos\ReproApp\ReproApp.iOS\Main.cs:17 }

2

[CollectionView] An attempt to prepare a layout while a prepareLayout call was already in progress (i.e. reentrant call) has been ignored. Please file a bug. UICollectionView instance is (<UICollectionView: 0x7f9e79f4f400; frame = (0 0; 375 320); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600002561410>; layer = <CALayer: 0x600002c2dc20>; contentOffset: {0, 0}; contentSize: {375, 0}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7f9e7863af60>)

3

{System.Threading.SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count. at System.Threading.SemaphoreSlim.Release (System.Int32 releaseCount) [0x0004c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:795 at System.Threading.SemaphoreSlim.Release () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:760 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Reload () [0x00098] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:146 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Add (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00037] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:165 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00061] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:117 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x000b7] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:108 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/UIKit/UIApplication.cs:86 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/UIKit/UIApplication.cs:65 at ReproApp.iOS.Application.Main (System.String[] args) [0x00001] in D:\repos\ReproApp\ReproApp.iOS\Main.cs:17 } | System.Threading.SemaphoreFullException

@softlion
Copy link
Contributor Author

softlion commented Aug 31, 2020

Repro app: https://github.com/softlion/ReproFormsCollectionViewBug
Run the app on an iOS simulator or device.

Tested on iOS 12.4 / iPhone 6 simulator.
But happens on all iOS OS and devices.

Does not happen on Android / UWP.

This app will trigger one of the 3 above crashes, which comes from different synchronization bugs.
The key is the change of the IsVisible property just before or after items have been added to the collectionviewsource.

@samhouts samhouts removed the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Aug 31, 2020
@hartez hartez self-assigned this Sep 5, 2020
@hartez hartez removed the s/unverified New report that has yet to be verified label Sep 5, 2020
@hartez hartez added i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often e/5 🕔 5 labels Sep 5, 2020
@samhouts samhouts added this to the 5.0.0 milestone Sep 8, 2020
@softlion
Copy link
Contributor Author

softlion commented Sep 24, 2020

Please fix it fast as i gave you a 1 line change solution + a repro app.

I've PR only once in Forms and it's 4 years ago. I'm not sure how much time it will now require to: clone the repo / build a local nuget / add all forms projects to the repro app / make sure the issue still triggers / fix it / rebase the repo / create a fix branch / push / commit / pr / receive feedbacks / re-rebase / re-commit

image

image

@lafritay
Copy link

lafritay commented Oct 5, 2020

@samhouts @hartez FWIW, we're seeing this error happen often in production as well on 4.7.0.latest. @softlion Thanks for the great details in the bug. Would really appreciate traction on this bug given a fix has already been proposed.

@denisgeom
Copy link

I also use CollectionView on a ContentPage. When I return to previous Page I always get the exception shown below.
This only happen if the collection has more than one item. This does not happen with just one item and it also works fine with UWP and Android. It only happen with iOS. This is a show stopper and not sure of a work around. Please fix it ASAP or provide a work around. Thanks.

◢ | $exception | {System.Threading.SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count. at System.Threading.SemaphoreSlim.Release (System.Int32 releaseCount) [0x0004c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:795 at System.Threading.SemaphoreSlim.Release () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/SemaphoreSlim.cs:760 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Reload () [0x00098] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:146 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00198] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:129 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x000b7] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:108 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/Foundation/NSAction.cs:178 at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/UIKit/UIApplication.cs:86 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/UIKit/UIApplication.cs:65 at Geometrics.AtomDownloader.iOS.Application.Main (System.String[] args) [0x00001] in C:\Project\XappsDev\AtomDownloaderXF\AtomDownloader.iOS\Main.cs:12 } | System.Threading.SemaphoreFullException

@michaelstonis
Copy link

We've just updated a handful of apps to 4.8, from 4.4 and 4.5, with no other NuGet packages and we are seeing the same issues.

bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 11, 2020
@softlion
Copy link
Contributor Author

Still not fixed in Xamarin Forms 5 prerelease

image

@softlion
Copy link
Contributor Author

softlion commented Nov 19, 2020

@bruzkovsky have you updated your clone to apply the fix the xamarin forms 5 prerelease ?

I tried to checkout the 5 branch and apply your fix but after that building fails:

"C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.sln" (Build cible) (1) ->
"C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.ControlGallery.WindowsUniversal\Xamarin.Forms.ControlGalle
ry.WindowsUniversal.csproj" (cible par défaut) (27) ->
(_ComputeAppxPackagePayload cible) ->
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\AppxPackage\Micro
soft.AppXPackage.Targets(1943,5): error APPX1111: Payload contains two or more files with the same destination path 'Xa
marin.Forms.Xaml.pdb', but they are different sizes. Source files:  [C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xama
rin.Forms.ControlGallery.WindowsUniversal\Xamarin.Forms.ControlGallery.WindowsUniversal.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\AppxPackage\Microso
ft.AppXPackage.Targets(1943,5): error APPX1111: C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.Controls\bi
n\Debug\netstandard2.0\Xamarin.Forms.Xaml.pdb [C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.ControlGalle
ry.WindowsUniversal\Xamarin.Forms.ControlGallery.WindowsUniversal.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\AppxPackage\Microso
ft.AppXPackage.Targets(1943,5): error APPX1111: C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.Platform.UA
P\bin\Debug\uap10.0.14393\Xamarin.Forms.Xaml.pdb [C:\Dev\repos\_clones\Xamarin.Forms.Bruzkovsky\Xamarin.Forms.ControlGa
llery.WindowsUniversal\Xamarin.Forms.ControlGallery.WindowsUniversal.csproj]

@bruzkovsky
Copy link
Contributor

Hi @softlion , sorry for the delay, busy week...
I still have my fix targeted on 4.8, but I think I'll have to switch to 5.0.0 to get my PR merged anyway. Your error looks like it could be solved by restarting VS or your machine. Did you succeed in applying the change?

bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 27, 2020
…ItemsSource

- refactored ObservableItemsSource and ObersvableGroupedSource to not use SemaphoreSlim
- ObservableItemsSource and ObservableGroupedSource now use a copy of the original items source to keep it in sync with the state of the UICollectionView
- fixes xamarin#11853
bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 27, 2020
…ItemsSource

- refactored ObservableItemsSource and ObservableGroupedSource to not use SemaphoreSlim
- ObservableItemsSource now uses a copy of the original items source to keep it in sync with the state of the UICollectionView
- fixes xamarin#11853
bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 27, 2020
…ItemsSource

- refactored ObservableItemsSource and ObservableGroupedSource to not use SemaphoreSlim
- ObservableItemsSource now uses a copy of the original items source to keep it in sync with the state of the UICollectionView
- fixes xamarin#11853
bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 27, 2020
…ItemsSource

- refactored ObservableItemsSource and ObservableGroupedSource to not use SemaphoreSlim
- ObservableItemsSource now uses a copy of the original items source to keep it in sync with the state of the UICollectionView
- added warnings for using BatchUpdate on the UICollectionView
- fixes xamarin#11853
bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 27, 2020
…ItemsSource

- refactored ObservableItemsSource and ObservableGroupedSource to not use SemaphoreSlim
- ObservableItemsSource now uses a copy of the original items source to keep it in sync with the state of the UICollectionView
- added warnings for using BatchUpdate on the UICollectionView
- fixes xamarin#11853
@softlion
Copy link
Contributor Author

No I have not been able to fix the build issue.

About your workaround, it duplicates the content of the list. This is not optimal.

I do have an implementation of this for mvvmcross which is both efficient and safe.

It does freeze the list state between updates but does not duplicate the items.

@bruzkovsky
Copy link
Contributor

@softlion so please share it then :)

Strange that the items are duplicated. I tested the fix with a lot of rapid modifications to the list and didn't find any issue. But I still want to test it in our production app next week, maybe I'll find more errors there...

bruzkovsky added a commit to bruzkovsky/Xamarin.Forms that referenced this issue Nov 30, 2020
…ItemsSource- refactored ObservableItemsSource and ObservableGroupedSource to not use SemaphoreSlim- ObservableItemsSource now uses a copy of the original items source to keep it in sync with the state of the UICollectionView- added warnings for using BatchUpdate on the UICollectionView- fixes xamarin#11853
@mfmadsen
Copy link

mfmadsen commented Dec 2, 2020

We are seeing this issue as well using Xamarin.Forms 4.8.
@bruzkovsky Is a fix for this issue expected to be released anytime soon (either for Xamarin.Forms 4.8 (preferably) or for Xamarin.Forms 5)?

@mfmadsen
Copy link

mfmadsen commented Dec 3, 2020

I have now successfully build a custom nuget package based on newest 4.8.0 branch combined with updates from @bruzkovsky. This fixes the issue we saw. Looking forward to an official update/release though :-)

@softlion
Copy link
Contributor Author

softlion commented Dec 8, 2020

@mfmadsen could you share your github/branch with the updates ?

Is it https://github.com/mfmadsen/Xamarin.Forms ?

Ok i can't use it. I'm already using Xamarin.Forms 5 features.

@mfmadsen
Copy link

mfmadsen commented Dec 8, 2020

@softlion, yes that is the repo we are currently using to build our temporary custom nuget package for 4.8 Xamarin.Forms. I was never able to build the repo from @bruzkovsky directly, so instead I cloned the Xamarin.Forms repo and then replaced the contents of the three updated files in Xamarin.Forms.Platform.iOS/CollectionView (ListSource.cs, ObservableGroupedSource.cs and ObservableItemsSource.cs). After that I was able to build. It seems some changes was made in the original Xamarin.Forms repo that fixes the build issues we both were seeing when trying to build the branch from @bruzkovsky.

@softlion
Copy link
Contributor Author

softlion commented Dec 8, 2020

EDIT: the custom code they put in XamarinForms 5 pre5 don't work at all. The one in the 5.0.0 branch don't work at all too. Among the issues: crash when the view is on a page in the stack (ie: another page has been pushed), crash randomly, displays no item (related to offscreen composition too).

EDIT2: ok you really need me to fix this mess. Ok then i'll take 4 hours to fix this shit.

EDIT3:
Console log when no item is displayed (wrongly, as the collection does contain 1 item).
It's a timing problem. If i add Console.WriteLine calls or ConfigureAwait(false) it works ok.

*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:], UICollectionView.m:7088
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.

Also this version has a limitation to 1 item added at a time for animations to work. Otherwise it will simply reload the whole list, loosing the current scroll position. Making lazy loading impossible. Especially it does not support AddRange, RemoveRange, ReplaceRange, MoveRange. Nor collection change transactions.

EDIT 4: easy repro: the collection stays empty if the control is in a page which is not attached to a parent yet, event if the ObservableCollection has data, and stays empty even after the page is attached (for example as a Detail of a MasterDetail page).

@mfmadsen Good news the fix of your fix is in XamarinForms 5 pre5 (you used async for nothing in your fix).

git checkout tags/beta-5.0.0-pre5

And to compile it you need a fix to your VS as explained in this solution: https://developercommunity.visualstudio.com/content/problem/1256345/uwp-build-fails-after-update-to-1682-error-msb3774.html

hartez added a commit that referenced this issue Dec 11, 2020
…alls that

were messing up the internal UICollectionView bookkeeping; fixes #11853
@hartez hartez linked a pull request Dec 13, 2020 that will close this issue
2 tasks
@hartez hartez closed this as completed in 626f756 Dec 17, 2020
@Tommigun1980
Copy link

Tommigun1980 commented Dec 18, 2020

I wonder if #13126, ie filling an ObservableCollectionView is related to this, as it shares several characteristics (toggling IsVisible alters behavior, the ObservableCollection sends only one update event which doesn’t draw properly and so on).

Thank you @softlion for your work. It’s discouraging to see how many bugs you found in the “fixes”. Xamarin needs to step up their testing. It’s getting to a point where the expectation is that things won’t work :/

@Tommigun1980
Copy link

EDIT: the custom code they put in XamarinForms 5 pre5 don't work at all. The one in the 5.0.0 branch don't work at all too. Among the issues: crash when the view is on a page in the stack (ie: another page has been pushed), crash randomly, displays no item (related to offscreen composition too).

EDIT2: ok you really need me to fix this mess. Ok then i'll take 4 hours to fix this shit.

EDIT3:
Console log when no item is displayed (wrongly, as the collection does contain 1 item).
It's a timing problem. If i add Console.WriteLine calls or ConfigureAwait(false) it works ok.

*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:], UICollectionView.m:7088
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N10__cxxabiv116__shim_type_infoE, id, N10__cxxabiv117__pbase_type_infoE.

Also this version has a limitation to 1 item added at a time for animations to work. Otherwise it will simply reload the whole list, loosing the current scroll position. Making lazy loading impossible. Especially it does not support AddRange, RemoveRange, ReplaceRange, MoveRange. Nor collection change transactions.

EDIT 4: easy repro: the collection stays empty if the control is in a page which is not attached to a parent yet, event if the ObservableCollection has data, and stays empty even after the page is attached (for example as a Detail of a MasterDetail page).

@mfmadsen Good news the fix of your fix is in XamarinForms 5 pre5 (you used async for nothing in your fix).

git checkout tags/beta-5.0.0-pre5

And to compile it you need a fix to your VS as explained in this solution: https://developercommunity.visualstudio.com/content/problem/1256345/uwp-build-fails-after-update-to-1682-error-msb3774.html

@softlion The regressions you mentioned in the 5.0.0 branch, is there any chance you could make a new bug report about them as I think they’ll get buried as this has now been closed?

@softlion
Copy link
Contributor Author

@Tommigun1980 @hartez completely rewrote a fix, removing all the new code from above, and the semaphore and all other thing.

i have not tested it yet as I moved things in my customer app to workaround these issues.

i hope it’s quality is better! ;)

@Tommigun1980
Copy link

@Tommigun1980 @hartez completely rewrote a fix, removing all the new code from above, and the semaphore and all other thing.

i have not tested it yet as I moved things in my customer app to workaround these issues.

i hope it’s quality is better! ;)

Oh good to hear, really looking forward to testing those fixes as there's a good chance they will fix #13126 also! Thanks for the info, I appreciate it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview e/5 🕔 5 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎 t/bug 🐛
Projects
None yet
Development

Successfully merging a pull request may close this issue.