RLMResults

@interface RLMResults <RLMObjectType> : NSObject<RLMCollection,NSFastEnumeration> 

RLMResults is an auto-updating container type in Realm returned from object queries. It represents the results of the query in the form of a collection of objects.

RLMResults can be queried using the same predicates as RLMObject and RLMArray, and you can chain queries to further filter results.

RLMResults always reflect the current state of the Realm on the current thread, including during write transactions on the current thread. The one exception to this is when using for...in fast enumeration, which will always enumerate over the objects which matched the query when the enumeration is begun, even if some of them are deleted or modified to be excluded by the filter during the enumeration.

RLMResults are lazily evaluated the first time they are accessed; they only run queries when the result of the query is requested. This means that chaining several temporary RLMResults to sort and filter your data does not perform any extra work processing the intermediate state.

Once the results have been evaluated or a notification block has been added, the results are eagerly kept up-to-date, with the work done to keep them up-to-date done on a background thread whenever possible.

RLMResults cannot be directly instantiated.

  • The number of objects in the results collection.

    Declaration

    Objective-C

    @property (readonly, assign, nonatomic) NSUInteger count;
  • The type of the objects in the results collection.

    Declaration

    Objective-C

    @property (readonly, assign, nonatomic) RLMPropertyType type;
  • Indicates whether the objects in the collection can be nil.

    Declaration

    Objective-C

    @property (getter=isOptional, assign, readwrite, nonatomic) BOOL optional;
  • The class name of the objects contained in the results collection.

    Will be nil if type is not RLMPropertyTypeObject.

    Declaration

    Objective-C

    @property (readonly, copy, nonatomic, nullable) NSString *objectClassName;
  • The Realm which manages this results collection.

    Declaration

    Objective-C

    @property (readonly, nonatomic) RLMRealm *_Nonnull realm;
  • Indicates if the results collection is no longer valid.

    The results collection becomes invalid if invalidate is called on the containing realm. An invalidated results collection can be accessed, but will always be empty.

    Declaration

    Objective-C

    @property (readonly, getter=isInvalidated, nonatomic) BOOL invalidated;
  • Returns the object at the index specified.

    Declaration

    Objective-C

    - (nonnull RLMObjectType)objectAtIndex:(NSUInteger)index;

    Parameters

    index

    The index to look up.

    Return Value

    An object of the type contained in the results collection.

  • Returns the first object in the results collection.

    Returns nil if called on an empty results collection.

    Declaration

    Objective-C

    - (nullable RLMObjectType)firstObject;

    Return Value

    An object of the type contained in the results collection.

  • Returns the last object in the results collection.

    Returns nil if called on an empty results collection.

    Declaration

    Objective-C

    - (nullable RLMObjectType)lastObject;

    Return Value

    An object of the type contained in the results collection.

  • Returns the index of an object in the results collection.

    Returns NSNotFound if the object is not found in the results collection.

    Declaration

    Objective-C

    - (NSUInteger)indexOfObject:(nonnull RLMObjectType)object;

    Parameters

    object

    An object (of the same type as returned from the objectClassName selector).

  • Returns the index of the first object in the results collection matching the predicate.

    Declaration

    Objective-C

    - (NSUInteger)indexOfObjectWhere:(nonnull NSString *)predicateFormat, ...;

    Parameters

    predicateFormat

    A predicate format string, optionally followed by a variable number of arguments.

    Return Value

    The index of the object, or NSNotFound if the object is not found in the results collection.

  • Returns the index of the first object in the results collection matching the predicate.

    Declaration

    Objective-C

    - (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;

    Parameters

    predicate

    The predicate with which to filter the objects.

    Return Value

    The index of the object, or NSNotFound if the object is not found in the results collection.

  • Returns all the objects matching the given predicate in the results collection.

    Declaration

    Objective-C

    - (nonnull RLMResults<RLMObjectType> *)objectsWhere:
        (nonnull NSString *)predicateFormat, ...;

    Parameters

    predicateFormat

    A predicate format string, optionally followed by a variable number of arguments.

    Return Value

    An RLMResults of objects that match the given predicate.

  • Returns all the objects matching the given predicate in the results collection.

    Declaration

    Objective-C

    - (nonnull RLMResults<RLMObjectType> *)objectsWithPredicate:
        (nonnull NSPredicate *)predicate;

    Parameters

    predicate

    The predicate with which to filter the objects.

    Return Value

    An RLMResults of objects that match the given predicate.

  • Returns a sorted RLMResults from an existing results collection.

    Declaration

    Objective-C

    - (nonnull RLMResults<RLMObjectType> *)
    sortedResultsUsingKeyPath:(nonnull NSString *)keyPath
                    ascending:(BOOL)ascending;

    Parameters

    keyPath

    The key path to sort by.

    ascending

    The direction to sort in.

    Return Value

    An RLMResults sorted by the specified key path.

  • Returns a sorted RLMResults from an existing results collection.

    Declaration

    Objective-C

    - (nonnull RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors:
        (nonnull NSArray<RLMSortDescriptor *> *)properties;

    Parameters

    properties

    An array of RLMSortDescriptors to sort by.

    Return Value

    An RLMResults sorted by the specified properties.

  • Returns a distinct RLMResults from an existing results collection.

    Declaration

    Objective-C

    - (nonnull RLMResults<RLMObjectType> *)distinctResultsUsingKeyPaths:
        (nonnull NSArray<NSString *> *)keyPaths;

    Parameters

    keyPaths

    The key paths used produce distinct results

    Return Value

    An RLMResults made distinct based on the specified key paths

  • Registers a block to be called each time the results collection changes.

    The block will be asynchronously called with the initial results collection, and then called again after each write transaction which changes either any of the objects in the results, or which objects are in the results.

    The change parameter will be nil the first time the block is called. For each call after that, it will contain information about which rows in the results collection were added, removed or modified. If a write transaction did not modify any objects in the results collection, the block is not called at all. See the RLMCollectionChange documentation for information on how the changes are reported and an example of updating a UITableView.

    If an error occurs the block will be called with nil for the results parameter and a non-nil error. Currently the only errors that can occur are when opening the Realm on the background worker thread.

    At the time when the block is called, the RLMResults object will be fully evaluated and up-to-date, and as long as you do not perform a write transaction on the same thread or explicitly call -[RLMRealm refresh], accessing it will never perform blocking work.

    Notifications are delivered via the standard run loop, and so can’t be delivered while the run loop is blocked by other activity. When notifications can’t be delivered instantly, multiple notifications may be coalesced into a single notification. This can include the notification with the initial results. For example, the following code performs a write transaction immediately after adding the notification block, so there is no opportunity for the initial notification to be delivered first. As a result, the initial notification will reflect the state of the Realm after the write transaction.

    RLMResults<Dog *> *results = [Dog allObjects];
    NSLog(@"dogs.count: %zu", dogs.count); // => 0
    self.token = [results addNotificationBlock:^(RLMResults *dogs,
                                                 RLMCollectionChange *changes,
                                                 NSError *error) {
        // Only fired once for the example
        NSLog(@"dogs.count: %zu", dogs.count); // => 1
    }];
    [realm transactionWithBlock:^{
        Dog *dog = [[Dog alloc] init];
        dog.name = @"Rex";
        [realm addObject:dog];
    }];
    // end of run loop execution context
    

    You must retain the returned token for as long as you want updates to continue to be sent to the block. To stop receiving updates, call -invalidate on the token.

    Warning

    This method cannot be called during a write transaction, or when the containing Realm is read-only.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)addNotificationBlock:
        (nonnull void (^)(RLMResults<RLMObjectType> *_Nullable,
                          RLMCollectionChange *_Nullable, NSError *_Nullable))block;

    Parameters

    block

    The block to be called whenever a change occurs.

    Return Value

    A token which must be held for as long as you want updates to be delivered.

  • Returns the minimum (lowest) value of the given property among all the objects represented by the results collection.

    NSNumber *min = [results minOfProperty:@"age"];
    

    Warning

    You cannot use this method on RLMObject, RLMArray, and NSData properties.

    Declaration

    Objective-C

    - (nullable id)minOfProperty:(nonnull NSString *)property;

    Parameters

    property

    The property whose minimum value is desired. Only properties of types int, float, double, and NSDate are supported.

    Return Value

    The minimum value of the property, or nil if the Results are empty.

  • Returns the maximum (highest) value of the given property among all the objects represented by the results collection.

    NSNumber *max = [results maxOfProperty:@"age"];
    

    Warning

    You cannot use this method on RLMObject, RLMArray, and NSData properties.

    Declaration

    Objective-C

    - (nullable id)maxOfProperty:(nonnull NSString *)property;

    Parameters

    property

    The property whose maximum value is desired. Only properties of types int, float, double, and NSDate are supported.

    Return Value

    The maximum value of the property, or nil if the Results are empty.

  • Returns the sum of the values of a given property over all the objects represented by the results collection.

    NSNumber *sum = [results sumOfProperty:@"age"];
    

    Warning

    You cannot use this method on RLMObject, RLMArray, and NSData properties.

    Declaration

    Objective-C

    - (nonnull NSNumber *)sumOfProperty:(nonnull NSString *)property;

    Parameters

    property

    The property whose values should be summed. Only properties of types int, float, and double are supported.

    Return Value

    The sum of the given property.

  • Returns the average value of a given property over the objects represented by the results collection.

    NSNumber *average = [results averageOfProperty:@"age"];
    

    Warning

    You cannot use this method on RLMObject, RLMArray, and NSData properties.

    Declaration

    Objective-C

    - (nullable NSNumber *)averageOfProperty:(nonnull NSString *)property;

    Parameters

    property

    The property whose average value should be calculated. Only properties of types int, float, and double are supported.

    Return Value

    The average value of the given property, or nil if the Results are empty.

  • -[RLMResults init] is not available because RLMResults cannot be created directly. RLMResults can be obtained by querying a Realm.

    Declaration

    Objective-C

    - (nonnull instancetype)init;
  • +[RLMResults new] is not available because RLMResults cannot be created directly. RLMResults can be obtained by querying a Realm.

    Declaration

    Objective-C

    + (nonnull instancetype) new;
  • Subscribe to the query represented by this RLMResults.

    Subscribing to a query asks the server to synchronize all objects to the client which match the query, along with all objects which are reachable from those objects via links. This happens asynchronously, and the local client Realm may not immediately have all objects which match the query. Observe the state property of the returned subscription object to be notified of when the subscription has been processed by the server and all objects matching the query are available.

    The subscription will not be explicitly named. A name will be automatically generated for internal use. The exact format of this name may change without warning and should not be depended on.

    See

    RLMSyncSubscription

    Declaration

    Objective-C

    - (nonnull RLMSyncSubscription *)subscribe;

    Swift

    func subscribe() -> RLMSyncSubscription

    Return Value

    An object representing the newly-created subscription.

  • Subscribe to the query represented by this RLMResults.

    Subscribing to a query asks the server to synchronize all objects to the client which match the query, along with all objects which are reachable from those objects via links. This happens asynchronously, and the local client Realm may not immediately have all objects which match the query. Observe the state property of the returned subscription object to be notified of when the subscription has been processed by the server and all objects matching the query are available.

    Creating a new subscription with the same name and query as an existing subscription will not create a new subscription, but instead will return an object referring to the existing sync subscription. This means that performing the same subscription twice followed by removing it once will result in no subscription existing.

    The newly created subscription will not be reported by -[RLMRealm subscriptions] or -[RLMRealm subscriptionWithName:] until state has transitioned from RLMSyncSubscriptionStateCreating to any of the other states.

    See

    RLMSyncSubscription

    Declaration

    Objective-C

    - (nonnull RLMSyncSubscription *)subscribeWithName:
        (nullable NSString *)subscriptionName;

    Swift

    func subscribe(withName subscriptionName: String?) -> RLMSyncSubscription

    Parameters

    subscriptionName

    The name of the subscription.

    Return Value

    An object representing the newly-created subscription.

  • Subscribe to a subset of the query represented by this RLMResults.

    Subscribing to a query asks the server to synchronize all objects to the client which match the query, along with all objects which are reachable from those objects via links. This happens asynchronously, and the local client Realm may not immediately have all objects which match the query. Observe the state property of the returned subscription object to be notified of when the subscription has been processed by the server and all objects matching the query are available.

    Creating a new subscription with the same name and query as an existing subscription will not create a new subscription, but instead will return an object referring to the existing sync subscription. This means that performing the same subscription twice followed by removing it once will result in no subscription existing.

    The newly created subscription will not be reported by -[RLMRealm subscriptions] or -[RLMRealm subscriptionWithName:] until state has transitioned from RLMSyncSubscriptionStateCreating to any of the other states.

    The number of top-level matches may optionally be limited. This limit respects the sort and distinct order of the query being subscribed to, if any. Please note that the limit does not count or apply to objects which are added indirectly due to being linked to by the objects in the subscription. If the limit is larger than the number of objects which match the query, all objects will be included.

    See

    RLMSyncSubscription

    Declaration

    Objective-C

    - (nonnull RLMSyncSubscription *)subscribeWithName:
                                         (nullable NSString *)subscriptionName
                                                 limit:(NSUInteger)limit;

    Swift

    func subscribe(withName subscriptionName: String?, limit: UInt) -> RLMSyncSubscription

    Parameters

    subscriptionName

    The name of the subscription

    limit

    The maximum number of objects to include in the subscription.

    Return Value

    The subscription