Tencent / MMKV

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

鸿蒙版本的MMKV,double、float、int、bool现在不能返回undefined #1310

Closed HideNameUser closed 1 week ago

HideNameUser commented 1 month ago

let defaultValue:number | undefined = undefined MMKV.decodeDouble("KEY_NOT_FUND", defaultValue) 返回的结果为0,不是想要的默认值undefined 标题中提到的int、float也是同样的 boolean类型返回默认值为false,也无法返回undefined。 返回undefined能知道是没有存过这个值。

lingol commented 1 month ago

Noted.

lingol commented 1 month ago

I have taken a deeper look into this issue. It is more complicated than I thought.

The problem is that the Android interface, which many of you OHOS developers are familiar with, returns 0/false when a primitive type of value does not exist. If we declared the interface as

public decodeBool(key: string, defaultValue?: boolean): boolean | undefined;

By default, it will return undefined on non-exist values, even if you have not passed undefined as the defaultValue. This behavior doesn't agree with the Andoird/Java interface many of you have been familiar with.

Now, how about we declare it as:

public decodeBool(key: string, defaultValue?: boolean = false): boolean | undefined;

The problem is that it won't compile.

lingol commented 1 month ago

Maybe we have to drop the undefined default value.

lingol commented 1 month ago

I've decided that undefined will be returned on non-existent values unless a specific defaultValue is passed in. It's fixed in 1.3.6-beta2 on OHPM.

lingol commented 1 week ago

It's fixed in v1.3.6.