Tencent / MMKV

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
Other
17.39k stars 1.91k forks source link

[Feature Request] Add a Multi-process Callback/listener being notified in all processes when a change is done in any of them #1387

Closed dimitar71 closed 2 months ago

dimitar71 commented 2 months ago

MMKV for Android is great supporting multiple processes.

One important feature is missing though: the ability to notify a listener across all processes when a change is done (add/remove/delete). Something of the vicinity of SharedPreferences.OnSharedPreferenceChangeListener or LiveData or alike.

Not sure if there is even a plan for it, at least for Android.

But honestly, there is literally no library around which supports this. Except Harmony - but it does not work in some cases.

lingol commented 2 months ago

MMKV has something similar to this, it's called ContentChangeHandler. Note that it's not in realtime. The listener must do a CRUD or at least checkContentChanged() to trigger the callback.

dimitar71 commented 2 months ago

Yeah, this will require a while loop or a count timer or alike. Which will be read/write expensive, happening so often. Also, not seeing it for Android, maybe Win32-only?

lingol commented 2 months ago

It's available on most platforms (except OHOS). For Android https://github.com/Tencent/MMKV/blob/master/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java#L1647-L1648

dimitar71 commented 2 months ago

Thank you!

Spent some time with it. Extended MMKV, and overrode checkContentChangedByOuterProcess(),

It is going to be an overkill, since it does not provide the key names for changed values. We have around 300 items stored. Getting all values every time there is a change may be an overkill, especially for long JSON strings.

Is there a possibility to know which keys have been updated?

E.g. checkContentChangedByOuterProcess(changedKeys: List<String>)

lingol commented 2 months ago

It's impossible. Even if we did, it won't be accurate because the file might be full write back by the writer, and there's no way to know which key changes.

dimitar71 commented 2 months ago

Yes, I was suspecting such an answer.

Thank you for validating and confirming all this!