Reputation: 41
I am having this error "Accessing object which has been invalidated or deleted", i have gone through the solutions online but still cant find a solution to fix it, see code below
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');
}
});
}
and the call to the function
<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>
Upvotes: 4
Views: 1717
Reputation: 159
Try to retrieve the object first, and check if the object is valid or not then only perform the deletion.
var obj = realm.objects('track_info').filtered('track_id =' + t_id)
if(obj.isValid())
// perform deletion
else
// alert message
Upvotes: 0