Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm Android: what is mean by isValid(), isLoaded(), isManaged()?

Tags:

android

realm

In Realm there are three methods isValid(), isLoaded(), isManaged(). I want to know which method should used when?

like image 637
Rajesh Nasit Avatar asked Mar 06 '23 06:03

Rajesh Nasit


2 Answers

isValid

public final boolean isValid()

Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the Realm been closed. It will always return true for unmanaged objects.

Two things invalidate a RealmObject:

- closing the Realm instance it belongs to and deleting the object on any thread (if you're on an auto-updating looper thread)

isLoaded

public final boolean isLoaded()

Checks if the query used to find this RealmObject has completed. Async methods like RealmQuery.findFirstAsync() return an RealmObject that represents the future result of the RealmQuery. It can be considered similar to a Future in this regard.

Once isLoaded() returns true, the object represents the query result even if the query didn't find any object matching the query parameters. In this case the RealmObject will become a "null" object.

isManaged

public static boolean isManaged(E object)

Checks if this object is managed by Realm. A managed object is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed object will be live-updated so it always points to the latest data. It is possible to register a change listener using addChangeListener(RealmModel, RealmChangeListener) to be notified when changes happen. Managed objects are thread confined so that they cannot be accessed from other threads than the one that created them.

Instances of Realm objects can be either managed or unmanaged. Managed objects are persisted in Realm, are always up to date and thread confined. They are generally more lightweight than the unmanaged version as they take up less space on the Java heap. Unmanaged objects are just like ordinary Java objects, they are not persisted and they will not be updated automatically. They can be moved freely across threads.

More info refer:https://realm.io/docs/java/4.3.3/api/io/realm/RealmObject.html

like image 84
Rohan Lodhi Avatar answered Mar 23 '23 08:03

Rohan Lodhi


FROM DOCS

  1. isValid()
  • Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the Realm been closed. It will always return true for unmanaged objects.

  • Note that this can be used to check the validity of certain conditions such as being null when observed.

    EXAMPLE :

    // With RealmObject
    yourRealmObject.isValid();
    
  1. isLoaded()
  • Checks if the query used to find this RealmObject has completed.
  • Returns: true if the query has completed, false if the query is in progress.

3. isManaged()

  • Checks if this object is managed by Realm. A managed object is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed object will be live-updated so it always points to the latest data. It is possible to register a change listener using addChangeListener(RealmModel, RealmChangeListener) to be notified when changes happen. Managed objects are thread confined so that they cannot be accessed from other threads than the one that created them.

  • If this method returns false, the object is unmanaged. An unmanaged object is just a normal Java object, so it can be parsed freely across threads, but the data in the object is not connected to the underlying Realm, so it will not be live updated.

like image 21
AskNilesh Avatar answered Mar 23 '23 09:03

AskNilesh