question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callback for Realm file events: Created/deleted/migrated

See original GitHub issue

Goal

My goal is to run initialData transaction asynchronously. However, given my Android application use case, I don’t think the new Realm.getInstanceAsync is the best fit.

Expected Results

Get Realm instance synchronously, but execute initialData transaction asynchronously.

Actual Results

With Realm.getInstanceAsync, introduced in Realm 3.2.0, a Realm object is not returned synchronously.

That has implications when you are using dependency injection for the Realm instance, as it would make the workflow much more complex than it needs to be (should I not be using it at all?).

Right now, to initialize data, I’m simply making use of SharedPreferences and running a transaction asynchronously.

Version of Realm

Realm version: 3.2.0

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
cmelchiorcommented, May 19, 2017

Hi @jpmcosta No, feature requests should go here 😃

It is an interesting use case. By doing an async transaction it also means that you are OK with the Realm instance not being “fully” initialized?

The natural option would be to add something like RealmConfiguration.initialDataAsync() but calling something “initial*” kinda implies that it is setup when the Realm is opened, so that doesn’t feel right to me.

If I remember correctly we have heard other use cases for doing other things when a Realm is created, like resetting SharedPrefs or similar (need to dig those up). So I’m thinking that perhaps we should expose a lifecylecallback whenever the Realm is created/reset, and then you could do whatever. Something like:

RealmConfiguration config = new RealmConfiguration.Builder()
  .realmHandler(new RealmCreatedCallback() {
      @override
      public void onRealmCreated(RealmConfiguration config) {
         // Callback when the Realm file is created the first time. Called after inititialData/assetFile has been executed
      }
  })
 .build();

Naming and scope is up for debate of course:

Name:

  • .realmHandler(...)
  • .onRealmCreated(...)
  • .lifecycleHandler(...)
  • .realmFileHandler(...)
  • .eventHandler(...)
  • Other ideas?

Possible scope:

  • onRealmCreated
  • onRealmMigrated
  • onRealmDeleted
  • others?

Would something like that work for you?

1reaction
jpmcostacommented, May 19, 2017

Would something like that work for you?

@cmelchior Yes, that would be perfect! I also envisioned something like initialDataAsync, but the lifecyclecallback should work much better.

Edit: I didn’t read the comma! Thanks for your answer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DynamicRealm (Realm 10.10.1) - MongoDB
The Callback used when reporting back the result of loading a Realm asynchronously using either Realm.getInstanceAsync(RealmConfiguration, Realm.Callback) or ...
Read more >
Secondary .realm file getInstance taking too long
I solved this problem by caching the realm instance: private Realm auxiliaryRealmInstance; fun getAuxiliaryRealmInstance(): Realm{ return ...
Read more >
Event Handling in the Realm Object Server
The Realm way eschews that entire concept of data translation because all we really do is have Realm files on the device and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found