question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing object of type X which has been invalidated or deleted

See original GitHub issue

Even though the .isValid method is present in both collection and object, neither of them is doing anything to prevent this error.

I tried using Collection.isValid, Object.isValid, Results.snapshot … nothing works

I’m rendering the Results on one view and if I delete one Object in another view and then return it throws this error.

photo_2017-05-24_11-37-18

here is my code for the 2 views

import React, { Component } from 'react';
import {
  InteractionManager
} from 'react-native';
import {
  Body,
  Button,
  Container,
  Content,
  Fab,
  Footer,
  FooterTab,
  Header,
  Icon,
  Input,
  Item,
  Left,
  List,
  ListItem,
  Right,
  Text,
  Title,
  View
} from 'native-base';
import realm from '../realm';

class CharactersScreen extends Component {

  constructor (props) {
    super(props);

    this.characters = realm.objects('Character');
    this.state = {
      searchBar: {
        active: false
      },
      characters: this.characters
    };
  }

  navigateToAddCharacter = () => {
    let { navigate } = this.props.navigation;

    navigate('AddCharacter');
  }

  static navigationOptions = {
    title: 'Characters',
    headerRight: <View style={{ flexDirection: 'row' }}>
      <Button transparent><Icon name="md-search"/></Button>
      <Button transparent><Icon name="md-funnel"/></Button>
    </View>
  }

  listenToRealm = (name, changes) => {
    this.setState({
      characters: this.characters
    });
  }

  componentWillMount () {
    InteractionManager.runAfterInteractions(() => {
      this.characters.addListener(this.listenToRealm);
    });
  }

  componentWillUnmount () {
    this.characters.removeListener(this.listenToRealm);
  }

  render () {
    let { navigate } = this.props.navigation;
    let {
      searchBar,
      characters
    } = this.state;

    return (
      <Container>
        {
          searchBar.active ?
            <Header searchBar rounded>
              <Item>
                <Icon name="md-search" />
                <Input placeholder="Search" />
                <Icon name="md-people" />
              </Item>
              <Button transparent>
                <Text>Search</Text>
              </Button>
            </Header> : null
        }
        <Content>
          <List>
            {
              characters.map((character, index) => {
                return (
                  <ListItem
                    key={index} onPress={navigate.bind(null, 'CharacterDetails', {
                      character
                    })}>
                    <Body>
                      <Text>{character.name}</Text>
                      <Text note>{character.note}</Text>
                    </Body>
                    <Right>
                      <Icon name="md-arrow-round-forward" />
                    </Right>
                  </ListItem>
                );
              })
            }
          </List>
        </Content>
        <Fab
          active={false}
          direction="top"
          containerStyle={{ marginLeft: 10 }}
          style={{ backgroundColor: '#5067FF' }}
          position="bottomRight"
          onPress={this.navigateToAddCharacter}>
            <Icon name="md-add" />
        </Fab>
      </Container>
    );
  }
}

export default CharactersScreen;

Here is the view where I delete the object

import React, { Component } from 'react';
import {
  Alert
} from 'react-native';
import {
  Body,
  Button,
  Card,
  CardItem,
  Container,
  Content,
  Icon,
  Left,
  Right,
  Text
} from 'native-base';
import realm from '../realm';

class CharacterDetailsScreen extends Component {

  static navigationOptions = {
    title: 'Character: Details'
  }

  promptDelete = () => {
    Alert.alert(
      'Delete Character',
      'Deleting is irreversible, are you sure?',
      [
        { text: 'Cancel', onPress: () => false },
        { text: 'OK', onPress: () => this.deleteCharacter() }
      ],
      { cancelable: false }
    );
  }

  deleteCharacter = () => {
    let {
      state: {
        params: {
          character
        }
      }
    } = this.props.navigation;
    let { goBack } = this.props.navigation;

    realm.write(() => {
      realm.delete(character);
    });

    goBack();
  }

  render () {
    let {
      navigate,
      state: {
        params: {
          character
        }
      }
    } = this.props.navigation;

    return (
      <Container>
        <Content>
          <Card>
            <CardItem header>
              <Left>
                <Icon name={character.favorite ? 'md-star' : 'md-person'}/>
                <Body>
                  <Text>{character.name}</Text>
                  <Text note>{character.note}</Text>
                </Body>
              </Left>
            </CardItem>
            <CardItem>
              <Body>
                <Text>{character.description}</Text>
              </Body>
            </CardItem>
            <CardItem>
              <Body>
                <Text>Status</Text>
                <Text note>{character.status}</Text>
              </Body>
            </CardItem>
            <CardItem>
              <Left>
                <Button onPress={this.promptDelete} transparent>
                  <Icon name="md-close" style={{ color: '#FF0000' }} />
                  <Text style={{ color: '#FF0000' }}> Delete</Text>
                </Button>
              </Left>
              <Right>
                <Button transparent>
                  <Icon name="md-create" />
                  <Text> Edit</Text>
                </Button>
              </Right>
            </CardItem>
          </Card>
        </Content>
      </Container>
    );
  }
}

export default CharacterDetailsScreen;

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:19
  • Comments:63 (4 by maintainers)

github_iconTop GitHub Comments

42reactions
kunalqsscommented, Jul 17, 2018

This is how I solved this problem -

First I created a function in my utils package -

export function convertToArray(realmObjectsArray)
{
    let copyOfJsonArray = Array.from(realmObjectsArray);
    let jsonArray = JSON.parse(JSON.stringify(copyOfJsonArray));
    return jsonArray;
}

Then, I used it to query Realm -

...
let allServicePlans = realm.objects(ScheduleSchema.name);
allServicePlans = convertToArray(allServicePlans);
return allServicePlans;
...

Haven’t noticed any performance issues till now, and have also gotten rid of that error for now it seems.

39reactions
martijngerritscommented, Sep 27, 2017

I had this issue as wel. The reason why was that I was referencing to an object of the Realm DB in a list view. In my particular case the solution was creating a clone of the object to use in the list: Object.assign({}, realmObject);

This way when a row was deleted all references within the row pointed to that cloned object instead of an object from realm that did not exist anymore.

Maybe this is of some use for someone 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

realm.js - Accessing object of type Contact which has been ...
Here is the code I'm using, which, after delete get an exception - "Accessing object of type Contact which has been invalidated or...
Read more >
Procedure when object is invalidated - Realm - MongoDB
The Object has been deleted or invalidated error will occur if an object has been deleted from a Realm, but you subsequently try...
Read more >
Accessing object of type info_data which has been invalidated ...
Coding example for the question Accessing object of type info_data which has been invalidated or deleted realm in react native-React Native.
Read more >
iOS : Realm object has been deleted or invalidated - YouTube
iOS : Realm object has been deleted or invalidated [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] iOS : Realm object has...
Read more >
DROP statement - IBM
Whenever an object is deleted, its description is deleted from the ... Note that if an event monitor has been previously activated using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found