Package io.realm

Class RealmSet<E>

    • Constructor Detail

      • RealmSet

        public RealmSet()
        Instantiates a RealmSet in unmanaged mode.
      • RealmSet

        public RealmSet​(Collection<E> collection)
        Instantiates a RealmSet in unmanaged mode with another collection.
        Parameters:
        collection - the collection with which the set will be initially populated.
      • RealmSet

        public RealmSet​(io.realm.BaseRealm baseRealm,
                        io.realm.internal.OsSet osSet,
                        Class<E> valueClass)
        Instantiates a RealmSet in managed mode. This constructor is used internally by Realm.
        Parameters:
        baseRealm -
        osSet -
        valueClass -
      • RealmSet

        public RealmSet​(io.realm.BaseRealm baseRealm,
                        io.realm.internal.OsSet osSet,
                        String className)
        Instantiates a RealmSet in managed mode. This constructor is used internally by a Dynamic Realm.
        Parameters:
        baseRealm -
        osSet -
        className -
    • Method Detail

      • isManaged

        public boolean isManaged()
        Checks if the collection is managed by Realm. A managed collection is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed collection will be live-updated so it always points to the latest data. Managed collections are thread confined so that they cannot be accessed from other threads than the one that created them.

        If this method returns false, the collection is unmanaged. An unmanaged collection is just a normal java collection, so it will not be live updated.

        Specified by:
        isManaged in interface io.realm.internal.ManageableObject
        Specified by:
        isManaged in interface RealmCollection<E>
        Returns:
        true if this is a managed RealmCollection, false otherwise.
      • isValid

        public boolean isValid()
        Checks if the collection is still valid to use, i.e., the Realm instance hasn't been closed. It will always return true for an unmanaged collection.
        Specified by:
        isValid in interface io.realm.internal.ManageableObject
        Specified by:
        isValid in interface RealmCollection<E>
        Returns:
        true if it is still valid to use or an unmanaged collection, false otherwise.
      • isFrozen

        public boolean isFrozen()
        Specified by:
        isFrozen in interface io.realm.internal.ManageableObject
      • size

        public int size()
        Specified by:
        size in interface Collection<E>
        Specified by:
        size in interface Set<E>
      • contains

        public boolean contains​(@Nullable
                                Object o)
        Tests whether this Collection contains the specified object. Returns true if and only if at least one element elem in this Collection meets following requirement: (object==null ? elem==null : object.equals(elem)).
        Specified by:
        contains in interface Collection<E>
        Specified by:
        contains in interface RealmCollection<E>
        Specified by:
        contains in interface Set<E>
        Parameters:
        o - the object to search for.
        Returns:
        true if object is an element of this Collection, false otherwise.
      • freeze

        public RealmSet<E> freeze()
        Specified by:
        freeze in interface io.realm.internal.Freezable<E>
      • addChangeListener

        public void addChangeListener​(RealmChangeListener<RealmSet<E>> listener)
        Adds a change listener to this RealmSet.

        Registering a change listener will not prevent the underlying RealmSet from being garbage collected. If the RealmSet is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.

         
         public class MyActivity extends Activity {
        
             private RealmSet<Dog> dogs; // Strong reference to keep listeners alive
        
             \@Override
             protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               dogs = realm.where(Person.class).findFirst().getDogs();
               dogs.addChangeListener(new RealmChangeListener<RealmSet<Dog>>() {
                   \@Override
                   public void onChange(RealmSet<Dog> map) {
                       // React to change
                   }
               });
             }
         }
         
         
        Parameters:
        listener - the listener to be notified.
        Throws:
        IllegalArgumentException - if the change listener is null.
        IllegalStateException - if you try to add a listener from a non-Looper or IntentService thread.
      • addChangeListener

        public void addChangeListener​(SetChangeListener<E> listener)
        Adds a change listener to this RealmSet.

        Registering a change listener will not prevent the underlying RealmSet from being garbage collected. If the RealmSet is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.

         
         public class MyActivity extends Activity {
        
             private RealmSet<Dog> dogs; // Strong reference to keep listeners alive
        
             \@Override
             protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               dogs = realm.where(Person.class).findFirst().getDogs();
               dogs.addChangeListener(new SetChangeListener<Dog>() {
                   \@Override
                   public void onChange(RealmSet<Dog> set, SetChangeSet changeSet) {
                       // React to change
                   }
               });
             }
         }
         
         
        Parameters:
        listener - the listener to be notified.
        Throws:
        IllegalArgumentException - if the change listener is null.
        IllegalStateException - if you try to add a listener from a non-Looper or IntentService thread.
      • removeChangeListener

        public void removeChangeListener​(SetChangeListener<E> listener)
        Removes the specified change listener.
        Parameters:
        listener - the change listener to be removed.
        Throws:
        IllegalArgumentException - if the change listener is null.
        IllegalStateException - if you try to remove a listener from a non-Looper Thread.
      • removeAllChangeListeners

        public void removeAllChangeListeners()
        Removes all user-defined change listeners.
        Throws:
        IllegalStateException - if you try to remove listeners from a non-Looper Thread.
        See Also:
        RealmChangeListener
      • where

        public RealmQuery<E> where()
        Returns a RealmQuery, which can be used to query for specific objects of this class.
        Specified by:
        where in interface RealmCollection<E>
        Returns:
        a RealmQuery object.
        Throws:
        IllegalStateException - if Realm instance has been closed or parent object has been removed.
        See Also:
        RealmQuery
      • min

        @Nullable
        public Number min​(String fieldName)
        Finds the minimum value of a field.
        Specified by:
        min in interface RealmCollection<E>
        Parameters:
        fieldName - the field to look for a minimum on. Only number fields are supported.
        Returns:
        if no objects exist or they all have null as the value for the given field, null will be returned. Otherwise the minimum value is returned. When determining the minimum value, objects with null values are ignored.
      • max

        @Nullable
        public Number max​(String fieldName)
        Finds the maximum value of a field.
        Specified by:
        max in interface RealmCollection<E>
        Parameters:
        fieldName - the field to look for a maximum on. Only number fields are supported.
        Returns:
        if no objects exist or they all have null as the value for the given field, null will be returned. Otherwise the maximum value is returned. When determining the maximum value, objects with null values are ignored.
      • sum

        public Number sum​(String fieldName)
        Calculates the sum of a given field.
        Specified by:
        sum in interface RealmCollection<E>
        Parameters:
        fieldName - the field to sum. Only number fields are supported.
        Returns:
        the sum. If no objects exist or they all have null as the value for the given field, 0 will be returned. When computing the sum, objects with null values are ignored.
      • average

        public double average​(String fieldName)
        Returns the average of a given field.
        Specified by:
        average in interface RealmCollection<E>
        Parameters:
        fieldName - the field to calculate average on. Only number fields are supported.
        Returns:
        the average for the given field amongst objects in query results. This will be of type double for all types of number fields. If no objects exist or they all have null as the value for the given field, 0 will be returned. When computing the average, objects with null values are ignored.
      • maxDate

        @Nullable
        public Date maxDate​(String fieldName)
        Finds the maximum date.
        Specified by:
        maxDate in interface RealmCollection<E>
        Parameters:
        fieldName - the field to look for the maximum date. If fieldName is not of Date type, an exception is thrown.
        Returns:
        if no objects exist or they all have null as the value for the given date field, null will be returned. Otherwise the maximum date is returned. When determining the maximum date, objects with null values are ignored.
      • minDate

        @Nullable
        public Date minDate​(String fieldName)
        Finds the minimum date.
        Specified by:
        minDate in interface RealmCollection<E>
        Parameters:
        fieldName - the field to look for the minimum date. If fieldName is not of Date type, an exception is thrown.
        Returns:
        if no objects exist or they all have null as the value for the given date field, null will be returned. Otherwise the minimum date is returned. When determining the minimum date, objects with null values are ignored.
      • deleteAllFromRealm

        public boolean deleteAllFromRealm()
        This deletes all objects in the collection from the underlying Realm as well as from the collection.
        Specified by:
        deleteAllFromRealm in interface RealmCollection<E>
        Returns:
        true if objects was deleted, false otherwise.
      • isLoaded

        public boolean isLoaded()
        Checks if a collection has finished loading its data yet.
        Specified by:
        isLoaded in interface RealmCollection<E>
        Returns:
        true if data has been loaded and is available, false if data is still being loaded.
      • load

        public boolean load()
        Blocks the collection until all data are available.
        Specified by:
        load in interface RealmCollection<E>
        Returns:
        true if the data could be successfully loaded, false otherwise.
      • getValueClassName

        public String getValueClassName()
      • getValueClass

        public Class<E> getValueClass()