使用asyncStorage时与远程数据库同步数据 [英] sync data with remote db when using asyncStorage

查看:249
本文介绍了使用asyncStorage时与远程数据库同步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用react-native,并考虑将其用于脱机优先应用程序.我将使用asyncStorage作为本地存储,因为它可以满足我对该应用程序的所有需求.但是,我进行了毫无结果的搜索,寻找了诸如本机android的Sync Adapter之类的东西,该工具可让您根据各种条件自动将数据从本地存储传输到远程数据库.

I've just started using react-native and thinking of using it for an offline-first application. I will be using asyncStorage as my local storage as it tends to meet all my needs for this app. However, I've had a fruitless search looking for something like native android's Sync Adapter which allows you to automate data transfer from your local storage to a remote DB based on a variety of criteria.

我可以轻松地将任何库导入RN项目中以帮助进行数据同步.也许我不需要任何外部模块,并且asyncStorage API已经支持与远程数据库的数据同步.

Is there any library I can easily import into my RN project to help with data synchronization. Or Maybe, I don't need any external module, and the asyncStorage API already supports data synchronization to remote databases.

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

Either way, I'd like to know if it is possible to synchronize data between AsyncStorage and a remote database. Thank You.

推荐答案

让我们这样做:

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

after api hit you get the data as response.

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 native存储(第三方) https ://github.com/sunnylqm/react-native-storage

for sync with remote database i would prefer you to use react native storage(third party) https://github.com/sunnylqm/react-native-storage

这篇关于使用asyncStorage时与远程数据库同步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆