rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
288 stars 328 forks source link

fix Exception occurred during the initialization of the flutter #248

Closed gomazaba closed 3 years ago

gomazaba commented 3 years ago

i have same problem as #237

iOS devices. background_locator: 1.6.0+1-beta flutter: 2.2.1

sometimes, app crash when launched.

2021-06-09 08:19:21.006065+0900 Runner[33423:232150] *** Assertion failure in -[FlutterEngine sendOnChannel:message:binaryReply:], FlutterEngine.mm:799
2021-06-09 08:19:21.034245+0900 Runner[33423:232150] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Sending a message before the FlutterEngine has been run.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff20420af6 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2042091f +[NSException raise:format:] + 0
    3   Foundation                          0x00007fff2077056a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   Flutter                             0x000000010a196246 -[FlutterEngine sendOnChannel:message:binaryReply:] + 544
    5   background_locator                  0x000000010a0df5dc -[BackgroundLocatorPlugin sendLocationEvent:] + 316
    6   background_locator                  0x000000010a0df290 -[BackgroundLocatorPlugin prepareLocationMap:] + 128
    7   background_locator                  0x000000010a0df360 -[BackgroundLocatorPlugin locationManager:didUpdateLocations:] + 160
    8   CoreLocation                        0x00007fff234082eb CLClientStopVehicleHeadingUpdates + 79384
    9   CoreLocation                        0x00007fff234074ff CLClientStopVehicleHeadingUpdates + 75820
    10  CoreLocation                        0x00007fff233ef909 CLClientInvalidate + 1479
    11  CoreFoundation                      0x00007fff2038f120 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    12  CoreFoundation                      0x00007fff2038e534 __CFRunLoopDoBlocks + 434
    13  CoreFoundation                      0x00007fff20388f44 __CFRunLoopRun + 899
    14  CoreFoundation                      0x00007fff203886d6 CFRunLoopRunSpecific + 567
    15  GraphicsServices                    0x00007fff2bededb3 GSEventRunModal + 139
    16  UIKitCore                           0x00007fff24690e0b -[UIApplication _run] + 912
    17  UIKitCore                           0x00007fff24695cbc UIApplicationMain + 101
    18  Runner                              0x0000000108be7feb main + 75
    19  libdyld.dylib                       0x00007fff202593e9 start + 1
    20  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Sending a message before the FlutterEngine has been run.'
terminating with uncaught exception of type NSException

I think it happened because the plugin was calling FlutterMethodChannel during the initialization of the flutter engine.

FlutterEngine.mm#L798-L799

so, I changed to ignore that exception.

Wian-TMC commented 3 years ago

When can this be merged?

Wian-TMC commented 3 years ago

@mehdok I forked and tried this fix, but I was still able to reproduce the error when changing iOS permissions from 'Allow only while using App' to 'Always allow' when prompted, the iOS app crashes on startup again and again, even with the above workaround.

It seems to crash on iOS consistently on app startup when permissions are set to 'Always allow'.

mehdok commented 3 years ago

@Wian-TMC I did a little modification to this branch, I'll publish it in a few minutes, please test that to see if the problem still exists;

Wian-TMC commented 3 years ago

@Wian-TMC I did a little modification to this branch, I'll publish it in a few minutes, please test that to see if the problem still exists;

Will test again, thank a mil

mehdok commented 3 years ago

@Wian-TMC Please check the latest version 1.6.3;

Wian-TMC commented 3 years ago

@Wian-TMC Please check the latest version 1.6.3;

Awesome, I assume the change in android/src/main/kotlin/rekab/app/background_locator/BackgroundLocatorPlugin.kt fixed issue #252 as well?

mehdok commented 3 years ago

@Wian-TMC I'm not sure exactly, please check to see if that problem is fixed or not;

Wian-TMC commented 3 years ago

@Wian-TMC I'm not sure exactly, please check to see if that problem is fixed or not;

Issue still stands.

  1. Allow background locator to initialise
  2. Close the app
  3. Change permissions to 'Deny', and then back to 'Always allow'. This kills the background locator service.
  4. Trying to initialise the locator again with registerLocationUpdate() will not run unless you run unRegisterLocationUpdate(). This is because the locator still thinks it is running in the background -> Confirmed with isServiceRunning(), which always returns true after the above procedure.
mehdok commented 3 years ago

@Wian-TMC So there is not any crash, am I right? I'm aware of this malfunctioning and marked it as a bug somewhere;

The problem is I'm saving the state of the service in UserDefault and SharedPreferences; I couldn't find a better way to realize whether the service is running or not (in the case of iOS there is not a service, I just have to find a way to tell if the location update started or not);

I'm open to any suggestions;

Wian-TMC commented 3 years ago

Correct, there is no crash. Just the functionality issue.

Wian-TMC commented 3 years ago

@Wian-TMC So there is not any crash, am I right? I'm aware of this malfunctioning and marked it as a bug somewhere;

The problem is I'm saving the state of the service in UserDefault and SharedPreferences; I couldn't find a better way to realize whether the service is running or not (in the case of iOS there is not a service, I just have to find a way to tell if the location update started or not);

I'm open to any suggestions;

Is the process running on a separate thread? Will it be possible to clear the state of the service in UserDefault and SharedPreferences when the thread ends or throws an exception?

Wian-TMC commented 3 years ago

@Wian-TMC I did a little modification to this branch, I'll publish it in a few minutes, please test that to see if the problem still exists;

Issue with 'Always allow' permissions is gone on iOS! Thanks so much

mehdok commented 3 years ago

Is the process running on a separate thread? Will it be possible to clear the state of the service in UserDefault and SharedPreferences when the thread ends or throws an exception?

No, they are not in a separate thread, But it's a really good idea. I think it is possible to do that in Android, not sure about iOS.

Wian-TMC commented 3 years ago

@Wian-TMC I did a little modification to this branch, I'll publish it in a few minutes, please test that to see if the problem still exists;

Issue with 'Always allow' permissions is gone on iOS! Thanks so much

@mehdok Bad news... The problem is NOT gone. It worked for a few hours, but leaving the app permissions on iOS as 'Always allow' brought the crash back. The logs are as follows:


Sending a message before the FlutterEngine has been run.
Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x1a0206754 __exceptionPreprocess
1  libobjc.A.dylib                0x1b4ccd7a8 objc_exception_throw
2  CoreFoundation                 0x1a01083f8 -[CFPrefsSearchListSource addManagedSourceForIdentifier:user:]
3  Foundation                     0x1a14dc714 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  Flutter                        0x105eb115c (Missing)
5  background_locator             0x105cbb8fc (Missing)
6  background_locator             0x105cbc864 (Missing)
7  background_locator             0x105cbc638 (Missing)
8  background_locator             0x105cbba50 (Missing)
9  Flutter                        0x105ebd15c (Missing)
10 Runner                         0x1044c3674 AppDelegate.application(_:didFinishLaunchingWithOptions:) + 24 (AppDelegate.swift:24)
11 Runner                         0x1044c37a8 @objc AppDelegate.application(_:didFinishLaunchingWithOptions:) (<compiler-generated>)
12 UIKitCore                      0x1a2bf07a8 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:]
13 UIKitCore                      0x1a2bf223c -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:]
14 UIKitCore                      0x1a2bf8388 -[UIApplication _runWithMainScene:transitionContext:completion:]
15 UIKitCore                      0x1a224bc98 -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:]
16 UIKitCore                      0x1a27b4f58 _UIScenePerformActionsWithLifecycleActionMask
17 UIKitCore                      0x1a224c830 __101-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]_block_invoke
18 UIKitCore                      0x1a224c238 -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:]
19 UIKitCore                      0x1a224c640 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]
20 UIKitCore                      0x1a224be7c -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:]
21 UIKitCore                      0x1a22543c0 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke
22 UIKitCore                      0x1a26c2970 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:]
23 UIKitCore                      0x1a27cdd68 _UISceneSettingsDiffActionPerformChangesWithTransitionContext
24 UIKitCore                      0x1a22540b8 -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]
25 UIKitCore                      0x1a207bfa0 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke
26 UIKitCore                      0x1a207a920 -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:]
27 UIKitCore                      0x1a207bbc8 -[UIScene scene:didUpdateWithDiff:transitionContext:completion:]
28 UIKitCore                      0x1a2bf6528 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]
29 UIKitCore                      0x1a26ebfd0 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]
30 FrontBoardServices             0x1af7985d8 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:]
31 FrontBoardServices             0x1af7c3d44 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.200
32 FrontBoardServices             0x1af7a76a4 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:]
33 FrontBoardServices             0x1af7c3a0c __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke
34 libdispatch.dylib              0x19fdf381c _dispatch_client_callout
35 libdispatch.dylib              0x19fdf730c _dispatch_block_invoke_direct
36 FrontBoardServices             0x1af7ebfa0 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
37 FrontBoardServices             0x1af7ebc30 -[FBSSerialQueue _targetQueue_performNextIfPossible]
38 FrontBoardServices             0x1af7ec184 -[FBSSerialQueue _performNextFromRunLoopSource]
39 CoreFoundation                 0x1a01809e8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
40 CoreFoundation                 0x1a01808e4 __CFRunLoopDoSource0
41 CoreFoundation                 0x1a017fbe8 __CFRunLoopDoSources0
42 CoreFoundation                 0x1a0179bc8 __CFRunLoopRun
43 CoreFoundation                 0x1a0179360 CFRunLoopRunSpecific
44 GraphicsServices               0x1b77b7734 GSEventRunModal
45 UIKitCore                      0x1a2bf4584 -[UIApplication _run]
46 UIKitCore                      0x1a2bf9df4 UIApplicationMain
47 Runner                         0x1044c3a70 main + 15 (AppDelegate.swift:15)
48 libdyld.dylib                  0x19fe35cf8 start
Wian-TMC commented 3 years ago

@mehdok Should I open a new issue regarding the above, seeing as this one has been merged?

mehdok commented 3 years ago

@Wian-TMC Sure, please do that and provide all the information available;