RLMSectionedResults

Objective-C

@interface RLMSectionedResults<RLMKeyType : id <RLMValue>,
                               RLMObjectType : id <RLMValue>>
    : NSObject <RLMSectionedResult>

Swift

class RLMSectionedResults<RLMKeyType, RLMObjectType> : NSObject, RLMSectionedResult where RLMKeyType : RLMValue, RLMObjectType : RLMValue

A lazily evaluated collection that holds elements in sections determined by a section key.

  • An array of all keys in the sectioned results collection.

    Declaration

    Objective-C

    @property (nonatomic) NSArray<RLMKeyType> *_Nonnull allKeys;

    Swift

    var allKeys: [RLMKeyType] { get set }
  • The total amount of sections in this collection.

    Declaration

    Objective-C

    @property (nonatomic, readonly) NSUInteger count;

    Swift

    var count: UInt { get }
  • Returns the section at a given index.

    Declaration

    Objective-C

    - (nonnull RLMSection<RLMKeyType, RLMObjectType> *)objectAtIndexedSubscript:
        (NSUInteger)index;

    Swift

    subscript(index: UInt) -> RLMSection<RLMKeyType, RLMObjectType> { get }
  • Returns the section at a given index.

    Declaration

    Objective-C

    - (nonnull RLMSection<RLMKeyType, RLMObjectType> *)objectAtIndex:
        (NSUInteger)index;

    Swift

    func object(at index: UInt) -> RLMSection<RLMKeyType, RLMObjectType>

Freeze

  • Returns a frozen (immutable) snapshot of this sectioned results collection.

    The frozen copy is an immutable sectioned results collection which contains the same data as this sectioned results collection currently contains, but will not update when writes are made to the containing Realm. Unlike live sectioned results collections, frozen sectioned results collection 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 sectioned results 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 sectioned results collection.

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

    Declaration

    Objective-C

    - (nonnull instancetype)thaw;

    Swift

    func thaw() -> Self
  • Indicates if the underlying sectioned results collection is frozen.

    Frozen sectioned results collections are immutable and can be accessed from any thread.

    Declaration

    Objective-C

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

    Swift

    var isFrozen: Bool { get }

Sectioned Results Notifications

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

    The block will be asynchronously called with the initial sectioned 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 section were added, removed or modified. If a write transaction did not modify any objects in the section, the block is not called at all. See the RLMSectionedResultsChange documentation for information on how the changes are reported and an example of updating a UITableView.

    At the time when the block is called, the RLMSectionedResults object will be fully evaluated and up-to-date.

    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 *results = [Dog allObjects]; RLMSectionedResults *sectionedResults = [results sectionedResultsUsingKeyPath:@“age” ascending:YES]; self.token = [sectionedResults addNotificationBlock:^(RLMSectionedResults *sectionedResults, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“sectionedResults.count: %zu”, sectionedResults.count); // => 1 }]; [realm transactionWithBlock:^{ Dog *dog = [[Dog alloc] init]; dog.name = @“Rex”; dog.age = 5; [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.

    Warning

    The queue must be a serial queue.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)addNotificationBlock:
        (nonnull void (^)(RLMSectionedResults<RLMKeyType, RLMObjectType> *_Nonnull,
                          RLMSectionedResultsChange *_Nonnull))block;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSectionedResults<RLMKeyType, RLMObjectType>, RLMSectionedResultsChange) -> 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 sectioned results collection changes.

    The block will be asynchronously called with the initial sectioned 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 section were added, removed or modified. If a write transaction did not modify any objects in the section, the block is not called at all. See the RLMSectionedResultsChange documentation for information on how the changes are reported and an example of updating a UITableView.

    At the time when the block is called, the RLMSectionedResults object will be fully evaluated and up-to-date.

    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 *results = [Dog allObjects]; RLMSectionedResults *sectionedResults = [results sectionedResultsUsingKeyPath:@“age” ascending:YES]; self.token = [sectionedResults addNotificationBlock:^(RLMSectionedResults *sectionedResults, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“sectionedResults.count: %zu”, sectionedResults.count); // => 1 }]; [realm transactionWithBlock:^{ Dog *dog = [[Dog alloc] init]; dog.name = @“Rex”; dog.age = 5; [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.

    Warning

    The queue must be a serial queue.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)
        addNotificationBlock:
            (nonnull void (^)(
                RLMSectionedResults<RLMKeyType, RLMObjectType> *_Nonnull,
                RLMSectionedResultsChange *_Nonnull))block
                       queue:(nonnull dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSectionedResults<RLMKeyType, RLMObjectType>, RLMSectionedResultsChange) -> Void, queue: DispatchQueue) -> 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 sectioned results collection changes.

    The block will be asynchronously called with the initial sectioned 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 section were added, removed or modified. If a write transaction did not modify any objects in the section, the block is not called at all. See the RLMSectionedResultsChange documentation for information on how the changes are reported and an example of updating a UITableView.

    At the time when the block is called, the RLMSectionedResults object will be fully evaluated and up-to-date.

    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 *results = [Dog allObjects]; RLMSectionedResults *sectionedResults = [results sectionedResultsUsingKeyPath:@“age” ascending:YES]; self.token = [sectionedResults addNotificationBlock:^(RLMSectionedResults *sectionedResults, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“sectionedResults.count: %zu”, sectionedResults.count); // => 1 }]; [realm transactionWithBlock:^{ Dog *dog = [[Dog alloc] init]; dog.name = @“Rex”; dog.age = 5; [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.

    Warning

    The queue must be a serial queue.

    Note

    When filtering with key paths a notification will be fired in the following scenarios:

    • An object in the collection has been modified at the filtered properties.
    • An object has been modified on the section key path property, and the result of that modification has changed it’s position in the section, or the object may need to move to another section.
    • An object of the same observed type has been inserted or deleted from the Realm.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)
        addNotificationBlock:
            (nonnull void (^)(
                RLMSectionedResults<RLMKeyType, RLMObjectType> *_Nonnull,
                RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nonnull NSArray<NSString *> *)keyPaths;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSectionedResults<RLMKeyType, RLMObjectType>, RLMSectionedResultsChange) -> 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.

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

    The block will be asynchronously called with the initial sectioned 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 section were added, removed or modified. If a write transaction did not modify any objects in the section, the block is not called at all. See the RLMSectionedResultsChange documentation for information on how the changes are reported and an example of updating a UITableView.

    At the time when the block is called, the RLMSectionedResults object will be fully evaluated and up-to-date.

    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 *results = [Dog allObjects]; RLMSectionedResults *sectionedResults = [results sectionedResultsUsingKeyPath:@“age” ascending:YES]; self.token = [sectionedResults addNotificationBlock:^(RLMSectionedResults *sectionedResults, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“sectionedResults.count: %zu”, sectionedResults.count); // => 1 }]; [realm transactionWithBlock:^{ Dog *dog = [[Dog alloc] init]; dog.name = @“Rex”; dog.age = 5; [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.

    Warning

    The queue must be a serial queue.

    Note

    When filtering with key paths a notification will be fired in the following scenarios:

    • An object in the collection has been modified at the filtered properties.
    • An object has been modified on the section key path property, and the result of that modification has changed it’s position in the section, or the object may need to move to another section.
    • An object of the same observed type has been inserted or deleted from the Realm.

    Declaration

    Objective-C

    - (nonnull RLMNotificationToken *)
        addNotificationBlock:
            (nonnull void (^)(
                RLMSectionedResults<RLMKeyType, RLMObjectType> *_Nonnull,
                RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nullable NSArray<NSString *> *)keyPaths
                       queue:(nullable dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSectionedResults<RLMKeyType, RLMObjectType>, RLMSectionedResultsChange) -> Void, keyPaths: [String]?, queue: DispatchQueue?) -> 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.

    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.