删除绑定到 ListView 的领域对象时崩溃

Crash when deleting a realm object bound to a ListView

我有一个带有 ListView 的 Xamarin XAML 页面。列表数据源链接到一个用领域对象填充的可观察集合。

当我尝试从 ListView 中删除一个对象时(首先从数据源,然后从领域)我遇到了崩溃。

崩溃仅在 iOS 的绑定时发生,内部异常消息为:

This object is detached. Was it deleted from the realm?

所以,删除后,似乎仍然从ListView访问该对象。

堆栈跟踪:

  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00050] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:313 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.TryGetValue (System.Object source, System.Object& value) [0x00043] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:498 
  at Xamarin.Forms.BindingExpression.Unapply () [0x00030] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:90 
  at Xamarin.Forms.Binding.Unapply () [0x0000e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Binding.cs:142 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext, System.Boolean skipBindingContext) [0x0002e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:407 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:123 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x0005a] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:117 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.Grid.OnBindingContextChanged () [0x00006] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Grid.cs:139 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.Grid.OnBindingContextChanged () [0x00006] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Grid.cs:139 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.Cell.OnBindingContextChanged () [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Cells\Cell.cs:117 
  at Xamarin.Forms.BindableObject.BindingContextPropertyBindingPropertyChanged (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0001b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:429 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:531 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:83 
  at Xamarin.Forms.BindableObject.set_BindingContext (System.Object value) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:24 
  at Xamarin.Forms.TemplatedItemsList`2+<UnhookItem>d__153[TView,TItem].MoveNext () [0x000a7] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:1210 

InnerException 堆栈跟踪:

  at Realms.NativeException.ThrowIfNecessary (System.Func`2[T,TResult] overrider) [0x00014] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at Realms.RowHandle.get_RowIndex () [0x00009] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at Realms.RealmObject.GetObjectValue[T] (System.String propertyName) [0x00000] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at TestProject.StockApp.Data.ManagedObjects.ArticleEntity.get_Article () [0x00006] in <bc12041acc564a39b37d8658934e3bc3>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:305

更新

似乎只有当 ListView 中使用的 Realm 对象具有也显示到 ListView 单元格的关系时,才会出现错误。

例如,如果我有一个 Dogs 列表,但在单元格中我还显示了 Owner .FirstName,那么当我删除选定的狗项时会发生崩溃。

public class Dog : RealmObject 
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person Owner { get; set; }
}

public class Person: RealmObject 
{
    public string FirstName{ get; set; }
}

The list data source is linked to an observable collection which is populated with realm objects

该可观察集合是如何更新的?

堆栈跟踪在 Forms 中很深 - 看起来这是他们代码工作方式的一个怪癖 - 你能把源代码发送到 support@realm.io 我会看看它。

v0.81.0 开始,RealmObject 隐含地是一个 ObservableObject,因此它会在属性上触发 INotifyPropertyChanged,在 IList 上触发 INotifyCollectionChanged,并且IQueryable 个属性。

更多信息: