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

Question: What is the difference between react-native-mmkv & react-native-mmkv-storage ? #301

Closed
KJ-GM opened this issue Jan 24, 2022 · 9 comments

Comments

@KJ-GM
Copy link

KJ-GM commented Jan 24, 2022

I am wondering why do we have two libraries doing the same job, which one shall I use ?

@hirbod
Copy link
Contributor

hirbod commented Jan 24, 2022

Duplicate of #100

Using the search function isn't that hard :)

@mrousavy
Copy link
Owner

As @hirbod said, I explained the differences in detail here: #100 (comment)

@KJ-GM
Copy link
Author

KJ-GM commented Jan 25, 2022

As @hirbod said, I explained the differences in detail here: #100 (comment)

Hey, I have read the comparison section , I am really hesitant at this point, one thing that got my attention is that everything is synchronous in this library, does this make a difference ?

@mrousavy
Copy link
Owner

mrousavy commented Jan 25, 2022

everything is synchronous in this library, does this make a difference?

Well yes, it does.

Faster

It's faster than a Bridge call. By magnitudes. Like, really really fast.

Easy access

Consider the following code:

const isLoggedIn = storage.getBoolean('is-logged-in')
function App() {
  return isLoggedIn ? <Main /> : <Login />
}

This is fast, shows the correct UI (Main or Login) immediately, and does not cause any re-renders. This is how it works with my library.

Let's try to build the same UI with an asynchronous storage:

function App() {
  const [isLoggedIn, setIsLoggedIn] = useState(false)
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    (async () => {
      const data = await storage.getBooleanAsync('is-logged-in')
      setIsLoggedIn(data)
      setIsLoading(false)
    })()
  }, [])

  if (isLoading) return <Loading />
  return isLoggedIn ? <Main /> : <Login />
}

Also note that there is a downside with a synchronous API: When storing huge amounts of data, it is better to use an actual database like WatermelonDB or SQLite since MMKV is in-memory.

@KJ-GM
Copy link
Author

KJ-GM commented Jan 25, 2022

everything is synchronous in this library, does this make a difference?

Well yes, it does.

Faster

It's faster than a Bridge call. By magnitudes. Like, really really fast.

Easy access

Consider the following code:

const isLoggedIn = storage.getBoolean('is-logged-in')
function App() {
  return isLoggedIn ? <Main /> : <Login />
}

This is fast, shows the correct UI (Main or Login) immediately, and does not cause any re-renders. This is how it works with my library.

Let's try to build the same UI with an asynchronous storage:

function App() {
  const [isLoggedIn, setIsLoggedIn] = useState(false)
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    (async () => {
      const data = await storage.getBooleanAsync('is-logged-in')
      setIsLoggedIn(data)
      setIsLoading(false)
    })()
  }, [])

  if (isLoading) return <Loading />
  return isLoggedIn ? <Main /> : <Login />
}

Also note that there is a downside with a synchronous API: When storing huge amounts of data, it is better to use an actual database like WatermelonDB or SQLite since MMKV is in-memory.

Well, thank you for this part, I can see a big difference now, last thing I want to ask about is encryption, we are creating a password manager app where we need to store some secret key for local login usage, is the encryption reliable for this case? I did not really get how it works from docs, is it handled by the library or do I need to create my own keys ?

@mrousavy
Copy link
Owner

Here are the docs for Encryption: iOS & Android

Quote:

By default MMKV stores all key-values in plain text on file, relying on Android's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.

@KJ-GM
Copy link
Author

KJ-GM commented Jan 25, 2022

Okay, thank you, Does the data storage remain in the phone after uninstalling the app, or does it get deleted automatically? I hope that everything would be deleted? If not, what is the solution for this?

@mrousavy
Copy link
Owner

Everything will be deleted when the user uninstalls your app.

@thoth-seshat
Copy link

Can I encrypt without providing a key? Or do I always need to provide a key. I'm a bit new to this but I'm comparing this to SecureStore (with expo). SecureStore mentions it uses Keychain for iOS at least. I'm guessing this doesn't use keychain or anything like that correct?

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

4 participants