Skip to content

Realm accessed from incorrect thread #3339

Answered by nirinchev
dharamhbtik asked this question in Q&A
Discussion options

You must be logged in to vote

Okay, so the problem is likely related to the async calls - what happens is roughly this:

async Task RefreshData()
{
    // Thread A - we open the Realm
    using var realm = Realm.GetInstance(_realm.Config);
    _ =  realm.Refresh(); // explicit refresh only!
    var persons = realm.All<Person>();

    await DoSomethingAsync();

    // Continuation after the await is scheduled on Thread B

    realm.Refresh(); // Accessing realm on Thread B
}

If you want to do check for Realm changes in the background, it's better to install a synchronization context and rely on notifications. Something like (relies on Nito.AsyncEx.Context):

private void StartRefrshingData()
{
    var cts = new Cancellat…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dharamhbtik
Comment options

@nirinchev
Comment options

Answer selected by dharamhbtik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants