【问题标题】:sync data with remote db when using asyncStorage使用 asyncStorage 时与远程数据库同步数据
【发布时间】:2017-05-16 16:04:51
【问题描述】:

我刚刚开始使用 react-native 并考虑将其用于离线优先应用程序。我将使用 asyncStorage 作为我的本地存储,因为它可以满足我对这个应用程序的所有需求。但是,我一直在寻找类似原生 android 的 Sync Adapter 这样的东西,它允许您根据各种条件自动将数据从本地存储传输到远程 DB。

是否有任何库可以轻松导入到我的 RN 项目中以帮助进行数据同步。或者可能,我不需要任何外部模块,并且 asyncStorage API 已经支持数据同步到远程数据库。

无论哪种方式,我都想知道是否可以在 AsyncStorage 和远程数据库之间同步数据。谢谢。

【问题讨论】:

    标签: react-native react-native-android react-native-fbsdk


    【解决方案1】:

    让我们这样做:

    在 api 命中后,您将获得数据作为响应。

    import {AsyncStorage} from 'react-native'
    response={username:'xyz',access_token:'jivejije'};
    
    //to save data 
    try {
          await AsyncStorage.setItem(username, response.username);
    }
    catch (error) {
          console.log("error is",error.message);
        }
    
    //to get data
    
    try {
      const name=await AsyncStorage.getItem(username);
      console.log('stored data',name)
    }
    catch (error) {
          console.log("error is",error.message);
        }
    

    为了与远程数据库同步,我希望您使用 react 本机存储(第三方)https://github.com/sunnylqm/react-native-storage

    【讨论】:

    • 感谢您的回复。我认为你没有理解我的问题。我正在寻找与远程数据库实现的数据同步,而不是写入或从本地存储中获取。
    • 这回答了我更普遍的问题:stackoverflow.com/questions/52045560/…
    猜你喜欢
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多