简体   繁体   English

会话之间的React-native存储数据

[英]React-native store data between sessions

I need to store some user data, such as settings, login, token etc. between session. 我需要在会话之间存储一些用户数据,例如设置,登录,令牌等。 Tried asyncstorage but when I restart app, data in it gets deleted. 尝试了异步存储,但是当我重新启动应用程序时,其中的数据将被删除。 Found some modules that refers to keychain or NSUserDefaults, but I don't know which and any of it is good for this purpose. 找到了一些引用钥匙串或NSUserDefaults的模块,但我不知道哪个模块和任何模块都有用。
The main feature is that I can get that data even after app is killed and restarted. 主要功能是即使应用程序被杀死并重新启动后,我也可以获取该数据。 Mostly on iOS but if it would be also on Andorid, that would be great. 主要是在iOS上,但如果它也在Andorid上,那就太好了。
Anybody had some experience with it? 有人有过一些经验吗?

EDIT: 编辑:
I tried using asyncstorage. 我尝试使用asyncstorage。 But it doesn't do a thing when you kill and restart app. 但是当你杀死并重启应用程序时,它没有做任何事情。
My code for storing data: 我存储数据的代码:

 AsyncStorage.setItem("prefix", responseData['prefix'])
 AsyncStorage.setItem("api_token", responseData['api_token'])

And retrieving data: 并检索数据:

async _checkAutoLogin(){ 
    var prefix;
    var token;
    try { 
       prefix = await AsyncStorage.getItem("prefix");
       token = await AsyncStorage.getItem("api_token"); 
} catch (error) { 
      AlertIOS.alert('AsyncStorage error: ' + error.message); 
    } 

AsyncStorage should save your data to the device so that you can access it regardless of if your app has previously been sent to the background or has been cold launched. AsyncStorage应将您的数据保存到设备,以便您可以访问它,无论您的应用程序先前是否已发送到后台或已冷启动。 If you are trying to use the await keyword I think there is some extra setup that you will need to do to get it running. 如果您尝试使用await关键字,我认为您需要执行一些额外的设置才能使其运行。 Here's a good article to get you going with that. 这是一篇很好的文章 ,可以帮助您实现这一目标。

Also, depending on how much you intend on doing with AsyncStorage, the React Native site says this: 此外,根据您打算使用AsyncStorage的程度,React Native网站会说:

It is recommended that you use an abstraction on top of AsyncStorage instead of AsyncStorage directly for anything more than light usage since it operates globally. 建议您在AsyncStorage之上使用抽象而不是AsyncStorage直接用于除了轻量级用途以外的任何抽象,因为它全局运行。

I've written a small wrapper around it called react-native-simple-store which just wraps up AsyncStorage and exposes a minimalistic API, but gives you JSON.stringifying and JSON.parsing by default for more complex values so you don't have to worry about it. 我已经编写了一个名为react-native-simple-store的小包装器,它只包含AsyncStorage并公开了一个简约API,但默认情况下为JSON.stringifying和JSON.parsing提供更复杂的值,所以你没有担心它。

Or if you end up needing more of a local database there are other modules for that . 或者,如果您最终需要更多本地数据库, 那么还有其他模块

Hope that helps! 希望有所帮助!

https://facebook.github.io/react-native/docs/asyncstorage.html#content might do the trick. https://facebook.github.io/react-native/docs/asyncstorage.html#content可能会成功。 It says that it's persistent storage which probably means the quality you are looking for. 它说这是持久存储,这可能意味着你正在寻找的质量。

This works even after restarting the app: 这甚至在重新启动应用程序后仍然有效:

  _login:function(token){
    AsyncStorage.setItem('ApiToken',token,
      () => this.setState({isLoggedIn:true}),
      (error) => console.log(error)
    );
  },

还有Realm for React Native,它为您提供了完整的本地数据库: https//realm.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM