question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing stored data from native code?

See original GitHub issue

You want to:

From my iOS extension be able to access the data stored by my JavaScript code within the react native app.

Is it possible to call read/write functions from native code?

Exmaple: From javascript

import AsyncStorage from '@react-native-community/async-storage';
await AsyncStorage.setItem('count', 999);

and then, from my native code I want to call what would be equivalent to getItem('count') to access the stored data. In my case, from the iOS share extension which needs to access this data. Are there any working example of how to do this? Is it even possible?

Details:

Sorry for asking questions that are a bit outside the scope of this module. I know it is supposed to be react native context, but some documentation about how this work behind the scene would be fantastic.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
tido64commented, Sep 2, 2020

This is possible but requires some knowledge of how native modules work in general. I’m not sure how future-proof this is with regards to the upcoming refactoring work in React Native but this is how you could access it today:

// First, get the RNCAsyncStorage instance from the bridge
RNCAsyncStorage *asyncStorage = [bridge moduleForClass:[RNCAsyncStorage class]];

// Then call the getter:
[asyncStorage multiGet:@[@"count"]] callback:^(NSArray *response){
    NSObject *count = response[0];
    if ([count isKindOfClass:[NSError class]]) {
        // Failed to get count
        return;
    }

    // Use count here
}];

Another (arguably better) way to do this is to implement RNCAsyncStorageDelegate as described in this guide. You would have to handle storage yourself but this will allow your extensions to access data directly without knowledge of the bridge or even React Native. We will also strive to have this API stable in the future.

1reaction
tido64commented, May 2, 2019

@kodeusz: Unfortunately, I’m not as well-versed on the Android side as I’d like to be. From looking at the code, it seems the Android equivalent is ReactContext.getNativeModule(). There’s an example of it being used in UIManagerHelper. Hope that helps 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access data stored in AsyncStorage from ios native code ...
I've just been faced with the same problem. My solution was to move the code native side. On iOS: #import <React/RCTAsyncLocalStorage.h> ...
Read more >
Storing and Getting Data from Async Storage in React Native
In this video, we look at storing and getting data from async storage in react native for our todo app. Part 2. GET...
Read more >
A guide to React Native's AsyncStorage - LogRocket Blog
AsyncStorage is an unencrypted and asynchronous data storage system in React Native that allows users to persist data offline.
Read more >
Access documents and other files from shared storage
Access documents and other files from shared storage · On this page · Use cases for accessing documents and other files · Create...
Read more >
AsyncStorage - React Native
On iOS, AsyncStorage is backed by native code that stores small ... Flushes any pending requests using a single batch call to get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found