sync data with remote db when using asyncStorage

北城以北 提交于 2019-12-23 12:35:55

问题


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.

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.

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


回答1:


Let's do it like this:

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);
    }

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



来源:https://stackoverflow.com/questions/41428683/sync-data-with-remote-db-when-using-asyncstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!