Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-50

Accessing object of type X which has been invalidated or deleted

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      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.

      here is my code for the 2 views

      Unable to find source-code formatter for language: jsx. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

      Unable to find source-code formatter for language: jsx. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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;
      
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: