1

我在我的項目中使用react-native-navigation軟件包以及Realm。我的應用程序有一個名爲Notebook的領域對象,其中包含Verse對象的列表,這是另一個領域對象。創建新的Realm對象後導航錯誤

該應用的結構非常簡單,第一頁顯示的Notebook秒的列表,並且當選擇一個,應用程序轉換到第二屏幕這是Verse秒的列表。

這裏是我的代碼從筆記本列表中的詩句列表中導航:

this.props.navigator.push({ 
    screen: 'com.beyersapps.biblebinderrn.verselist', 
    title: notebook.name, 
    passProps: {notebook: notebook}, 
    animated: true, 
    animationType: 'fade', 
    backButtonTitle: undefined, 
    backButtonHidden: false 
}) 

此導航工作正常,我可以來回在兩個屏幕之間。當我創建一個新的Verse並將其添加到Notebook時,我的問題就出現了。這裏是我的代碼,住在第二屏幕來創建一個新的Verse

realm.write(() => { 
    let newVerse = realm.create('Verse', { 
    reference: 'Genesis 1:1', 
    favorite: false, 
    memorized: false, 
    scripture: 'My favorite verse' 
    }); 

    if (this.notebook != null) { 
    this.notebook.verses.push(newVerse); 
    } 
}); 

這是我的問題開始的地方。在這一點上,如果我選擇了後退按鈕返回到的Notebook是清單,然後再選擇一個筆記本,我得到這個錯誤:

Attempting to change value of a readonly property.

Exception in native call java.lang.RuntimeException: Error calling RCTEventEmitter.receiveTouches

at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
at android.os.Looper.loop(Looper.java:154)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:199)
at java.lang.Thread.run(Thread.java:761)
Caused by: com.facebook.jni.CppException: Exception calling object as function: TypeError: Attempting to change value of a readonly property.
at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) 
at android.os.Looper.loop(Looper.java:154) 
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:199) 
at java.lang.Thread.run(Thread.java:761) 

有幾件事情我可以做,使這問題消失,但都使我的應用程序無用。因爲它們可能在確定問題的幫助,雖然我可以從passProps刪除{notebook: notebook},當我瀏覽到新的屏幕(但當時沒有任何顯示的Verse列表屏幕上,因爲它不知道哪個Notebook選擇)。或者,我無法將新創建的Verse添加到所選的Notebook(但後來我無法添加數據)。

由於這兩個更改是在兩個不同的組件(Realm和react-native-navigation)中,我不確定哪個組件是問題的根源。

回答

1

作爲道具傳遞的對象(passProps)被React Native凍結。 嘗試改變:

passProps: {notebook: notebook} 

到:

passProps: {notebook: _.cloneDeep(notebook)} 

如果您需要反映在之前的畫面改變爲notebook,我想你應該通過商店這樣做。

另一種選擇是通過notebookId並通過id從狀態獲取正確的筆記本。

+0

我沒有使用'_.cloneDeep',而是傳遞了筆記本的主鍵並搜索了該ID的領域。現在一切都很好。謝謝! – beyerss