Reacting to Boolean state in reactive ViewModels When this ViewModel loads we kick off the IsAuthenticatedCommand:


If the result of IsAuthenticatedCommand is false we set the value of NeedsToLogin to true.
If the result of IsAuthenticatedCommand is true we set the value of IsAuthenticated to true.


There are (subscriptions?) to both NeedsToLogin and IsAuthenticated that are waiting for the true value.


Is there a better way to handle this if/else logic?
Should I be worried about race conditions?




public class LoadingViewModel : ReactiveObject, IRoutableViewModel {

    private bool _isAuthenticated = false;

    public bool IsAuthenticated {
    get { return _isAuthenticated; }
    set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }}


    private bool _needsToLogin;

    public bool NeedsToLogin {
    get { return _needsToLogin; }
    set { this.RaiseAndSetIfChanged (ref _needsToLogin, value); }}

    public ReactiveCommand&lt;bool&gt; IsAuthenticatedCommand { get; protected set; }

    public LoadingViewModel (IScreen screen)
    {
        HostScreen = screen;

        this.WhenAnyValue (x =&gt; x.IsAuthenticated)
            .Where (x =&gt; x == true)
            .Subscribe (x =&gt; { HostScreen.Router.Navigate.ExecuteAsyncTask (new OnboardingViewModel (HostScreen));});

        this.WhenAnyValue (x =&gt; x.NeedsToLogin)
            .Where (x =&gt; x == true)
            .Subscribe (_ =&gt; { HostScreen.Router.Navigate.ExecuteAsyncTask (new LoginViewModel (HostScreen)); });


        //always return false for testing.   
        IsAuthenticatedCommand = ReactiveCommand.CreateAsyncTask (async (_) =&gt; await Task.Run (() =&gt; {return false;}));

        IsAuthenticatedCommand.Subscribe (_ =&gt; {
        if (_) { this.IsAuthenticated = true;} else {this.NeedsToLogin = true;}
        });
    }
}
You need to format your code neatly; this will improve readability, which will decrease bugs.  First, you have this:

public bool IsAuthenticated {
get { return _isAuthenticated; }
set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }}


This should be:

public bool IsAuthenticated
{
    get { return _isAuthenticated; }
    set { this.RaiseAndSetIfChanged (ref _isAuthenticated, value); }
}


The if/else statement should be written like this:

IsAuthenticatedCommand.Subscribe (_ =&gt; {
    if (_) { this.IsAuthenticated = true; }
    else { this.NeedsToLogin = true; }
});


Also, you need to be consistent with your spacing; sometimes you have a space between braces, sometimes you don't:

if (_) { this.IsAuthenticated = true;} else {this.NeedsToLogin = true;}


I am no expert at lambda statements, but your lambda with the if/else looks fine to me.