Merge pull request #1614 from hossinasaadi/dev-contentNotify

implement onMMKVContentLoadSuccessfully
This commit is contained in:
Guo Ling
2025-12-23 11:02:55 +08:00
committed by GitHub
6 changed files with 42 additions and 0 deletions
+15
View File
@@ -295,6 +295,7 @@ const string &MMKV::mmapID() const {
}
mmkv::ContentChangeHandler g_contentChangeHandler = nullptr;
mmkv::ContentLoadedHandler g_contentLoadedHandler = nullptr;
void MMKV::notifyContentChanged() {
if (g_contentChangeHandler) {
@@ -302,6 +303,12 @@ void MMKV::notifyContentChanged() {
}
}
void MMKV::notifyContentLoaded() {
if (g_contentLoadedHandler) {
g_contentLoadedHandler(m_mmapID);
}
}
void MMKV::checkContentChanged() {
SCOPED_LOCK(m_lock);
checkLoadData();
@@ -315,6 +322,14 @@ void MMKV::unRegisterContentChangeHandler() {
g_contentChangeHandler = nullptr;
}
void MMKV::registerContentLoadedHandler(mmkv::ContentChangeHandler handler) {
g_contentLoadedHandler = handler;
}
void MMKV::unRegisterContentLoadedHandler() {
g_contentLoadedHandler = nullptr;
}
void MMKV::clearMemoryCache(bool keepSpace) {
SCOPED_LOCK(m_lock);
if (m_needLoadFromFile) {
+5
View File
@@ -246,6 +246,7 @@ class MMKV_EXPORT MMKV {
#endif
void notifyContentChanged();
void notifyContentLoaded();
#if defined(MMKV_ANDROID) && !defined(MMKV_DISABLE_CRYPT)
void checkReSetCryptKey(int fd, int metaFD, const std::string *cryptKey, bool aes256);
@@ -648,6 +649,10 @@ public:
static void registerContentChangeHandler(mmkv::ContentChangeHandler handler);
static void unRegisterContentChangeHandler();
// called when content is loaded successfully
static void registerContentLoadedHandler(mmkv::ContentLoadedHandler handler);
static void unRegisterContentLoadedHandler();
// by default MMKV will discard all datas on failure
// return `OnErrorRecover` to recover any data from file
static void registerErrorHandler(mmkv::ErrorHandler handler);
+3
View File
@@ -181,6 +181,9 @@ typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErro
// doesn't guarantee real-time notification
typedef void (*ContentChangeHandler)(const std::string &mmapID);
// called when content is loaded successfully
typedef void (*ContentLoadedHandler)(const std::string &mmapID);
extern MMKV_EXPORT size_t DEFAULT_MMAP_SIZE;
#define DEFAULT_MMAP_ID "mmkv.default"
+1
View File
@@ -143,6 +143,7 @@ void MMKV::loadFromFile() {
}
auto count = m_crypter ? m_dicCrypt->size() : m_dic->size();
MMKVInfo("loaded [%s] with %zu key-values", m_mmapID.c_str(), count);
notifyContentLoaded();
// auto keys = allKeys();
// for (size_t index = 0; index < count; index++) {
// MMKVInfo("key[%llu]: %s", index, keys[index].c_str());
+4
View File
@@ -55,6 +55,10 @@ typedef NS_ENUM(NSUInteger, MMKVLogLevel) {
// doesn't guarantee real-time notification
- (void)onMMKVContentChange:(NSString *)mmapID;
// called when an MMKV file is loaded successfully.
// This is triggered only when MMKV actually opens and maps the file.
- (void)onMMKVContentLoadSuccessfully:(NSString *)mmapID;
@end
#endif /* MMKVHandler_h */
+14
View File
@@ -51,6 +51,7 @@ static BOOL g_isRunningInAppExtension = NO;
static void LogHandler(mmkv::MMKVLogLevel level, const char *file, int line, const char *function, NSString *message);
static mmkv::MMKVRecoverStrategic ErrorHandler(const string &mmapID, mmkv::MMKVErrorType errorType);
static void ContentChangeHandler(const string &mmapID);
static void ContentLoadedHandler(const string &mmapID);
@interface MMKVNameSpace ()
@@ -114,6 +115,9 @@ static BOOL g_hasCalledInitializeMMKV = NO;
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
mmkv::MMKV::registerContentChangeHandler(ContentChangeHandler);
}
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentLoadSuccessfully:)]) {
mmkv::MMKV::registerContentLoadedHandler(ContentLoadedHandler);
}
#if defined(MMKV_IOS) && !defined(MMKV_IOS_EXTENSION) && !defined(TARGET_OS_MACCATALYST)
// just in case someone forget to set the MMKV_IOS_EXTENSION macro
@@ -1090,6 +1094,9 @@ static NSString *md5(NSString *value) {
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
mmkv::MMKV::registerContentChangeHandler(ContentChangeHandler);
}
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentLoadSuccessfully:)]) {
mmkv::MMKV::registerContentLoadedHandler(ContentLoadedHandler);
}
}
+ (void)unregiserHandler {
@@ -1102,6 +1109,7 @@ static NSString *md5(NSString *value) {
mmkv::MMKV::unRegisterLogHandler();
mmkv::MMKV::unRegisterErrorHandler();
mmkv::MMKV::unRegisterContentChangeHandler();
mmkv::MMKV::unRegisterContentLoadedHandler();
}
+ (void)setLogLevel:(MMKVLogLevel)logLevel {
@@ -1258,6 +1266,12 @@ static void ContentChangeHandler(const string &mmapID) {
}
}
static void ContentLoadedHandler(const string &mmapID) {
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentLoadSuccessfully:)]) {
[g_callbackHandler onMMKVContentLoadSuccessfully:[NSString stringWithUTF8String:mmapID.c_str()]];
}
}
#pragma mark - MMKVNameSpace
@implementation MMKVNameSpace {