Skip to content

[Bug]: GetAppState<TAppState>(…) Returns Null #4331

@JosiahDanger

Description

@JosiahDanger

Describe the bug 🐞

This bug involves the use of SetupDefaultSuspendResume<TAppState>(…), together with a typed suspension host. It occurs when no persisted application state exists, such as on first launch. Under these conditions, GetAppState<TAppState>(…) will return null, having failed to invoke the CreateNewAppStateTyped delegate.

According to the XML documentation for CreateNewAppStateTyped, the delegate is intended to be "typically used when the application is launching fresh". However, it is never invoked in this scenario. The suspension system instead seems to fail silently.

This behaviour forces developers to initialise manually the AppStateValue after execution of SetupDefaultSuspendResume<TAppState>(…), which defeats the documented purpose of CreateNewAppStateTyped.

Step to reproduce

  1. Create a new Avalonia application with ReactiveUI and ReactiveUI.Avalonia.
  2. Create a simple data transfer object (DTO) class to persist application state.
  3. Create a JsonSerializerContext with [JsonSerializable] attributes for the DTO.
  4. Implement suspension:
  • Call OnFrameworkInitializationCompleted() on your AutoSuspendHelper instance.
  • Cast RxSuspension.SuspensionHost to ISuspensionHost<YourDto>.
  • Assign a function to CreateNewAppStateTyped.
  • Call SetupDefaultSuspendResume<TAppState>(…) with type info.
  • Call GetAppState<TAppState>().
  1. Ensure that no persisted state file exists. Run the application.
  2. Observe that GetAppState<TAppState>(…) returns null, and that CreateNewAppStateTyped is never invoked.

You may refer to my 'DT-Version-2.0' repository linked in this issue as a convenient means of replicating the bug.

Reproduction repository

https://github.com/JosiahDanger/DateToday/tree/DT-Version-2.0

Expected behavior

If GetAppState<TAppState>(…) is called when no persisted state exists, it should:

  • Invoke the CreateNewAppStateTyped delegate to generate a default application state.
  • Populate AppStateValue with the result.
  • Return the new, valid state.

Alternatively, the documentation should clarify that CreateNewAppStateTyped is never automatically invoked and that developers must manually initialise AppStateValue before calling GetAppState<TAppState>(…).

Screenshots 🖼️

Image

IDE

Visual Studio 2022

Operating system

Windows 11

Version

25H2

Device

No response

ReactiveUI Version

23.1.8

Additional information ℹ️

ReactiveUI.Avalonia version is 11.4.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions