I am developing a dictionary app which has many interconnected tables, so when a user makes a query the app searches through almost all of those tables and across many their fields, which results in heavy queries to the database and UI freezes.
So, how can I make these queries in a separate task asynchronously each time updating the UI with the results?
I've seen findAllAsync and the like for android version of the realm but for .net I couldn't find any alternatives, I tried to reinitialize as suggested elsewhere the database each time I run the async, but somehow it doesn't work and give me the same error.
System.Exception: 'Realm accessed from incorrect thread.'
The error gets thrown on ToList() when I try to convert the realm results to normal list to handle on UI, please help to fix this behavior
Here is my code
using Data.Models;
using Microsoft.EntityFrameworkCore.Internal;
using Realms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class RealmAsync
{
IQueryable<Entry> Entries { get; set; }
public async void Run()
{
Realm RealmDatabase = Realm.GetInstance();
Entries = RealmDatabase.All<Entry>();
var entries = await FindAsync("а", Entries);
Console.WriteLine($"async result async {entries.Count()}");
}
public async Task<IEnumerable<Entry>> FindAsync(string needle, IQueryable<Entry> haystack)
{
var foregroundRealm = Realm.GetInstance();
var haystackRef = ThreadSafeReference.Create<Entry>(haystack);
var filteredEntriesRef = await Task.Run(() =>
{
using (var realm = Realm.GetInstance())
{
var bgHaystack = realm.ResolveReference(haystackRef);
return ThreadSafeReference.Create(bgHaystack.Where(entry => entry.Content.ToLower().StartsWith(needle)));
}
});
var result = foregroundRealm.ResolveReference(filteredEntriesRef).ToArray();
return result;
}
}
Entry model class:
using System.ComponentModel.DataAnnotations.Schema;
using Realms;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System;
namespace Data.Models
{
[Table("entry")]
public class Entry : RealmObject
{
public class EntryType
{
public const byte Word = 1;
public const byte Phrase = 2;
public const byte Text = 3;
};
[Key]
[PrimaryKey]
[Column("entry_id")]
public int Id { get; set; }
[Column("user_id")]
public int UserId { get; set; }
[Column("source_id")]
public int SourceId { get; set; }
[Indexed]
[Column("type")]
public byte Type { get; set; }
[Column("rate")]
public int Rate { get; set; }
[Column("created_at")]
public string CreatedAt { get; set; }
[Column("updated_at")]
public string UpdatedAt { get; set; }
[NotMapped]
public Phrase Phrase { get; set; }
[NotMapped]
public Word Word { get; set; }
[NotMapped]
public Text Text { get; set; }
[NotMapped]
public IList<Translation> Translations { get; }
[NotMapped]
public string Content
{
get {
switch (Type)
{
case EntryType.Phrase:
return Phrase?.Content;
case EntryType.Word:
return Word?.Content;
case EntryType.Text:
return Text?.Content;
}
return "";
}
}
}
}