title: Cannot modify managed objects outside of a write transaction - React Native, Realm
body: <pre><code>onPressFavourites(item) {
      let realm = Realm.open({
      path: RNFS.DocumentDirectoryPath +'/trees.realm',
      schema: [sightingSchema, treeSchema],
      schemaVersion: 7,
     }).then(realm =&gt; {
      if(item.favourites === 0) {
       realm.write(() =&gt; {
          item.favourites = 1;
          item.favouritesColour = '#91b54d';
      });
  
      } else {
          realm.write(() =&gt; {
        item.favourites = 0;
          item.favouritesColour = 'transparent';
            }); 
      }
          }); 
      alert(item.favourites)// Update a property value
      }
  </code></pre>
  
  <p>I am trying to update an object in the Realm when a button is clicked however I get the error </p>
  
  <p>"Possible Unhandled Promise Rejection (id: 0):
  Error: Cannot modify managed objects outside of a write transaction."</p>
  
  <p>This code was working a couple days ago but is now throwing the above error.</p>
  
  <p>I am still learning React Native and Realm but from my understanding and following examples and the Realm docs, I am using the correct code so it should work.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Seems the realm and write transactions were fine.
  We were able to find a roundabout way to fix it however now the updates don't display until the app is refreshed. </p>
  
  <p>It seems the error <code>Possible Unhandled Promise Rejection (id: 0):
  Error: Cannot modify managed objects outside of a write transaction.</code> was produced when using the argument <code>item</code> to define the chosen Realm Object. However, if we create a variable related to the <code>item</code>, it doesn't produce that error. Any ideas why this would happen?</p>
  
  <pre><code>onPressFavourites(item) {
      //console.log(realm);
      let realm = Realm.open({
          path: RNFS.DocumentDirectoryPath +'/trees.realm',
          schema: [sightingSchema, treeSchema],
          schemaVersion: 7,
      }).then(realm =&gt; {
          let trees = realm.objects('TreeInfo');
          for(var i=0; i&lt;trees.length;i++){
              if(trees[i].commonName == item.commonName){
                  var chosen = trees[i];
                  break;
              }  
          }
          console.log(chosen);
          if(item.favourites === 0) {
                  realm.write(() =&gt; {
                      //item.favourites = 1;
                      //item.favouritesColour = '#91b54d';
                      chosen.favourites = 1;
                      chosen.favouritesColour = '#91b54d';
                  });
          }
          else {
              realm.write(() =&gt; {
                  chosen.favourites = 0;
                  chosen.favouritesColour = 'transparent';
              }); 
          }
      });
      alert(item.favourites)// Update a property value
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>render() { 
      var treeList = this.state.trees;
      console.log(treeList);
      const {navigate} = this.props.navigation;
       //var  tree = treeList[0];
      return(
          &lt;Container&gt;
              &lt;Header searchBar style={styles.searchBar}&gt;
                &lt;Item style={styles.searchBarInner}&gt;
                  &lt;Icon name="ios-search" /&gt;
                  &lt;Input placeholder="Search" /&gt;
                &lt;/Item&gt;
              &lt;/Header&gt;
              &lt;List dataArray={treeList}
                  renderRow={(item) =&gt; 
                      &lt;ListItem style={styles.ListItem} button={true}&gt;
                          &lt;ListButton item={item}
                              onSelect={(item) =&gt; this.saveTreeInfo(item)}
                              onSelectFavourites={(item) =&gt;                               this.onPressFavourites(item)
                              }
                          /&gt; 
                      &lt;/ListItem&gt;
                  }
              &gt;
              &lt;/List&gt;
          &lt;/Container&gt;
      );
  }
  </code></pre>
  
  <p>Above is where <code>item</code> is being passed to the <code>onPressFavourites</code> function. <code>item</code> is being generated from an array of Realm Objects <code>treelist</code> and displayed in a list. </p>
  
  <p>The <code>treelist</code> array comes from the variable <code>this.state.trees</code> which is displayed below.</p>
  
  <pre><code>filterContent(){
          Realm.open({
              path: RNFS.DocumentDirectoryPath +'/trees.realm',
              schema: [sightingSchema, treeSchema],
              schemaVersion: 7,
              }).then(realm =&gt; {
                  let trees = realm.objects('TreeInfo');
                  let length = trees.length;
                  let treeRealm = trees.sorted('commonName');
  
                  console.log(this.state.leafEdges);
                  if (this.state.leafEdges === 'smooth') {
                      var smoothLeaf = treeRealm.filtered('leafEdges CONTAINS "Smooth"');
                      this.setState({trees:smoothLeaf});
                  }
                  if (this.state.leafEdges === 'toothed') {
                      var toothedLeaf = treeRealm.filtered('leafEdges CONTAINS "Toothed"');
                      this.setState({trees:toothedLeaf});
                  }
                  if (this.state.leafEdges === 'notsure') {
                      this.setState({trees:treeRealm});
                  }
                  else if (this.state.leafEdges === 'null') {
                      this.setState({trees:treeRealm});   
                  }
              });
      }
  </code></pre>