Jacob Harris
Jacob Harris

Reputation: 53

Where does React Native's AsyncStorage store its data?

I am new to the React-Native framework and was wondering where AsyncStorage stores the data on a device. I know it creates a serialized dictionary for small data storage and complete files for large data storage, but where do I find these files?

Upvotes: 5

Views: 2932

Answers (2)

manbearshark
manbearshark

Reputation: 141

The answer is in the react-native code, as suggested by a previous answer. The actual location on your mobile device will be determined by NSDocumentDirectory. Because Apple runs your app in a sandboxed environment, you only get access to the directories in you app bundle. See this SO question / answers for more detail on this topic: What is the documents directory (NSDocumentDirectory)?.

TL;DR: you can put any files in this directory, but there are some cases where this can be deleted, like when the user deletes your app and reinstalls it, or during OS upgrades. The directory itself is ~/Documents in the bundle, apparently though I am not sure why this matters that much.

The relevant code in react-native:

static NSString *RCTGetStorageDirectory()
{
  static NSString *storageDirectory = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
#if TARGET_OS_TV
    storageDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
#else
    storageDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
#endif
    storageDirectory = [storageDirectory stringByAppendingPathComponent:RCTStorageDirectory];
  });
  return storageDirectory;
}

Upvotes: 2

Austin Fatheree
Austin Fatheree

Reputation: 842

On IOS it looks like it puts in

static NSString *const RCTStorageDirectory = @"RCTAsyncLocalStorage_V1";
static NSString *const RCTManifestFileName = @"manifest.json";

static NSString *RCTGetStorageDirectory()
{
  static NSString *storageDirectory = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    storageDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    storageDirectory = [storageDirectory stringByAppendingPathComponent:RCTStorageDirectory];
  });
  return storageDirectory;
}

Upvotes: 1

Related Questions