RLMResults

Objective-C

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

Swift

@_nonSendable(_assumed) class RLMResults<RLMObjectType> : NSObject, RLMCollection, NSFastEnumeration where RLMObjectType : AnyObject

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.

Properties

  • The number of objects in the results collection.

    Declaration

    Objective-C

    @property (nonatomic, readonly) NSUInteger count;

    Swift

    var count: UInt { get }
  • The type of the objects in the results collection.

    Declaration

    Objective-C

    @property (nonatomic, readonly) RLMPropertyType type;

    Swift

    var type: RLMPropertyType { get }
  • Indicates whether the objects in the collection can be nil.

    Declaration

    Objective-C

    @property (nonatomic, getter=isOptional) BOOL optional;

    Swift

    var isOptional: Bool { get set }
  • The class name of the objects contained in the results collection.

    Will be nil if type is not RLMPropertyTypeObject.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly, nullable) NSString *objectClassName;

    Swift

    var objectClassName: String? { get }
  • The Realm which manages this results collection.

    Declaration

    Objective-C

    @property (nonatomic, readonly) RLMRealm *_Nonnull realm;

    Swift

    var realm: RLMRealm { get }
  • 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 (nonatomic, readonly, getter=isInvalidated) BOOL invalidated;

    Swift

    var isInvalidated: Bool { get }

Accessing Objects from an RLMResults

  • Returns the object at the index specified.

    Declaration

    Objective-C

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

    Swift

    func object(at index: UInt) -> RLMObjectType

    Parameters

    index

    The index to look up.

    Return Value

    An object of the type contained in the results collection.

  • Returns an array containing the objects in the results at the indexes specified by a given index set. nil will be returned if the index set contains an index out of the arrays bounds.

    Declaration

    Objective-C

    - (nullable NSArray<RLMObjectType> *)objectsAtIndexes:
        (nonnull NSIndexSet *)indexes;

    Swift

    func objects(at indexes: IndexSet) -> [RLMObjectType]?

    Parameters

    indexes

    The indexes in the results to retrieve objects from.

    Return Value

    The objects at the specified indexes.

  • Returns the first object in the results collection.

    Returns nil if called on an empty results collection.

    Declaration

    Objective-C

    - (nullable RLMObjectType)firstObject;

    Swift

    func firstObject() -> RLMObjectType?

    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;

    Swift

    func lastObject() -> RLMObjectType?

    Return Value

    An object of the type contained in the results collection.

Querying Results

  • 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;

    Swift

    func index(of object: RLMObjectType) -> UInt

    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;

    Swift

    func indexOfObject(with predicate: NSPredicate) -> UInt

    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;

    Swift

    func objects(with predicate: NSPredicate) -> RLMResults<RLMObjectType>

    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;

    Swift

    func sortedResults(usingKeyPath keyPath: String, ascending: Bool) -> RLMResults<RLMObjectType>

    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;

    Swift

    func sortedResults(using properties: [RLMSortDescriptor]) -> RLMResults<RLMObjectType>

    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;

    Swift

    func distinctResults(usingKeyPaths keyPaths: [String]) -> RLMResults<RLMObjectType>

    Parameters

    keyPaths

    The key paths used produce distinct results

    Return Value

    An RLMResults made distinct based on the specified key paths

Notifications

  • 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.

    The error parameter is present only for backwards compatibility and will always be nil.

    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;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMResults<RLMObjectType>?, RLMCollectionChange?, Error?) -> Void) -> RLMNotificationToken

    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.

  • 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.

    The error parameter is present only for backwards compatibility and will always be nil.

    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 on the given queue. If the queue is blocked and notifications can’t be delivered instantly, multiple notifications may be coalesced into a single notification.

    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 when the containing Realm is read-only or frozen.

    Warning

    The queue must be a serial queue.

    Declaration

    Objective-C

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

    Swift

    func addNotificationBlock(_ block: @escaping (RLMResults<RLMObjectType>?, RLMCollectionChange?, Error?) -> Void, queue: dispatch_queue_t?) -> RLMNotificationToken

    Parameters

    block

    The block to be called whenever a change occurs.

    queue

    The serial queue to deliver notifications to.

    Return Value

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

  • 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.

    The error parameter is present only for backwards compatibility and will always be nil.

    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 on the given queue. If the queue is blocked and notifications can’t be delivered instantly, multiple notifications may be coalesced into a single notification.

    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 when the containing Realm is read-only or frozen.

    Warning

    The queue must be a serial queue.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)
        addNotificationBlock:(nonnull void (^)(RLMResults<RLMObjectType> *_Nullable,
                                               RLMCollectionChange *_Nullable,
                                               NSError *_Nullable))block
                    keyPaths:(nullable NSArray<NSString *> *)keyPaths
                       queue:(nullable dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMResults<RLMObjectType>?, RLMCollectionChange?, Error?) -> Void, keyPaths: [String]?, queue: dispatch_queue_t?) -> RLMNotificationToken

    Parameters

    block

    The block to be called whenever a change occurs.

    queue

    The serial queue to deliver notifications to.

    keyPaths

    The block will be called for changes occurring on these keypaths. If no key paths are given, notifications are delivered for every property key path.

    Return Value

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

  • 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.

    The error parameter is present only for backwards compatibility and will always be nil.

    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.

    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 when the containing Realm is read-only or frozen.

    Warning

    The queue must be a serial queue.

    Declaration

    Objective-C

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

    Swift

    func addNotificationBlock(_ block: @escaping (RLMResults<RLMObjectType>?, RLMCollectionChange?, Error?) -> Void, keyPaths: [String]?) -> RLMNotificationToken

    Parameters

    block

    The block to be called whenever a change occurs.

    keyPaths

    The block will be called for changes occurring on these keypaths. If no key paths are given, notifications are delivered for every property key path.

    Return Value

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

Flexible Sync

  • Creates a RLMSyncSubscription matching the RLMResults’s local filter.

    After committing the subscription to the realm’s local subscription set, the method will wait for downloads according to RLMWaitForSyncMode behavior.

    Unnamed subscriptions

    If subscribeWithCompletion: is called without a name whose query matches an unnamed subscription, another subscription is not created.

    If subscribeWithCompletion: is called without a name whose query matches a named subscription, an additional unnamed subscription is created.

    Named Subscriptions

    If subscribeWithCompletion: is called with a name whose query matches an unnamed subscription, an additional named subscription is created.

    Existing name and query

    If subscribeWithCompletion: is called with a name whose name is taken on a different query, the old subscription is updated with the new query.

    Note

    This method opens an update block transaction that creates or updates a subscription. It’s advised to not loop over this method in order to create multiple subscriptions at once. This could create a performance bottleneck by opening multiple unnecessary write transactions. @see: [RLMSyncSubscription update:queue:onComplete:] in order to create multiple subscriptions.

    Warning

    This API is currently in Preview and may be subject to changes in the future.

    Declaration

    Objective-C

    - (void)subscribeWithCompletionOnQueue:(dispatch_queue_t _Nullable)queue
                                completion:
                                    (nonnull RLMResultsCompletionBlock)completion;

    Swift

    func subscribeWithCompletion(on queue: dispatch_queue_t?) async throws -> RLMResults<AnyObject>

    Parameters

    queue

    The queue where the completion dispatches.

    completion

    The completion block called after the subscription completes. The callback will wait for downloads according to the value in waitForSyncMode.

  • Creates a RLMSyncSubscription matching the RLMResults’s local filter.

    After committing the subscription to the realm’s local subscription set, the method will wait for downloads according to RLMWaitForSyncMode behavior.

    Unnamed subscriptions

    If subscribeWithCompletion: is called without a name whose query matches an unnamed subscription, another subscription is not created.

    If subscribeWithCompletion: is called without a name whose query matches a named subscription, an additional unnamed subscription is created.

    Named Subscriptions

    If subscribeWithCompletion: is called with a name whose query matches an unnamed subscription, an additional named subscription is created.

    Existing name and query

    If subscribeWithCompletion: is called with a name whose name is taken on a different query, the old subscription is updated with the new query.

    Note

    This method opens an update block transaction that creates or updates a subscription. It’s advised to not loop over this method in order to create multiple subscriptions at once. This could create a performance bottleneck by opening multiple unnecessary write transactions. @see: [RLMSyncSubscription update:queue:onComplete:] in order to create multiple subscriptions.

    Warning

    This API is currently in Preview and may be subject to changes in the future.

    Declaration

    Objective-C

    - (void)subscribeWithName:(NSString *_Nullable)name
                      onQueue:(dispatch_queue_t _Nullable)queue
                   completion:(nonnull RLMResultsCompletionBlock)completion;

    Swift

    func subscribe(withName name: String?, on queue: dispatch_queue_t?) async throws -> RLMResults<AnyObject>

    Parameters

    name

    The name used to identify the subscription.

    queue

    The queue where the completion dispatches.

    completion

    The completion block called after the subscription completes. The callback will wait for downloads according to the value in waitForSyncMode.

  • Creates a RLMSyncSubscription matching the RLMResults’s local filter.

    After committing the subscription to the realm’s local subscription set, the method will wait for downloads according to the RLMWaitForSyncMode.

    Unnamed subscriptions

    If subscribeWithCompletion: is called without a name whose query matches an unnamed subscription, another subscription is not created.

    If subscribeWithCompletion: is called without a name whose query matches a named subscription, an additional unnamed subscription is created.

    Named Subscriptions

    If subscribeWithCompletion: is called with a name whose query matches an unnamed subscription, an additional named subscription is created.

    Existing name and query

    If subscribeWithCompletion: is called with a name whose name is taken on a different query, the old subscription is updated with the new query.

    Note

    This method opens an update block transaction that creates or updates a subscription. It’s advised to not loop over this method in order to create multiple subscriptions at once. This could create a performance bottleneck by opening multiple unnecessary write transactions. @see: [RLMSyncSubscription update:queue:onComplete:] in order to create multiple subscriptions.

    Warning

    This API is currently in Preview and may be subject to changes in the future.

    Declaration

    Objective-C

    - (void)subscribeWithName:(NSString *_Nullable)name
                  waitForSync:(RLMWaitForSyncMode)waitForSyncMode
                      onQueue:(dispatch_queue_t _Nullable)queue
                   completion:(nonnull RLMResultsCompletionBlock)completion;

    Swift

    @_unsafeInheritExecutor func subscribe(withName name: String?, waitForSync waitForSyncMode: WaitForSyncMode, on queue: dispatch_queue_t?) async throws -> RLMResults<AnyObject>

    Parameters

    name

    The name used to identify the subscription.

    waitForSyncMode

    Dictates when the completion handler is called

    queue

    The queue where the completion dispatches.

    completion

    The completion block called after the subscription completes. The callback will wait for downloads according to the value in waitForSyncMode.

  • Creates a RLMSyncSubscription matching the RLMResults’s local filter.

    After committing the subscription to the realm’s local subscription set, the method will wait for downloads according to the RLMWaitForSyncMode.

    Unnamed subscriptions

    If subscribeWithCompletion: is called without a name whose query matches an unnamed subscription, another subscription is not created.

    If subscribeWithCompletion: is called without a name whose query matches a named subscription, an additional unnamed subscription is created.

    Named Subscriptions

    If subscribeWithCompletion: is called with a name whose query matches an unnamed subscription, an additional named subscription is created.

    Existing name and query

    If subscribeWithCompletion: is called with a name whose name is taken on a different query, the old subscription is updated with the new query.

    Note

    This method opens an update block transaction that creates or updates a subscription. It’s advised to not loop over this method in order to create multiple subscriptions at once. This could create a performance bottleneck by opening multiple unnecessary write transactions. @see: [RLMSyncSubscription update:queue:onComplete:] in order to create multiple subscriptions.

    Warning

    This API is currently in Preview and may be subject to changes in the future.

    Declaration

    Objective-C

    - (void)subscribeWithName:(NSString *_Nullable)name
                  waitForSync:(RLMWaitForSyncMode)waitForSyncMode
                      onQueue:(dispatch_queue_t _Nullable)queue
                      timeout:(NSTimeInterval)timeout
                   completion:(nonnull RLMResultsCompletionBlock)completion;

    Swift

    @_unsafeInheritExecutor func subscribe(withName name: String?, waitForSync waitForSyncMode: WaitForSyncMode, on queue: dispatch_queue_t?, timeout: TimeInterval) async throws -> RLMResults<AnyObject>

    Parameters

    name

    The name used to identify the subscription.

    waitForSyncMode

    Dictates when the completion handler is called

    queue

    The queue where the completion dispatches.

    timeout

    A timeout which ends waiting for downloads via the completion handler. If the timeout is exceeded the completion handler returns an error.

    completion

    The completion block called after the subscription completes. The callback will wait for downloads according to the value in waitForSyncMode.

  • Removes a RLMSubscription matching the RLMResults'slocal filter.

    The method returns after committing the subscription removal to the realm’s local subscription set. Calling this method will not wait for objects to be removed from the realm.

    Calling unsubscribe on a RLMResults does not remove the local filter from the RLMResults. After calling unsubscribe, RLMResults may still contain objects because other subscriptions may exist in the RLMRealm’s subscription set.

    Note

    In order for a named subscription to be removed, the RLMResults must have previously created the subscription. The RLMResults returned in the completion block when calling subscribe can be used to unsubscribe from the same subscription.

    Note

    This method opens an update block transaction that creates or updates a subscription. It’s advised to not loop over this method in order to create multiple subscriptions at once. This could create a performance bottleneck by opening multiple unnecessary write transactions. @see: [RLMSyncSubscription update:queue:onComplete:] in order to create multiple subscriptions.

    Warning

    This API is currently in Preview and may be subject to changes in the future.

    Declaration

    Objective-C

    - (void)unsubscribe;

    Swift

    func unsubscribe()

Aggregating Property Values

  • 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;

    Swift

    func min(ofProperty property: String) -> Any?

    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;

    Swift

    func max(ofProperty property: String) -> Any?

    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;

    Swift

    func sum(ofProperty property: String) -> NSNumber

    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;

    Swift

    func average(ofProperty property: String) -> NSNumber?

    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.

Sectioned Results

Freeze

  • Indicates if the result are frozen.

    Frozen Results are immutable and can be accessed from any thread.The objects read from a frozen Results will also be frozen.

    Declaration

    Objective-C

    @property (nonatomic, readonly, getter=isFrozen) BOOL frozen;

    Swift

    var isFrozen: Bool { get }
  • Returns a frozen (immutable) snapshot of these results.

    The frozen copy is an immutable collection which contains the same data as this collection currently contains, but will not update when writes are made to the containing Realm. Unlike live Results, frozen Results can be accessed from any thread.

    Warning

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

    Warning

    Holding onto a frozen collection for an extended period while performing write transaction on the Realm may result in the Realm file growing to large sizes. See RLMRealmConfiguration.maximumNumberOfActiveVersions for more information.

    Declaration

    Objective-C

    - (nonnull instancetype)freeze;

    Swift

    func freeze() -> Self
  • Returns a live version of this frozen collection.

    This method resolves a reference to a live copy of the same frozen collection. If called on a live collection, will return itself.

    Declaration

    Objective-C

    - (nonnull instancetype)thaw;

    Swift

    func thaw() -> Self

Unavailable Methods

  • Unavailable

    RLMResults cannot be created directly

    -[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;
  • Unavailable

    RLMResults cannot be created directly

    +[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;