在React Native中访问已失效或删除领域的类型为info_data的对象

时间:2019-01-30 10:33:48

标签: react-native realm

我遇到此错误“正在访问已失效或删除的对象”, 我已经在线查看了解决方案,但仍然找不到解决方案, 参见下面的代码

deleteData(t_id) {
realm.write(() => {
            if (realm.objects('track_info').filtered('track_id =' + t_id).length > 0) {
                realm.delete(realm.objects('track_info').filtered('track_id =' + t_id));
                console.warn("REcord deleted");

               
                Alert.alert(
                    'Successes', 'deleted');
            } else {
               Alert.alert(
                   'Error', 'Record not found, or has been deleted already');
            }

         


        });

    }

以及对该函数的调用

<TouchableOpacity style={[styles.BtnSuccess,
                        { flex: 2, flexDirection: 'row' }]} onPress={this.deleteData(this.state.t_id)}>
                            <Text style={[styles.btnText, { flex: 1, alignItems: 'center', alignContent: 'center', alignSelf: 'center' }]}>Submit</Text>
                        </TouchableOpacity>

1 个答案:

答案 0 :(得分:0)

尝试首先检索对象,然后检查对象是否有效,然后仅执行删除操作。

var obj = realm.objects('track_info').filtered('track_id =' + t_id)
if(obj.isValid())
 // perform deletion
else
 // alert message