2017-05-28 37 views
1

會(真的)欣賞這一個幫助。
我有一個領域的listView,導航到一個詳細的視圖。
詳細視圖將從原始列表視圖中刪除條目並返回。我已經註冊了領域數據庫更改的偵聽器,以更新列表視圖的內容。
這裏是我正在使用的代碼,它在刪除後得到一個異常 - 導航發生後「訪問已被無效或刪除的Contact類型的對象」。
有沒有人有一個想法,爲什麼?
此外,它似乎更改偵聽器(updateContactsFromDB)被調用兩次,同時刪除只有一個對象 - 想法?realm.js - 訪問對象的類型聯繫已被無效或刪除與沒有堆棧跟蹤

10X

ContactPage.js: 

export default class ContactsPage extends Component { 
    updateContactsFromDB(){ 
    console.log("ContactsPage:updateContactsFromDB()"); 
    let contacts = Realm.objects('Contact'); 
    this.setState({ 
     dataSource: this.state.dataSource.cloneWithRows(contacts.snapshot()), 
    }); 
    } 
    constructor(props) { 
    console.log("ContactsPage:constructor()"); 
    super(props); 
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => (r1 !== r2)}); 
    let contacts = Realm.objects('Contact'); 
    this.state = { 
     dataSource: ds.cloneWithRows(contacts.snapshot()), 
    }; 
    this.updateContactsFromDB = this.updateContactsFromDB.bind(this); 
    } 
    componentWillMount(props){ 
    console.log("ContactsPage:componentWillMount"); 
    Realm.addListener('change', this.updateContactsFromDB); 
    } 
    componentWillUnmount(props){ 
    console.log("ContactsPage:componentWillUnmount"); 
    Realm.removeListener('change', this.updateContactsFromDB); 
    } 
    render() { 
    console.log("ContactsPage:render()"); 
    return (
      <ListView 
       dataSource={this.state.dataSource} 
       renderRow={(contact) => (
       <TouchableOpacity onPress={() => this.props.navigation.navigate('ContactNotesPage', { contact: contact}) }> 
        <Text>test Contact</Text> 
       </TouchableOpacity> 
      ) 
      } 
      /> 
    ); 
    } 
} 

ContactNotesPage.js: 

export default class ContactNotesPage extends Component { 
    constructor(props) { 
    console.log("ContactNotesPage:constructor"); 
    super(props); 
    } 
    render(){ 
    console.log("ContactNotesPage:render()"); 
    const { params } = this.props.navigation.state; 
    return (
     <TouchableOpacity onPress={() => { 
      console.log("ContactNotesPage:delete"); 
      Realm.write(() => { Realm.delete(params.contact);}); 
      this.props.navigation.navigate('ContactsPage'); 
     } 
     }> 
     <Text>DeleteContact</Text> 
     </TouchableOpacity> 
    ) 
    } 
}; 

// main.js 
const MainStack = StackNavigator({ 
    ContactsPage: { 
    screen: ContactsPage, 
    }, 
    ContactNotesPage:{ 
    screen: ContactNotesPage, 
    }, 
}); 

export default MainStack; 
+0

開放你可能想移動'this.updateContactsFromDB = this.updateContactsFromDB.bind(本);''來componentWillMount(道具){' – EpicPandaForce

+1

似乎是一個問題的時候:使用反應導航和領域,同時傳遞一個對象到導航,然後刪除這個對象。當傳遞contactID並使用contactID進行刪除時,它工作正常:let currContact = Realm.objects('Contact')。filtered('recordID =''+ params.contactID +'''); Realm.delete(currContact); – EyalS

+0

你試過我的猜測了嗎? – EpicPandaForce

回答