add onMMKVContentLoadSuccessfully to content notify handler

This commit is contained in:
hossinasaadi
2025-12-20 18:22:41 +03:30
parent c708914c04
commit df721fa6cd
6 changed files with 45 additions and 26 deletions
+9 -9
View File
@@ -294,11 +294,11 @@ const string &MMKV::mmapID() const {
return m_mmapID;
}
mmkv::ContentChangeHandler g_contentChangeHandler = nullptr;
mmkv::ContentNotifyHandler g_contentNotifyHandler = nullptr;
void MMKV::notifyContentChanged() {
if (g_contentChangeHandler) {
g_contentChangeHandler(m_mmapID);
void MMKV::notifyContent(MMKVContentNotifyType notifyType) {
if (g_contentNotifyHandler) {
g_contentNotifyHandler(m_mmapID, notifyType);
}
}
@@ -307,12 +307,12 @@ void MMKV::checkContentChanged() {
checkLoadData();
}
void MMKV::registerContentChangeHandler(mmkv::ContentChangeHandler handler) {
g_contentChangeHandler = handler;
void MMKV::registerContentNotifyHandler(mmkv::ContentNotifyHandler handler) {
g_contentNotifyHandler = handler;
}
void MMKV::unRegisterContentChangeHandler() {
g_contentChangeHandler = nullptr;
void MMKV::unRegisterContentNotifyHandler() {
g_contentNotifyHandler = nullptr;
}
void MMKV::clearMemoryCache(bool keepSpace) {
@@ -1486,7 +1486,7 @@ bool MMKV::restoreOneFromDirectory(const string &mmapKey, const MMKVPath_t &srcP
kv->clearMemoryCache();
kv->loadFromFile();
if (kv->isMultiProcess()) {
kv->notifyContentChanged();
kv->notifyContent(MMKVContentChanged);
}
MMKVInfo("finish restore one mmkv[%s], ret: %d", mmapKey.c_str(), ret);
+5 -4
View File
@@ -245,7 +245,7 @@ class MMKV_EXPORT MMKV {
bool isDataHolder = false);
#endif
void notifyContentChanged();
void notifyContent(MMKVContentNotifyType notifyType);
#if defined(MMKV_ANDROID) && !defined(MMKV_DISABLE_CRYPT)
void checkReSetCryptKey(int fd, int metaFD, const std::string *cryptKey, bool aes256);
@@ -643,10 +643,11 @@ public:
// check if content been changed by other process
void checkContentChanged();
// called when content is changed by other process
// notify handler for content
// MMKVContentChanged is called when content is changed by other process
// doesn't guarantee real-time notification
static void registerContentChangeHandler(mmkv::ContentChangeHandler handler);
static void unRegisterContentChangeHandler();
static void registerContentNotifyHandler(mmkv::ContentNotifyHandler handler);
static void unRegisterContentNotifyHandler();
// by default MMKV will discard all datas on failure
// return `OnErrorRecover` to recover any data from file
+9 -3
View File
@@ -165,6 +165,11 @@ enum MMKVErrorType : int {
MMKVFileLength,
};
enum MMKVContentNotifyType : int {
MMKVContentChanged = 0,
MMKVContentLoaded,
};
enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
MMKV_NAMESPACE_END
@@ -177,9 +182,10 @@ typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const
// return `OnErrorRecover` to recover any data from file
typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
// called when content is changed by other process
// doesn't guarantee real-time notification
typedef void (*ContentChangeHandler)(const std::string &mmapID);
// notify handler for content
// MMKVContentChanged is called when content is changed by other process
// doesn't guarantee real-time notification
typedef void (*ContentNotifyHandler)(const std::string &mmapID, MMKVContentNotifyType notifyType);
extern MMKV_EXPORT size_t DEFAULT_MMAP_SIZE;
+3 -2
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);
notifyContent(MMKVContentLoaded);
// auto keys = allKeys();
// for (size_t index = 0; index < count; index++) {
// MMKVInfo("key[%llu]: %s", index, keys[index].c_str());
@@ -380,7 +381,7 @@ void MMKV::checkLoadData() {
clearMemoryCache();
loadFromFile();
notifyContentChanged();
notifyContent(MMKVContentChanged);
} else if ((m_metaInfo->m_crcDigest != metaInfo.m_crcDigest) || (m_metaInfo->m_actualSize != metaInfo.m_actualSize)) {
MMKVDebug("[%s] crcDigest %u -> %u, actualSize %u -> %u", m_mmapID.c_str(), m_metaInfo->m_crcDigest,
metaInfo.m_crcDigest, m_metaInfo->m_actualSize, metaInfo.m_actualSize);
@@ -396,7 +397,7 @@ void MMKV::checkLoadData() {
} else*/ {
partialLoadFromFile();
}
notifyContentChanged();
notifyContent(MMKVContentChanged);
}
}
+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 */
+15 -8
View File
@@ -50,7 +50,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 ContentNotifyHandler(const string &mmapID, mmkv::MMKVContentNotifyType notifyType);
@interface MMKVNameSpace ()
@@ -111,8 +111,9 @@ static BOOL g_hasCalledInitializeMMKV = NO;
[g_callbackHandler respondsToSelector:@selector(onMMKVFileLengthError:)]) {
mmkv::MMKV::registerErrorHandler(ErrorHandler);
}
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
mmkv::MMKV::registerContentChangeHandler(ContentChangeHandler);
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)] ||
[g_callbackHandler respondsToSelector:@selector(onMMKVContentLoadSuccessfully:)]) {
mmkv::MMKV::registerContentNotifyHandler(ContentNotifyHandler);
}
#if defined(MMKV_IOS) && !defined(MMKV_IOS_EXTENSION) && !defined(TARGET_OS_MACCATALYST)
@@ -1088,7 +1089,7 @@ static NSString *md5(NSString *value) {
mmkv::MMKV::registerErrorHandler(ErrorHandler);
}
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
mmkv::MMKV::registerContentChangeHandler(ContentChangeHandler);
mmkv::MMKV::registerContentNotifyHandler(ContentNotifyHandler);
}
}
@@ -1101,7 +1102,7 @@ static NSString *md5(NSString *value) {
mmkv::MMKV::unRegisterLogHandler();
mmkv::MMKV::unRegisterErrorHandler();
mmkv::MMKV::unRegisterContentChangeHandler();
mmkv::MMKV::unRegisterContentNotifyHandler();
}
+ (void)setLogLevel:(MMKVLogLevel)logLevel {
@@ -1252,9 +1253,15 @@ static mmkv::MMKVRecoverStrategic ErrorHandler(const string &mmapID, mmkv::MMKVE
return mmkv::OnErrorDiscard;
}
static void ContentChangeHandler(const string &mmapID) {
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
[g_callbackHandler onMMKVContentChange:[NSString stringWithUTF8String:mmapID.c_str()]];
static void ContentNotifyHandler(const string &mmapID, mmkv::MMKVContentNotifyType notifyType) {
if (notifyType == mmkv::MMKVContentChanged) {
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentChange:)]) {
[g_callbackHandler onMMKVContentChange:[NSString stringWithUTF8String:mmapID.c_str()]];
}
} else {
if ([g_callbackHandler respondsToSelector:@selector(onMMKVContentLoadSuccessfully:)]) {
[g_callbackHandler onMMKVContentLoadSuccessfully:[NSString stringWithUTF8String:mmapID.c_str()]];
}
}
}