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

Thread 1: "MMKV not initialized properly, must call +initializeMMKV: in main thread before calling any other MMKV methods" #1299

Closed EchoHayate closed 2 months ago

EchoHayate commented 2 months ago

Note:

An crash issue will be ignored & closed in a week without logs.

The language of MMKV

Swift

The version of MMKV

1.3.5

The platform of MMKV

iOS

The installation of MMKV

Cocoapods

What's the issue?

Thread 1: "MMKV not initialized properly, must call +initializeMMKV: in main thread before calling any other MMKV methods"

What's the log of MMKV when that happened?

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'MMKV not initialized properly, must call +initializeMMKV: in main thread before calling any other MMKV methods' First throw call stack: ( 0 CoreFoundation 0x00000001804ae138 exceptionPreprocess + 172 1 libobjc.A.dylib 0x0000000180087db4 objc_exception_throw + 56 2 Foundation 0x0000000180d2dfd8 _userInfoForFileAndLine + 0 3 MMKV 0x0000000100d89888 +[MMKV mmkvWithID:cryptKey:rootPath:mode:expectedCapacity:] + 156 4 MMKV 0x0000000100d897e0 +[MMKV mmkvWithID:cryptKey:rootPath:mode:] + 88 5 MMKV 0x0000000100d893e8 +[MMKV mmkvWithID:mode:] + 160 6 moment 0x0000000100aec104 $sSo4MMKVC6mmapID4modeABSgSS_So8MMKVModeVtcfCTO + 64 7 moment 0x0000000100aebe8c $s6moment13OperationViewC09configSubC0yyF + 728 8 moment 0x0000000100aca994 $s6moment8BaseViewC5frameACSo6CGRectV_tcfc + 320 9 moment 0x0000000100acaa14 $s6moment8BaseViewC5frameACSo6CGRectV_tcfcTo + 56 10 moment 0x0000000100aec608 $s6moment13OperationViewC5frameACSo6CGRectV_tcfc + 272 11 moment 0x0000000100aec66c $s6moment13OperationViewC5frameACSo6CGRectV_tcfcTo + 56 12 moment 0x0000000100ae548c $s6moment13OperationViewC5frameACSo6CGRectV_tcfC + 96 13 moment 0x0000000100ae5284 $s6moment9GroupCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfc + 3152 14 moment 0x0000000100ae5538 $s6moment9GroupCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfcTo + 160 15 moment 0x0000000100ae8290 $s6moment8TextCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfc + 228 16 moment 0x0000000100ae8370 $s6moment8TextCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfcTo + 160 17 UIKitCore 0x0000000185603c04 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 416 18 UIKitCore 0x0000000185603e44 -[UITableView dequeueReusableCellWithIdentifier:] + 152 19 UIKitCore 0x0000000185604034 -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:] + 216 20 UIKitCore 0x0000000185603f38 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 68 21 moment 0x0000000100ac9270 $s6moment16MomentControllerC9tableView_12cellForRowAtSo07UITableE4CellCSo0jE0C_10Foundation9IndexPathVtF + 612 22 moment 0x0000000100ac9408 $s6moment16MomentControllerC9tableView_12cellForRowAtSo07UITableE4CellCSo0jE0C_10Foundation9IndexPathVtFTo + 136 23 UIKitCore 0x0000000185618c8c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 1376 24 UIKitCore 0x00000001855eba54 -[UITableView _updateVisibleCellsForRanges:createIfNecessary:] + 560 25 UIKitCore 0x00000001855ec06c -[UITableView _updateVisibleCellsNow:] + 1112 26 UIKitCore 0x0000000185606168 -[UITableView layoutSubviews] + 144 27 UIKitCore 0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492 28 QuartzCore 0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440 29 QuartzCore 0x000000018a1b940c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124 30 QuartzCore 0x000000018a0f2d30 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460 31 QuartzCore 0x000000018a122aa8 _ZN2CA11Transaction6commitEv + 652 32 UIKitCore 0x0000000185400570 34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32 33 CoreFoundation 0x000000018040ec50 CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 20 34 CoreFoundation 0x000000018040e398 CFRunLoopDoBlocks + 352 35 CoreFoundation 0x0000000180408c3c CFRunLoopRun + 788 36 CoreFoundation 0x0000000180408514 CFRunLoopRunSpecific + 572 37 GraphicsServices 0x000000018ef06ae4 GSEventRunModal + 160 38 UIKitCore 0x00000001853e8040 -[UIApplication _run] + 868 39 UIKitCore 0x00000001853ebcc8 UIApplicationMain + 124 40 moment 0x0000000100ae2344 main + 64 41 dyld 0x0000000100cf9544 start_sim + 20 42 ??? 0x0000000100e7a0e0 0x0 + 4310147296 43 ??? 0x8152000000000000 0x0 + 9318510578982977536 ) libc++abi: terminating due to uncaught exception of type NSException

lingol commented 2 months ago

I think the crash is pretty clear. In fact it is crystal clear. MMKV is NOT initiated correctly.

What is your problem?

EchoHayate commented 2 months ago

![Uploading Screenshot 2024-05-13 at 10.23.01.png…]() Cheers ! Sorry that I forgot to mention that I have initialize the mmkv by calling this function.

EchoHayate commented 2 months ago

![Uploading Screenshot 2024-05-13 at 10.26.49.png…]() I'm not sure that if you could open the screenshot or not. So I upload it again and with the paste of the snippet

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds);
    window?.backgroundColor = UIColor.white;

     MMKV.initialize();
lingol commented 2 months ago

I'm not buying it. Please provide the full logs of mmkv, starting from App launch.

lingol commented 2 months ago

You should make MMKV.initialize() the first line of your didFinishLaunch, not the third line.

EchoHayate commented 2 months ago

Cheers ! Here's the full logs. Assertion failure in +[MMKV mmkvWithID:cryptKey:rootPath:mode:expectedCapacity:], libMMKV.mm:215 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'MMKV not initialized properly, must call +initializeMMKV: in main thread before calling any other MMKV methods' *** First throw call stack: ( 0 CoreFoundation 0x00000001804ae138 exceptionPreprocess + 172 1 libobjc.A.dylib 0x0000000180087db4 objc_exception_throw + 56 2 Foundation 0x0000000180d2dfd8 _userInfoForFileAndLine + 0 3 MMKV 0x0000000105f11888 +[MMKV mmkvWithID:cryptKey:rootPath:mode:expectedCapacity:] + 156 4 MMKV 0x0000000105f117e0 +[MMKV mmkvWithID:cryptKey:rootPath:mode:] + 88 5 MMKV 0x0000000105f113e8 +[MMKV mmkvWithID:mode:] + 160 6 moment 0x0000000104f5041c $sSo4MMKVC6mmapID4modeABSgSS_So8MMKVModeVtcfCTO + 64 7 moment 0x0000000104f501a4 $s6moment13OperationViewC09configSubC0yyF + 728 8 moment 0x0000000104f2ed68 $s6moment8BaseViewC5frameACSo6CGRectV_tcfc + 320 9 moment 0x0000000104f2ede8 $s6moment8BaseViewC5frameACSo6CGRectV_tcfcTo + 56 10 moment 0x0000000104f50920 $s6moment13OperationViewC5frameACSo6CGRectV_tcfc + 272 11 moment 0x0000000104f50984 $s6moment13OperationViewC5frameACSo6CGRectV_tcfcTo + 56 12 moment 0x0000000104f497a4 $s6moment13OperationViewC5frameACSo6CGRectV_tcfC + 96 13 moment 0x0000000104f4959c $s6moment9GroupCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfc + 3152 14 moment 0x0000000104f49850 $s6moment9GroupCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfcTo + 160 15 moment 0x0000000104f4c5a8 $s6moment8TextCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfc + 228 16 moment 0x0000000104f4c688 $s6moment8TextCellC5style15reuseIdentifierACSo011UITableViewC5StyleV_SSSgtcfcTo + 160 17 UIKitCore 0x0000000185603c04 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 416 18 UIKitCore 0x0000000185603e44 -[UITableView dequeueReusableCellWithIdentifier:] + 152 19 UIKitCore 0x0000000185604034 -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:] + 216 20 UIKitCore 0x0000000185603f38 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 68 21 moment 0x0000000104f2d644 $s6moment16MomentControllerC9tableView_12cellForRowAtSo07UITableE4CellCSo0jE0C_10Foundation9IndexPathVtF + 612 22 moment 0x0000000104f2d7dc $s6moment16MomentControllerC9tableView_12cellForRowAtSo07UITableE4CellCSo0jE0C_10Foundation9IndexPathVtFTo + 136 23 UIKitCore 0x0000000185618c8c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 1376 24 UIKitCore 0x00000001855eba54 -[UITableView _updateVisibleCellsForRanges:createIfNecessary:] + 560 25 UIKitCore 0x00000001855ec06c -[UITableView _updateVisibleCellsNow:] + 1112 26 UIKitCore 0x0000000185606168 -[UITableView layoutSubviews] + 144 27 UIKitCore 0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492 28 QuartzCore 0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440 29 QuartzCore 0x000000018a1b940c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124 30 QuartzCore 0x000000018a0f2d30 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460 31 QuartzCore 0x000000018a122aa8 _ZN2CA11Transaction6commitEv + 652 32 UIKitCore 0x0000000185400570 34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32 33 CoreFoundation 0x000000018040ec50 CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 20 34 CoreFoundation 0x000000018040e398 CFRunLoopDoBlocks + 352 35 CoreFoundation 0x0000000180408c3c CFRunLoopRun + 788 36 CoreFoundation 0x0000000180408514 CFRunLoopRunSpecific + 572 37 GraphicsServices 0x000000018ef06ae4 GSEventRunModal + 160 38 UIKitCore 0x00000001853e8040 -[UIApplication _run] + 868 39 UIKitCore 0x00000001853ebcc8 UIApplicationMain + 124 40 moment 0x0000000104f4671c main + 64 41 dyld 0x000000010515d544 start_sim + 20 42 ??? 0x00000001052020e0 0x0 + 4380958944 43 ??? 0x3401800000000000 0x0 + 3747417102437318656 ) libc++abi: terminating due to uncaught exception of type NSException

Now I have changed the position of the MMKV.initialize(). The issue remains the same.

lingol commented 2 months ago

No, that is the crash stack. I'm taking about logs from console.

lingol commented 2 months ago

To be clear, add a line of log before you call MMKV.initialize(). And if there's no log, then mmkv is not initiated.

lingol commented 2 months ago

And if that's the case, move it to your main() function or create a constructor function for your AppDelegate.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    init() {
        MMKV.initialize()

        super.init()
    }
……
}
EchoHayate commented 2 months ago

I have added the lines at the beginning of the code. @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

override init(){
    print(Thread.current)

    MMKV.initialize()

    super.init()
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds);
    window?.backgroundColor = UIColor.white;

Here is my backtrace

Also, I have upgraded the UIKit to the latest version. This issue is still unfixed.

lingol commented 2 months ago

What are the logs from the console? Is it so hard to understand?

lingol commented 2 months ago

You know what, I'm tired of repeating to ask the same question. Why don't you upload a reproducible demo?

EchoHayate commented 2 months ago

Sorry for causing you any trouble. I'm still learning as a beginner. Here is the repository of the project:https://github.com/EchoHayate/Moment

lingol commented 2 months ago

The problem is that you have called the wrong initialize method.

// MMKV.initialize();
MMKV.initialize(rootDir: nil);

Change it as the code above and you should have no problem.

The initialize() method is inherited from NSObject and is useless. I should make it hidden later.

EchoHayate commented 2 months ago

Cheers! I think I couldn't find the log from console even though I tried many times across the Internet. Could you please tell me how to do it? Many Thanks!

lingol commented 2 months ago

https://stackoverflow.com/questions/483493/how-to-show-the-logged-messages-window-in-xcode