Tencent / MMKV

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

about harmonyos ui preview error with mmkv #1362

Closed wangshaolei closed 1 month ago

wangshaolei commented 1 month ago

Note:

I invoke MMKV.initialize in EntryAbility onCreate and app work normal, But on the Component page using mmkv, the preview will give an error

The language of MMKV

e.g. ets

The version of MMKV

e.g. v1.3.7

The platform of MMKV

e.g. HarmonyOS

The installation of MMKV

e.g. ohpm

What's the issue?

Post the outputs or screenshots for errors.

Explain what you want by example or code in English. If you have a crash/OOM/ANR on Android, please provide symbolize stack traces. https://developer.android.com/ndk/guides/ndk-stack.html

What's the log of MMKV when that happened?

[Engine Log]Lifetime: 0.000000s 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log]Js-Engine: ark 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log]page: pages/Login.js 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log]Error message: You should Call > > MMKV.initialize() first. 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log]SourceCode: 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log] throw new Error('You > > > should Call MMKV.initialize() first.'); 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log] ^ 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log]Stacktrace: 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log] at mmkvWithID (oh_modules/.ohpm/@tencent+mmkv@1.3.7/oh_modules/@tencent/mmkv/src/main/ets/utils/MMKV.ets:141:19) 07-24 09:34:26.525 51268-995004 C03900/Ace E [Engine Log] at func_main_0 (entry/src/main/ets/cache/CacheKey.ets:9:27)

lingol commented 1 month ago

The log is crystal clear. You should call MMKV.initialize() first. Normally, you should initialize inside the EntryAbility.onCreate() function.

lingol commented 1 month ago

If you have done that and the error still exists, you should make two breakpoints to see if the place you invoke MMKV runs before the place you initialize MMKV.

wangshaolei commented 1 month ago

If you have done that and the error still exists, you should make two breakpoints to see if the place you invoke MMKV runs before the place you initialize MMKV.

我已经在EntryAbility onCreate 初始化了,app 程序正常运行,就是在使用的页面预览会报这个 Log 错误。我使用的是全局导出方式使用

import { MMKV } from '@tencent/mmkv'; class CacheKey { readonly LOGIN_TOKEN: string = "login_token" } export const cacheKey = new CacheKey() export const mmkv: MMKV = MMKV.mmkvWithID("app_cache")

lingol commented 1 month ago

If you have done that and the error still exists, you should make two breakpoints to see if the place you invoke MMKV runs before the place you initialize MMKV.

I will not repeat myself.