idisfkj / android-startup

🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.
https://rousetime.com
Apache License 2.0
1.6k stars 158 forks source link

fix wait logic #3

Closed ethanhua closed 4 years ago

ethanhua commented 4 years ago

the countdown create logic not match that task complete and reduce count logic

ethanhua commented 4 years ago

thanks for your great project, i found the problem that the "wait" api not work when i apply it to my app, I fixed it and you can check it and if it works you can merge it in

idisfkj commented 4 years ago

I think this logic is not a problem, because if it callCreateOnMainThread() == true, that the component is initialized in the main thread, and the main thread is blocked by default, so there's no need to call mNeedAwaitCount.IncrementAndGet() wait for additional jam.

However, the await() method is somewhat problematic, and I guess the problem you are experiencing is caused by the following reasons.

The cause should be this code in StartupManagerDispatcher:

    override fun notifyChildren(dependencyParent: Startup<*>, result: Any?, sortStore: StartupSortStore) {
        // immediately notify main thread,Unblock the main thread.
        if (dependencyParent.waitOnMainThread()) {
            needAwaitCount.incrementAndGet()
            awaitCountDownLatch?.countDown()
        }
        ...
    }

It should be amended to:

    override fun notifyChildren(dependencyParent: Startup<*>, result: Any?, sortStore: StartupSortStore) {
        // immediately notify main thread,Unblock the main thread.
        if (dependencyParent.waitOnMainThread() && !dependencyParent.callCreateOnMainThread()) {
            needAwaitCount.decrementAndGet()
            awaitCountDownLatch?.countDown()
        }
        ...
    }

Will this solve your problems?

ethanhua commented 4 years ago

I thought carefully that it would be all right for you to change it this way,in my way it's also can work

idisfkj commented 4 years ago

Thank you for your pr.