MMKV icon indicating copy to clipboard operation
MMKV copied to clipboard

[ENHANCEMENT] `MMKV.exists(..)` API

Open mrousavy opened this issue 1 year ago • 10 comments

Hey!

I was wondering if there's a way to find out if an MMKV instance with the given id exists or not?

Something like

auto id = "my-instance";
bool exists = MMKV::exists(id);
if (exists) {
  // load it, start app
} else {
  // show login page
}

This would maybe also open up other scenarios for e.g. crypto wallets, where we can check if an encrypted MMKV instance exists without knowing the encryption key.

mrousavy avatar Jul 10 '24 08:07 mrousavy

No there isn't one yet. Will you be kindly enough to make a PR to add one?

lingol avatar Jul 10 '24 08:07 lingol

Sure, can you give me some pointers on how I can find out if an instance exists? Try loading it? Looking for the file path? Some code pointers would be appreciated!

mrousavy avatar Jul 10 '24 09:07 mrousavy

You can follow the logic in removeStorage(). Checking the existence of the file and meta file should be enough.

lingol avatar Jul 10 '24 09:07 lingol

Created a draft PR here; https://github.com/Tencent/MMKV/pull/1347

Did not test yet, I'm still on the airport. Will test & finish it later.

mrousavy avatar Jul 10 '24 09:07 mrousavy

Hey @lingol sorry this is a bit unrelated but I didn't want to create a separate issue for my question; when I create an instance with an encryption key and write some data to it, and then try to open it again later with a different encryption key (or no encryption key at all), it will erase all data of that instance.

Is that intended behaviour? I would've expected it to throw some kind of error if the encryption key does not match..

mrousavy avatar Jul 11 '24 11:07 mrousavy

MMKV doesn't record the encryption key. So there's no way for MMKV to tell if the key is correct. Image that if MMKV does provide such functionality, doesn't that leave space for brute force encryption key cracking?

lingol avatar Jul 11 '24 12:07 lingol

Okay valid. Got it.

mrousavy avatar Jul 11 '24 12:07 mrousavy

Hey, is there any chance you can provide this feature this week? I'm considering a new release due to the encryption bug. I hope you can make it.

lingol avatar Jul 24 '24 07:07 lingol

Hey - sorry I got stuck up with some other work related to my camera library. I think I won't be able to finish it this week, I might be able to dedicated the time for that next week or the week after that.

mrousavy avatar Jul 24 '24 15:07 mrousavy

Of cause, take your time. We will release LTS version instead.

lingol avatar Jul 24 '24 23:07 lingol

Hey, it's been some time. I've decided to officially add this feature. Thanks for the idea.

lingol avatar Apr 22 '25 06:04 lingol

this is dope, thanks so much!

mrousavy avatar Apr 22 '25 08:04 mrousavy