X Tutup
Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

@EBean(SINGLETON) and getInstance_(Context) on BackgroundThread #1507

@tobias-

Description

@tobias-

When an Activity uses a @Bean that it initializes in a background Thread (afaik, there is no other way to avoid just showing the background until after initialization), the code for initializing the bean is not thread safe, and can cause (and for me, causes) Exceptions because the instance exists, but has not received a state yet.

The following is a typical getInstance_() method:

    public static Client_ getInstance_(Context context) {
        if (instance_ == null) {
            OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(null);
            instance_ = new Client_(context.getApplicationContext());
            instance_.init_();
            OnViewChangedNotifier.replaceNotifier(previousNotifier);
        }
        return instance_;
    }

What happens for me is that instance_ = new Client() has been run and completed, but not instance_.init(). This happens when e.g. the user rotates the screen causing a new BackgroundThread to be started.

I managed to hack around the delay by using @UiThread(propagation = ENQUEUE) and getInstance_(Context), but I would rather not have to do all initialization of all beans on the MainThread (for performance reasons).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup