Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typerror: withCustomKey is not working #18

Closed
sayem314 opened this issue Apr 21, 2020 · 13 comments
Closed

Typerror: withCustomKey is not working #18

sayem314 opened this issue Apr 21, 2020 · 13 comments

Comments

@sayem314
Copy link

Example code:

const MMKVwithEncryptionKey = new MMKVStorage.Loader().withEncryption().withCustomKey('encryptionKey').initialize();
@sayem314
Copy link
Author

.initialize() should not be async btw since there might be immediate .get* call after that line on app boot and await can only be used inside the function.

@ammarahm-ed
Copy link
Owner

Thanks for reporting this!

There is actually nothing such as withCustomKey, that is a bug in documentation. I will fix it soon.

Use this:

const MMKVwithEncryptionKey = new MMKVStorage.Loader().withEncryption().encryptWithCustomKey('encryptionKey').initialize();

@ammarahm-ed
Copy link
Owner

Without async the database will not be initialized when you make the get call. That is the other problem.

@sayem314
Copy link
Author

Thanks, understood. I'm using it this way now.

// src/store/mmkv.js
import MMKVStorage from 'react-native-mmkv-storage';

class MMKV {
  constructor() {
    this.instance = new MMKVStorage.Loader().withEncryption();
    this.MMKV = null;
  }

  initialize = async () => {
    this.MMKV = await this.instance.initialize();
  };

  setMapAsync = async (name, value) => {
    return this.MMKV.setMapAsync(name, value);
  };

  getMapAsync = async (name) => {
    return this.MMKV.getMapAsync(name);
  };
}

export default new MMKV();
// App.js
import 'react-native-gesture-handler';
import React, {useEffect} from 'react';
import {View, StyleSheet} from 'react-native';
import RNBootSplash from 'react-native-bootsplash';
import AppNavigator from './src/navigation/AppNavigator';
import {MMKV, dispatch} from './src/store';

export default function App() {
  useEffect(() => {
    MMKV.initialize().then(async () => {
      try {
        const settings = await MMKV.getMapAsync('settings');
        dispatch({type: 'settings', settings});
      } catch (err) {
        console.log(err.message);
      } finally {
        RNBootSplash.hide({duration: 250}); // fade
      }
    });
  }, []);

  return (
    <View style={styles.container}>
      <AppNavigator />
    </View>
  );
}
// other components
import {MMKV} from './src/store';

Btw thanks for the library, just what I wanted :)

@ammarahm-ed
Copy link
Owner

I will look into it and see if we can make the API directly exposed without async, because async is creating some problems especially with redux-persist etc.

@sayem314
Copy link
Author

It seems like data is stored inside the document directory, Is it possible to change it to somewhere else? My app is a file storing app, mmkv folder and sks stuff is being exposed to end users :(

image

@ammarahm-ed
Copy link
Owner

What folder is this? I mean the path? @sayem314

@sayem314
Copy link
Author

It's storing data inside DocumentDirectoryPath where users are supposed to put data. See https://github.com/itinance/react-native-fs#constants

@ammarahm-ed
Copy link
Owner

I don't think the document directory is exposed in any file manager directly. Its the App's document directory right? Then I don't understand why are you exposing this directory to the user?

@sayem314
Copy link
Author

In iOS there are no other directories for storing flat files, in fact after exposing iTunes file sharing this path is shared on both iTunes and files. On android it's possible to expose another directory but not for iOS. A while ago I have used realm and they don't seem to host on that, not sure where realm data is stored tho.

@ammarahm-ed
Copy link
Owner

can a folder be hidden? @sayem314

@sayem314
Copy link
Author

In-App I can hide it. But since my app downloads files and plays contents in-app I must expose it to the Files app and iTunes, no workaround for iOS. I can use realm but it adds about 5 MB and is meant for complex databases. If you can't change the path I would request you to add .(dot) before filenames and folder so it will be hidden by OS I think, and I would just hide in-app.

@ammarahm-ed
Copy link
Owner

@sayem314 I will look into it and see what we can do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants