10

I need to persist data in local storage using AsyncStorage from JS code.

I would like to know if there is a way to access data stored from AsyncStorage from native code (Objective-C or Java).

Thanks!

Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67
  • If you/anyone are still looking for this. Check my answer here: https://stackoverflow.com/questions/37164887/can-i-access-data-stored-in-react-natives-asyncstorage-from-java-layer/47551790#47551790 – Muhammad Riyaz Nov 30 '17 at 11:56

1 Answers1

2

If you import RCTAsyncLocalStorage.h, you can call _getValueForKey: https://github.com/facebook/react-native/blob/master/React/Modules/RCTAsyncLocalStorage.m#L266

John Shammas
  • 2,687
  • 1
  • 17
  • 33
  • Because `_getValueForKey` is a private method, I tried to call: `[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"]]; } } }];` But I had a SIGABRT – Jean Lebrument Dec 31 '15 at 09:57
  • @draw I got it going like this, but it's hacky at best: `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) { } });` – Felix Schlitter Jan 05 '17 at 03:08