RLMSectionedResult

Objective-C

@protocol RLMSectionedResult <NSFastEnumeration, RLMThreadConfined>

Swift

protocol RLMSectionedResult : NSFastEnumeration, RLMThreadConfined

The RLMSectionedResult protocol defines properties and methods common to both RLMSectionedResults and RLMSection

Object Access

  • The count of objects in the collection.

    Declaration

    Objective-C

    @property (nonatomic, readonly) NSUInteger count;

    Swift

    var count: UInt { get }
  • Returns the object for a given index in the collection.

    Declaration

    Objective-C

    - (nonnull id)objectAtIndexedSubscript:(NSUInteger)index;

    Swift

    subscript(index: UInt) -> Any { get }
  • Returns the object for a given index in the collection.

    Declaration

    Objective-C

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

    Swift

    func object(at index: UInt) -> Any

Freeze

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

    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 arrays, frozen collections 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
  • Indicates if the underlying collection is frozen.

    Frozen 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 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 RLMSection / 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 (^)(id<RLMSectionedResult> _Nonnull,
                          RLMSectionedResultsChange *_Nonnull))block;

    Swift

    func addNotificationBlock(_ block: @escaping (any RLMSectionedResult, 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 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 RLMSection / 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 (^)(id<RLMSectionedResult> _Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                       queue:(nonnull dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (any RLMSectionedResult, RLMSectionedResultsChange) -> 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 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 RLMSection / 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 (^)(id<RLMSectionedResult> _Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nonnull NSArray<NSString *> *)keyPaths;

    Swift

    func addNotificationBlock(_ block: @escaping (any RLMSectionedResult, 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 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 RLMSection / 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 (^)(id<RLMSectionedResult> _Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nullable NSArray<NSString *> *)keyPaths
                       queue:(nullable dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (any RLMSectionedResult, RLMSectionedResultsChange) -> Void, keyPaths: [String]?, queue: dispatch_queue_t?) -> 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.