我有一個 Xamarin.Forms 應用程式,在我的崩潰日志中,我不斷看到此錯誤的變化:
SIGABRT: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (60) must be equal to the number of rows contained in that section before the update (60), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
這個特定的串列視圖是一個聊天組件,我使用 ObservableCollection 并在收到訊息時使用 SignalR 添加訊息條目。相關代碼如下所示:
public ObservableCollection<MessageModel> Messages {
get {
return _messages;
}
set {
_messages = value;
OnPropertyChanged();
}
}
Messages = new ObservableCollection<MessageModel>();
hubConnection.On<long, string, string, long, long>("postChat", (id, name, message, userId, eventId) => {
Messages.Add(new MessageModel() { Id = id, UserId = userId, User = name, Message = message, IsSystemMessage = false, IsOwnMessage = userId == currentUserId });
this.MessageReceived.Invoke(this, new EventArgs());
});
感覺可能是執行緒問題,因為理論上可以同時進行聊天,但我不知道在哪里看。這是完整的 StackTrace
CoreFoundation
__exceptionPreprocess
libobjc.A.dylib
objc_exception_throw
CoreFoundation
[NSException raise:format:arguments:]
Foundation
-[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
UIKitCore
-[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:]
UIKitCore
-[UITableView _endCellAnimationsWithContext:]
UIKitCore
-[UITableView endUpdatesWithContext:]
BehaviorLive.iOS
wrapper_managed_to_native_ObjCRuntime_Messaging_objc_msgSend_intptr_intptr_11
BehaviorLive.iOS
UITableView.g.cs:292
BehaviorLive.iOS
Xamarin_Forms_Platform_iOS_ListViewRenderer__c__DisplayClass54_0__InsertRowsb__0 (D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:638)
BehaviorLive.iOS
Xamarin_Forms_Platform_iOS_ListViewRenderer_InsertRows_int_int_int (D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:642)
BehaviorLive.iOS
Xamarin_Forms_Platform_iOS_ListViewRenderer_UpdateItems_System_Collections_Specialized_NotifyCollectionChangedEventArgs_int_bool (D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:588)
BehaviorLive.iOS
Xamarin_Forms_Platform_iOS_ListViewRenderer_OnCollectionChanged_object_System_Collections_Specialized_NotifyCollectionChangedEventArgs (D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:386)
BehaviorLive.iOS
Xamarin_Forms_Internals_TemplatedItemsList_2_TView_REF_TItem_REF_OnCollectionChanged_System_Collections_Specialized_NotifyCollectionChangedEventArgs (D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:771)
BehaviorLive.iOS
Xamarin_Forms_Internals_TemplatedItemsList_2_TView_REF_TItem_REF_OnProxyCollectionChanged_object_System_Collections_Specialized_NotifyCollectionChangedEventArgs (D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:972)
BehaviorLive.iOS
Xamarin_Forms_ListProxy_OnCollectionChanged_System_Collections_Specialized_NotifyCollectionChangedEventArgs (D:\a\1\s\Xamarin.Forms.Core\ListProxy.cs:232)
BehaviorLive.iOS
Xamarin_Forms_ListProxy__c__DisplayClass34_0__OnCollectionChangedb__0 (D:\a\1\s\Xamarin.Forms.Core\ListProxy.cs:208)
BehaviorLive.iOS
NSAction.cs:152
BehaviorLive.iOS
wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr
BehaviorLive.iOS
mono_jit_runtime_invoke mini-runtime.c:3164
BehaviorLive.iOS
mono_runtime_invoke_checked object.c:3052
BehaviorLive.iOS
mono_runtime_invoke object.c:3107
BehaviorLive.iOS
native_to_managed_trampoline_11(objc_object*, objc_selector*, _MonoMethod**, unsigned int) registrar.m:400
BehaviorLive.iOS
-[__MonoMac_NSAsyncActionDispatcher xamarinApplySelector] registrar.m:8726
Foundation
__NSThreadPerformPerform
CoreFoundation
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
CoreFoundation
__CFRunLoopDoSource0
CoreFoundation
__CFRunLoopDoSources0
CoreFoundation
__CFRunLoopRun
CoreFoundation
CFRunLoopRunSpecific
GraphicsServices
GSEventRunModal
UIKitCore
-[UIApplication _run]
UIKitCore
UIApplicationMain
BehaviorLive.iOS
wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr
BehaviorLive.iOS
UIApplication.cs:86
BehaviorLive.iOS
UIApplication.cs:65
BehaviorLive.iOS
BehaviorLive_iOS_Application_Main_string__ <unknown>:1
BehaviorLive.iOS
wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr
BehaviorLive.iOS
mono_jit_runtime_invoke mini-runtime.c:3164
BehaviorLive.iOS
mono_runtime_invoke_checked object.c:3052
BehaviorLive.iOS
mono_runtime_exec_main_checked object.c:0
BehaviorLive.iOS
mono_jit_exec driver.c:1383
BehaviorLive.iOS
xamarin_main monotouch-main.m:493
BehaviorLive.iOS
main main.m:292
libdyld.dylib
start
uj5u.com熱心網友回復:
當您訪問系結到 UI 視圖的 ObservableCollection 時,請確保您在 MainThread 上。
試驗
MainThread.IsMainThread除錯測驗程序中,拋出例外,如果不上線(但你以為所有的呼叫代碼已經在該執行緒)。將代碼包裝在
Device.BeginInvokeOnMainThread. 這樣做可確保您不會在 UI 更新程序中更改集合:
.
Device.BeginInvokeOnMainThread( () => {
.. do something with `Messages` collection ..
});
注意:BeginInvokeOnMainThread 獨立運行,因此在接下來的任何代碼出現之前,您不能依賴它“完成”。
如果這種“一勞永逸”不夠/不合適,那么您需要改用任務繼續。或者執行等效的手動操作 - 我沒有一個觸手可及的示例,但原則是您在主執行緒上運行,然后在完成該作業時呼叫一個 Action。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/359223.html
標籤:xaml xamarin.forms 信号员 可观察的集合
下一篇:UWP(Windows10應用程式)InkCanvasLoadAsync錯誤HRESULTE_FAIL已從對COM組件的呼叫回傳
