Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android ListenableWorker MvxSetupSingleton.EnsureInitialized() crashes with InvalidOperationException #4555

Closed
1 task done
MagneticLlama opened this issue Jan 19, 2023 · 2 comments 路 Fixed by #4556
Closed
1 task done
Labels
p/android Android platform t/bug Bug type

Comments

@MagneticLlama
Copy link

MagneticLlama commented Jan 19, 2023

馃悰 Bug Report

When trying to initialize MvvmCross from a background worker when the main application is not open, calling MvvmCross.Core.MvxSetupSingleton.EnsureInitialized() crashes the app with System.InvalidOperationException: Cannot create Views Container without ApplicationContext due to public Context? ApplicationContext => _currentTopActivity?.Activity?.ApplicationContext; being null in MvvmCross.Platforms.Android.Core.MvxAndroidSetup

MvvmCross.Platforms.Android.Core.MvxAndroidSetup.CreateViewsContainer(IMvxIoCProvider iocProvider)
MvvmCross.Core.MvxSetup.InitializeViewsContainer(IMvxIoCProvider iocProvider)
MvvmCross.Core.MvxSetup.InitializeSecondary()
MvvmCross.Core.MvxSetupSingleton.<StartSetupInitialization>b__13_0()
MvvmCross.Core.MvxSetupSingleton.EnsureInitialized()
Judah.NightOut.Droid.Workers.WorkerBase`1[[Judah.Client.CoreStandard.Messages.DeleteLocalMediaMessageCacheEntry, Judah.Client.CoreStandard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Initialize()
Judah.NightOut.Droid.Workers.WorkerBase`1[[Judah.Client.CoreStandard.Messages.DeleteLocalMediaMessageCacheEntry, Judah.Client.CoreStandard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]..ctor(Context context, WorkerParameters workerParams)
Judah.NightOut.Droid.Workers.DeleteLocalMediaCacheWorker..ctor(Context context, WorkerParameters workerParams)
System.Reflection.ConstructorInvoker.InterpretedInvoke(Object obj, Span`1 args, BindingFlags invokeAttr)

Expected behavior

MvvmCross should initialize without a current top activity

Reproduction steps

Create a periodic work request in MainApplication that will run after the app is closed

public class MainApplication : MvxAndroidApplication<Setup, App>
{
    public MainApplication(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();

        var constraintsBuilder = new Constraints.Builder();
        constraintsBuilder.SetRequiredNetworkType(NetworkType.NotRequired);
        var constraints = constraintsBuilder.Build();
        var workRequest = new PeriodicWorkRequest.Builder(typeof(DummyWorker), new TimeSpan(0, 1, 0))
            .SetConstraints(constraints)
            .Build();
        WorkManager.GetInstance(ApplicationContext).EnqueueUniquePeriodicWork(nameof(DummyWorker), ExistingPeriodicWorkPolicy.Replace, workRequest);
    }
}
public class DummyWorker : ListenableWorker, CallbackToFutureAdapter.IResolver
{
    public DummyWorker(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
        Initialize();
    }

    protected DummyWorker(Context context, WorkerParameters workerParams) : base(context, workerParams)
    {
        Initialize();
    }

    private void Initialize()
    {
        var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
        setup.EnsureInitialized();
    }

    public override IListenableFuture StartWork()
    {
        return CallbackToFutureAdapter.GetFuture(this);
    }

    public Java.Lang.Object? AttachCompleter(CallbackToFutureAdapter.Completer completer)
    {
        completer.Set(Result.InvokeSuccess());
        return string.Empty;
    }
}

Configuration

Version: 9.0.8
net7.0-android

Platform:

  • 馃 Android
@Cheesebaron
Copy link
Member

Cheesebaron commented Jan 20, 2023

This should potentially be fixed when #4546 gets merged, then you don't need to call EnsureInitialized all over the place.

But I guess also I would need to change MvxAndroidSetupSingleton to not get ApplicationContext from top Activity. Will prepare a PR for this.

In the meantime you can override InitializeViewsContainer and provide your own CreateViewsContainer method to use instead, which uses ApplicationContext which is not from top Activity

@Cheesebaron
Copy link
Member

Cheesebaron commented Jan 20, 2023

9.0.9 is building now with the fix from the linked PR. Please open a new issue if this persists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p/android Android platform t/bug Bug type
Development

Successfully merging a pull request may close this issue.

2 participants