无法修改写入交易之外的托管对象 - 反应本机,领域


onPressFavourites(item) {
    let realm = Realm.open({
    path: RNFS.DocumentDirectoryPath +'/trees.realm',
    schema: [sightingSchema, treeSchema],
    schemaVersion: 7,
   }).then(realm => {
    if(item.favourites === 0) {
     realm.write(() => {
        item.favourites = 1;
        item.favouritesColour = '#91b54d';
    });
    } else {
        realm.write(() => {
      item.favourites = 0;
        item.favouritesColour = 'transparent';
          }); 
    }
        }); 
    alert(item.favourites)// Update a property value
    }

我正在尝试在单击按钮时更新域中的对象,但是我得到了错误

"可能的未经治疗的承诺拒绝(ID:0):错误:无法在写入交易之外修改托管对象。"

此代码在几天前正在工作,但现在正在丢下上述错误。

我仍在学习本地和领域的React React和Realm,但是根据我的理解和以下示例和领域文档,我使用了正确的代码,以便它可以正常工作。

编辑

似乎是领域和写入交易。我们能够找到一种修复它的回旋方式,但是现在更新在应用程序刷新之前不显示。

似乎在使用参数item定义所选领域对象时产生了错误Possible Unhandled Promise Rejection (id: 0): Error: Cannot modify managed objects outside of a write transaction.。但是,如果我们创建与item相关的变量,则不会产生该错误。有什么想法为什么会发生?

onPressFavourites(item) {
    //console.log(realm);
    let realm = Realm.open({
        path: RNFS.DocumentDirectoryPath +'/trees.realm',
        schema: [sightingSchema, treeSchema],
        schemaVersion: 7,
    }).then(realm => {
        let trees = realm.objects('TreeInfo');
        for(var i=0; i<trees.length;i++){
            if(trees[i].commonName == item.commonName){
                var chosen = trees[i];
                break;
            }  
        }
        console.log(chosen);
        if(item.favourites === 0) {
                realm.write(() => {
                    //item.favourites = 1;
                    //item.favouritesColour = '#91b54d';
                    chosen.favourites = 1;
                    chosen.favouritesColour = '#91b54d';
                });
        }
        else {
            realm.write(() => {
                chosen.favourites = 0;
                chosen.favouritesColour = 'transparent';
            }); 
        }
    });
    alert(item.favourites)// Update a property value
}

编辑

render() { 
    var treeList = this.state.trees;
    console.log(treeList);
    const {navigate} = this.props.navigation;
     //var  tree = treeList[0];
    return(
        <Container>
            <Header searchBar style={styles.searchBar}>
              <Item style={styles.searchBarInner}>
                <Icon name="ios-search" />
                <Input placeholder="Search" />
              </Item>
            </Header>
            <List dataArray={treeList}
                renderRow={(item) => 
                    <ListItem style={styles.ListItem} button={true}>
                        <ListButton item={item}
                            onSelect={(item) => this.saveTreeInfo(item)}
                            onSelectFavourites={(item) =>                               this.onPressFavourites(item)
                            }
                        /> 
                    </ListItem>
                }
            >
            </List>
        </Container>
    );
}

上面是item传递给onPressFavourites功能的地方。item是从Realm对象treelist数组生成的,并显示在列表中。

treelist数组来自下面显示的变量this.state.trees

filterContent(){
        Realm.open({
            path: RNFS.DocumentDirectoryPath +'/trees.realm',
            schema: [sightingSchema, treeSchema],
            schemaVersion: 7,
            }).then(realm => {
                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});   
                }
            });
    }

用英语答案:

treesrealm有关,因此您无法更改它。只需复制一个新数组:

let trees = realm.objects('TreeInfo');

var arr=[];
for (let i = 0;trees && i < trees .length; i++) {
  var item=trees[i];
  var item2={
    id: item.id,
     ....
  }
arr.push(item2);
}

您应该改进语法

let order = {...bla, bla,bla}
realm.write(() => {
            realm.create("order", order);
        });

这个树里面里面里面里面里里的一些,所以,所以不能,要不,要要复制一个个个数个,

让树= realm.objects('treeInfo');

var arr=[];
for (let i = 0;trees && i < trees .length; i++) {
  var item=trees[i];
  var item2={
    id: item.id,
     ....
  }
arr.push(item2);
}

相关内容

  • 没有找到相关文章

最新更新