簡體   English   中英

Xamarin.Forms SignalR:無效更新:第 0 節中的行數無效

[英]Xamarin.Forms SignalR: Invalid update: invalid number of rows in section 0

我有一個 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

當您訪問綁定到 UI 視圖的 ObservableCollection 時,請確保您在 MainThread 上。

  • 在調試測試期間測試MainThread.IsMainThread ,如果不在該線程上則拋出異常(但您認為所有調用代碼都已經在該線程上)。

  • Device.BeginInvokeOnMainThread包裝代碼。 這樣做可確保您不會在 UI 更新過程中更改集合:

.

Device.BeginInvokeOnMainThread( () => {
    .. do something with `Messages` collection ..
});

注意:BeginInvokeOnMainThread 獨立運行,因此在接下來的任何代碼出現之前,您不能依賴它“完成”。

如果這種“一勞永逸”不夠/不合適,那么您需要改用任務繼續。 或者執行等效的手動操作 - 我沒有一個觸手可及的示例,但原則是您在主線程上運行,然后在完成該工作時調用一個 Action。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM