sunnylqm / react-native-storage

local storage wrapper for both react-native and browser. Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
MIT License
3.02k stars 268 forks source link

怎么获取到最新的值 #239

Closed hainanzhang closed 4 years ago

hainanzhang commented 4 years ago

先storage.load 获取过一次了,然后设置新的值,执行storage.save,后面storage.load获取到的都是久的值,关掉项目重新打开,就有了新的值

sunnylqm commented 4 years ago

贴代码

---原始邮件--- 发件人: "hainanzhang"<notifications@github.com> 发送时间: 2019年12月6日(周五) 晚上6:49 收件人: "sunnylqm/react-native-storage"<react-native-storage@noreply.github.com>; 抄送: "Subscribed"<subscribed@noreply.github.com>; 主题: [sunnylqm/react-native-storage] 怎么获取到最新的值 (#239)

先storage.load 获取过一次了,然后设置新的值,执行storage.save,后面storage.load获取到的都是久的值,关掉项目重新打开,就有了新的值

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

hainanzhang commented 4 years ago

页面A (是获取数据) 代码:

getSignal = () => {
        storage.load({
            key: 'signal',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then(ret => {
            console.log("信号源列表",ret)
            this.setState({
                SignalArr: ret
            }, () => {
                console.log("我state结束了", this.state.SignalArr)
            })
        }).catch(err => {
        });

    }

B页面(设置新的数据)

 storage.load({
                key: 'signal',
                autoSync: true,
                syncInBackground: true,
                syncParams: {
                    extraFetchOptions: {
                        // 各种参数

                    },
                    someFlag: true,
                },
            }).then(ret => {
                arr =ret
                if( this.props.data){   //修改信号源
                    arr.splice(this.props.index,1,{
                        name:this.state.name,
                        type:this.state.type,
                        channel_num:this.state.channel_num,
                        id:this.randomId(1,10000000)
                    })
                }else{   //新增信号源
                    arr.push({
                        name:this.state.name,
                        type:this.state.type,
                        channel_num:this.state.channel_num,
                        id:this.randomId(1,10000000)
                    })
                }

                storage.save({
                    key: 'signal',
                    data: arr,
                    expires: null
                }).then( ( ) =>{
                    // this.props.addSignalModalHide()

                    storage.load({
                        key: 'signal',
                        autoSync: true,
                        syncInBackground: true,
                        syncParams: {
                            extraFetchOptions: {
                                // 各种参数

                            },
                            someFlag: true,
                        },
                    }).then(ret => {
                        console.log("设置信号源后的",ret)
                    })

                    Actions.A()  //跳到 A页面
                })
            }).catch(err => {
                arr.push({
                    name:this.state.name,
                    type:this.state.type,
                    channel_num:this.state.channel_num,
                    id:this.randomId(1,10000000)
                })
                storage.save({
                    key: 'signal',
                    data: arr,
                    expires: null
                }).then( ( ) =>{
                    console.log("我设置了err")
                    this.props.addSignalModalHide()
                })
            });

image

sunnylqm commented 4 years ago

你这个嵌套太难读了,建议改成async/await再看看

hainanzhang commented 4 years ago

这是简化后的,在更新的页面可以获取到新的值,但是在前面已经获取过一次的,返回再次获取到的就不是新的值,还是老的值

image

A页面

storage.load({
            key: 'title',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then(ret => {
            console.log("a页面获取的到的:",ret)
            this.setState({
                title: ret
            })

        }).catch(err => {  

        });

B页面:

 titleChange = (text)=>{

        storage.save({
            key: 'title',
            data: text,
            expires: null
        })
        this.setState({
            title:text
        })
        storage.load({
            key: 'title',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then( (ret)=>{
            console.log( "B页面设置后的",ret)
        } ).catch( err =>{
        })

    }
hainanzhang commented 4 years ago

image

sunnylqm commented 4 years ago
  1. 建议用async/await语法
  2. 有没有可能new了多个storage的实例?
  3. 如果是B页面输入然后跳回A页面取值,那么没必要用storage传递,直接用回调更合适,更简单,性能也更好
hainanzhang commented 4 years ago

没有办法啊,数据只能存再本地,局域网软件的

sunnylqm commented 4 years ago

看你B页面的代码,不是textinput输入取值吗?这个跟本地局域网的有啥关系?

hainanzhang commented 4 years ago

需要textinput输入的值保留下来,下次打开app,直接读取这条数据

hainanzhang commented 4 years ago

谢谢你,想到解决方法了,可以在a页面设置就行了,不一定要在b页面设置