The app I'm working on successfully creates am SQLite DB on Android or on an iOS Simulator and populates initial data, but Release iOS build on a real device just doesn't have any data.
I'm using Entity Framework Core Initially I was using SQLite Nuget packages. After switching to EF I cleaned things up - removed all related packages and added only those that EF pulled along with their Nuget.
I've created a context class
internal class MyDbContext : DbContext
{
public MyDbContext()
{
SQLitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
public DbSet<Entity1> Entities1 { get; set; }
public DbSet<Entity2> Entities2 { get; set; }
public DbSet<Entity3> Entities3 { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyDatabase.db3");
optionsBuilder
.UseSqlite($"Filename={dbPath}");
}
}
Made a base controller class
internal abstract class ControllerBase<T> where T : IDBItem, new()
{
protected IMapper _mapper;
/// <summary>
/// Use only in case of unavoidable execution of raw sql
/// </summary>
protected MyDbContext GetContext() => new MyDbContext();
public ControllerBase()
{
var config = new MapperConfiguration(cfg => {
cfg.AddProfile<DefaultMappingProfile>();
});
_mapper = config.CreateMapper();
}
}
DB items look like this:
[PrimaryKey("Id")]
public class Entity1: IDBItem
{
public int Id { get; set; }
public string Name { get; set; }
public float SomeField { get; set; }
}
So whenever context get acquired from inheriting classes - there's a check that database is created. On Android this works. on iOS in Release mode I'm not seeing any databae entries. I do see some entries on Simulator though. That confuses me even more. Can any1 suggest a solution please?
I've tried to clean up Nuget packages as initially I was using SQLite PCL one. Removing all SQLite and installing only EF Core and those that it pulled didn't solve the issue.
After a lot of digging I found out that the error was caused by "Attempting to JIT compile method InitializeComponent while running in aot-only mode"
It happened only in release mode on the device.
The issue was caused by absence if tag <UseInterpreter>true</UseInterpreter>
inside of the project file. Eventually it looked like this:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'">
[other option shere]
<MtouchLink>SdkOnly</MtouchLink>
<MtouchUseLlvm>False</MtouchUseLlvm>
<UseInterpreter>true</UseInterpreter>
</PropertyGroup>
Link to the original issue: https://github.com/xamarin/xamarin-macios/issues/17194