【问题标题】:Access AsyncStorage from native code从本机代码访问 AsyncStorage
【发布时间】:2015-12-29 16:21:27
【问题描述】:

我需要使用 JS 代码中的 AsyncStorage 将数据持久化到本地存储中。

我想知道是否有办法从本机代码(Objective-C 或 Java)访问从 AsyncStorage 存储的数据。

谢谢!

【问题讨论】:

标签: android ios react-native


【解决方案1】:

如果导入RCTAsyncLocalStorage.h,可以调用_getValueForKeyhttps://github.com/facebook/react-native/blob/master/React/Modules/RCTAsyncLocalStorage.m#L266

【讨论】:

  • 因为_getValueForKey是私有方法,我尝试调用:[asyncLocalStorage multiGet:@[@"USER_MESSENGER_SESSION"] callback:^(NSArray *response){ if ([response count] > 0) { NSDictionary *userMessengerSession = response[0]; if (userMessengerSession) { [[SinchClientManager sharedManager].sinchService logInUserWithId:userMessengerSession[@"user"][@"id"]]; } } }];但是我有一个SIGABRT
  • @draw 我是这样搞定的,但它充其量只是 hacky:RCTAsyncLocalStorage *asyncLocalStorage = [[RCTAsyncLocalStorage alloc] init]; dispatch_async([asyncLocalStorage methodQueue], ^{ [asyncLocalStorage _ensureSetup]; NSDictionary *err = [[NSDictionary alloc] init]; NSString* val = [asyncLocalStorage _getValueForKey:@"@foobar" errorOut:&err]; if (err) { } });
猜你喜欢
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 2011-07-06
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多