We recently upgraded our project to Xamarin 1.3.1. Since upgrading, many of our iOS ListViews have been giving us trouble. It seems like grouped lists (bound to ObservableCollection) work just fine, but regular lists (bound to ObservableCollection) crash occasionally.
While trying to solve the problem I came across this thread which solved some of the problems and I am currently using it in the code via the following extension method.
public static void SafeClear<T>(this ObservableCollection<T> collection)
{
while (collection.Count > 0) collection.RemoveAt(0);
}
Unfortunately, I'm now getting an ArgumentOutOfRange exception (Exception/Stacktrace at the bottom of the post) when adding multiple items to a list. This only seems to happen when the list already contains items and I clear (using the SafeClear method) and add items in.
For some context, we're implementing a simple type-ahead search box that filters a list based on a string in a SearchBar. When the user inputs a character, we clear the list and add all of the results to the list that contain the search string.
Here's what I've tried so far:
- The SafeClear method from this thread
- Using ObservableCollectionFast (which has solved a similar problem for some people) from this thread
- All combinations of these methods forced on the UI thread using Xamarin.Forms.Device.BeginInvokeOnMainThread.
I've seen that this issue seems to be resolved on Xamarin.Forms 1.3.2-pre1, but I'd like to stay on the stable channel if possible. If I absolutely can't resolve this issue without upgrading I'll do it, but I'd be grateful for any other solution.
Thanks in advance.
Exception:
Argument is out of range.
Parameter name: index;
StackTrace:
at Xamarin.Forms.ListProxy.get_Item (Int32 index) [0x00000] in <filename unknown>:0
at Xamarin.Forms.TemplatedItemsList`2[Xamarin.Forms.ItemsView`1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].get_Item (Int32 index) [0x00000] in <filename unknown>:0
at Xamarin.Forms.Platform.iOS.ListViewRenderer+ListViewDataSource.GetCellForPath (Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
at Xamarin.Forms.Platform.iOS.ListViewRenderer+UnevenListViewDataSource.GetHeightForRow (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
at UIKit.UITableView.EndUpdates () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/build/native/UIKit/UITableView.g.cs:279
at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateItems (System.Collections.Specialized.NotifyCollectionChangedEventArgs e, Int32 section, Boolean resetWhenGrouped) [0x00000] in <filename unknown>:0
at Xamarin.Forms.Platform.iOS.ListViewRenderer+<>c__DisplayClass5.<OnCollectionChanged>b__4 () [0x00000] in <filename unknown>:0
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSAction.cs:164
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46