领域:删除特定对象

鲁沙卜·拉克霍里亚

我正在使用反应本机开发应用程序,其中我需要删除特定对象,这在过滤方法中给了我,但它给了我一个错误

“只能删除事务内的对象。”

这是我的代码

allObj1 = {
                id : 1,
                speed : "1",
                accuracy: "100",
                bearing: "1",
                longitude: "192",
                altitude: "1111",
                latitude: "1111",
                time: "11111",
                locationProvider: "2222",
            };

        allObj2 = {
                id : 2,
                speed : "1",
                accuracy: "100",
                bearing: "1",
                longitude: "192",
                altitude: "1111",
                latitude: "1111",
                time: "22222",
                locationProvider: "2222",
            };

        allObj3 = {
                id : 3,
                speed : "1",
                accuracy: "100",
                bearing: "1",
                longitude: "192",
                altitude: "1111",
                latitude: "1111",
                time: "333333",
                locationProvider: "2222",
            };

        realm.write(() => {
            realm.create('Location',allObj1 );          
            //realm.delete(firstObj);
            realm.create('Location',allObj2 );
            realm.create('Location',allObj3 );
        });         

        let locationO = realm.objects('Location');
        //let tanlocation = locationO.filtered('id >1 AND id <3 ');
        // Observe Collection Notifications         

        realm.objects('Location').filtered('id >=1 AND id <=3').addListener((tanlocation, changes) => {

            try{
                tanlocation.forEach((realmObj,index) => {                           
                    realm.delete(realmObj);             
                });
            }
            catch(err){
                console.log(err);
            }
        });


        // Unregister all listeners
        realm.removeAllListeners();

        //realm.delete(tanlocation);
        //console.log( tanlocation );

        console.log(locationO);

它引发了我一个错误,即“只能删除事务中的对象”。

有人遇到过这种问题吗?任何人都知道如何解决此问题或替代方法以实现上述功能

克里斯·海豚

必须将删除内容包装在中realm.write,就像使用一样realm.create

realm.write(() => {
    realm.delete(realmObj)
})

当我遇到这个问题时,这对我有用。只有在我阅读了Github评论后才意识到

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章