update napi/settings/napi_settings_observer.cpp.

Signed-off-by: 侯志雄 <houzhixiong1@h-partners.com>
This commit is contained in:
侯志雄 2024-11-16 14:08:20 +00:00 committed by Gitee
parent 9c894d8472
commit 6081b2eb00
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -35,7 +35,6 @@ namespace OHOS {
namespace Settings {
std::map<std::string, sptr<SettingsObserver>> g_observerMap;
std::mutex g_observerMapMutex;
bool g_observerFlag = false;
SettingsObserver::~SettingsObserver()
{
@ -46,13 +45,22 @@ namespace Settings {
this->cbInfo = nullptr;
}
bool IsExistObserver(SettingsObserver* settingsObserver) {
for (auto it = g_observerMap.begin(); it != g_observerMap.end(); ++it) {
if (&(*(it->second)) == settingsObserver) {
return true;
}
}
return false;
}
int OnChangeAsync(uv_loop_s* loop, uv_work_t *work)
{
int ret = uv_queue_work(loop, work, [](uv_work_t *work) {},
[](uv_work_t *work, int status) {
SETTING_LOG_INFO("n_s_o_c_a");
SettingsObserver* settingsObserver = reinterpret_cast<SettingsObserver*>(work->data);
if (!g_observerFlag || settingsObserver == nullptr || settingsObserver->cbInfo == nullptr ||
if (!IsExistObserver(settingsObserver) || settingsObserver == nullptr || settingsObserver->cbInfo == nullptr ||
settingsObserver->toBeDelete) {
SETTING_LOG_ERROR("uv_work: cbInfo invalid.");
delete work;
@ -185,7 +193,6 @@ namespace Settings {
std::string strUri = GetStageUriStr(callbackInfo->tableName, GetObserverIdStr(), callbackInfo->key);
OHOS::Uri uri(strUri);
g_observerFlag = true;
sptr<SettingsObserver> settingsObserver = sptr<SettingsObserver>
(new (std::nothrow)SettingsObserver(callbackInfo));
g_observerMap[callbackInfo->key] = settingsObserver;
@ -248,7 +255,6 @@ namespace Settings {
dataShareHelper->UnregisterObserver(uri, g_observerMap[key]);
dataShareHelper->Release();
g_observerMap[key]->toBeDelete = true;
g_observerFlag = false;
g_observerMap[key] = nullptr;
g_observerMap.erase(key);