.NET MAUI sample crashes on startup in iOS Release mode

I can run this MAUI sample app with no problem in Windows and in Debug mode in iOS. However, when building in iOS Release mode and deploying to an iOS device, it crashes on startup.

I am wondering if this is the same type of serialization issue that I’ve run into with two other .NET synchronization libraries, as discussed here and here.

I also found this forum thread - it’s very old but it does describe the same experience in the same specific environment. Unfortunately there’s no resolution there.

Has anyone successfully run this sample app, or any .NET MAUI app, in Release mode on iOS?

(cc: @biozal)

I’m not familiar with the example application or with mobile Couchbase development. However, I have glanced at the example code and I believe I see the problem. The issue is that NativeAOT is not compatible with reflection-based JSON serialization, which is what is being done here: cbl-template-app-maui-todo/RealmTodo/Models/Item.cs at a43d64c5585b2d0ff3dfcbba83fb7f7a038f67d2 · couchbaselabs/cbl-template-app-maui-todo · GitHub.

Instead, source generation should be used to generate the required metadata at compile time for both serialization and deserialization. This is done by creating a type inherited from JsonSerializerContext, annotating it correctly, and then passing properties from it as a parameter whenever calling JsonSerializer.Serialize or JsonSerializer.Deserialize.

The end result would probably look something like this:

[JsonSerializable(typeof(Item))]
public partial class ItemSerializerContext : JsonSerializerContext
{
}

public partial class Item
{
    public string ToJson()
    {
        return JsonSerializer.Serialize(this, ItemSerializerContext.Default.Item);
    }
}

Similar changes will be needed for deserialization in CouchbaseService.

3 Likes

Thanks, @btburnett3 - yes, that was spot on.

In addition to setting up source generation for the serialization and deserialization, the other critical change is to enable the Monotouch interpreter by adding this line in the build properties for the iOS Release build in the app’s project properties file:

<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
    <UseInterpreter>all</UseInterpreter>
</PropertyGroup>

Once I made these two sets of changes, the sample app runs fine in iOS Release mode.