Tencent / MMKV

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

Some questions about the principle of realization #108

Closed danleechina closed 5 years ago

danleechina commented 5 years ago

Hello, I am an iOS developer.

I have read some source files to know how you create this project. I know that you use mmap to save data, the advantage is if the app is crashed, OS will help us care about the saving of the file. And when app is launched, construct the NSDictionary object manually from the local file.

But why you don't use NSSetUncaughtExceptionHandler(&HandleExceptions); to handle the crash? And if there is a crash, you can save the NSDictionary object as a json string to a file. And if there's no crash, you just need to save the object at appropriate time.

I think this is more simple and cover the most using cases. But I want to hear your options about this.

Thank you.

lingol commented 5 years ago

First thing, that crash handler can't catch every crash. It just catch Objective-C exceptions. While in the real world, crash may occur in any place, with any form.

While you do can catch crash with some framework like plcrashreporter, App will be shutdown not just crash. Things like OOM will do it too. While that happens, you just don't get any callback, right?

So here comes the mmap and MMKV. You just don't need to worry about crashes and OOMs, data will be saved.

danleechina commented 5 years ago

Yeah, it's necessary to use the mmap. I create a project named MKB(Yes, I am sorry 😂) which doesn't use mmap, but I will consider about using the mmap in an elegant way.

Thank you for your reply.