RLMSection

Objective-C

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

Swift

class RLMSection<RLMKeyType, RLMObjectType> : NSObject, RLMSectionedResult where RLMKeyType : RLMValue, RLMObjectType : AnyObject

An RLMSection contains the objects which belong to a specified section key.

  • key

    The value that represents the key in this section.

    Declaration

    Objective-C

    @property (nonatomic, readonly) RLMKeyType _Nonnull key;

    Swift

    var key: RLMKeyType { get }
  • The count of objects in the section.

    Declaration

    Objective-C

    @property (nonatomic, readonly) NSUInteger count;

    Swift

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

    Declaration

    Objective-C

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

    Swift

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

    Declaration

    Objective-C

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

    Swift

    func object(at index: UInt) -> RLMObjectType

Freeze

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

    The frozen copy is an immutable section which contains the same data as this section 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 section 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 section.

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

    Declaration

    Objective-C

    - (nonnull instancetype)thaw;

    Swift

    func thaw() -> Self
  • Indicates if the underlying section is frozen.

    Frozen sections are immutable and can be accessed from any thread.

    Declaration

    Objective-C

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

    Swift

    var isFrozen: Bool { get }

Section Notifications

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

    The block will be asynchronously called with the initial section, 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 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]; RLMSection *section = sectionedResults[0] // section with dogs aged ‘5’ already exists.

    self.token = [section addNotificationBlock:^(RLMSection *section, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“section.count: %zu”, section.count); // => 2 }]; [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 (^)(RLMSection<RLMKeyType, RLMObjectType> *_Nonnull,
                          RLMSectionedResultsChange *_Nonnull))block;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSection<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 section changes.

    The block will be asynchronously called with the initial section, 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 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]; RLMSection *section = sectionedResults[0] // section with dogs aged ‘5’ already exists.

    self.token = [section addNotificationBlock:^(RLMSection *section, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“section.count: %zu”, section.count); // => 2 }]; [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 (^)(RLMSection<RLMKeyType, RLMObjectType> *_Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                       queue:(nonnull dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSection<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 section changes.

    The block will be asynchronously called with the initial section, 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 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]; RLMSection *section = sectionedResults[0] // section with dogs aged ‘5’ already exists.

    self.token = [section addNotificationBlock:^(RLMSection *section, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“section.count: %zu”, section.count); // => 2 }]; [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 (^)(RLMSection<RLMKeyType, RLMObjectType> *_Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nonnull NSArray<NSString *> *)keyPaths;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSection<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 section changes.

    The block will be asynchronously called with the initial section, 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 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]; RLMSection *section = sectionedResults[0] // section with dogs aged ‘5’ already exists.

    self.token = [section addNotificationBlock:^(RLMSection *section, RLMSectionedResultsChange *changes) { // Only fired once for the example NSLog(@“section.count: %zu”, section.count); // => 2 }]; [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 (^)(RLMSection<RLMKeyType, RLMObjectType> *_Nonnull,
                              RLMSectionedResultsChange *_Nonnull))block
                    keyPaths:(nullable NSArray<NSString *> *)keyPaths
                       queue:(nullable dispatch_queue_t)queue;

    Swift

    func addNotificationBlock(_ block: @escaping (RLMSection<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.