RLMCollection
@protocol RLMCollection <NSFastEnumeration>
A homogenous collection of RLMObjects like RLMArray or RLMResults.
-
Number of objects in the collection.
Declaration
Objective‑C
@required @property (readonly, assign, nonatomic) NSUInteger count;Swift
var count: UInt { get } -
The class name (i.e. type) of the RLMObjects contained in this RLMCollection.
Declaration
Objective‑C
@required @property (readonly, copy, nonatomic) NSString *_Nonnull objectClassName;Swift
var objectClassName: String { get } -
The Realm in which this collection is persisted. Returns nil for standalone collections.
Declaration
Objective‑C
@required @property (readonly, nonatomic) RLMRealm *_Nonnull realm;Swift
var realm: RLMRealm { get }
-
Returns the object at the index specified.
Declaration
Objective‑C
- (nonnull id)objectAtIndex:(NSUInteger)index;Swift
func objectAtIndex(index: UInt) -> AnyObjectParameters
indexThe index to look up.
Return Value
An RLMObject of the type contained in this RLMCollection.
-
Returns the first object in the collection.
Returns
nilif called on an empty RLMCollection.Declaration
Objective‑C
- (nullable id)firstObject;Swift
func firstObject() -> AnyObject?Return Value
An RLMObject of the type contained in this RLMCollection.
-
Returns the last object in the collection.
Returns
nilif called on an empty RLMCollection.Declaration
Objective‑C
- (nullable id)lastObject;Swift
func lastObject() -> AnyObject?Return Value
An RLMObject of the type contained in this RLMCollection.
-
Gets the index of an object.
Returns NSNotFound if the object is not found in this RLMCollection.
Declaration
Objective‑C
- (NSUInteger)indexOfObject:(nonnull RLMObject *)object;Swift
func indexOfObject(object: RLMObject) -> UIntParameters
objectAn object (of the same type as returned from the objectClassName selector).
-
Gets the index of the first object matching the predicate.
Declaration
Objective‑C
- (NSUInteger)indexOfObjectWhere:(nonnull NSString *)predicateFormat, ...;Parameters
predicateFormatThe predicate format string which can accept variable arguments.
Return Value
Index of object or NSNotFound if the object is not found in this RLMCollection.
-
Gets the index of the first object matching the predicate.
Declaration
Objective‑C
- (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;Swift
func indexOfObjectWithPredicate(predicate: NSPredicate) -> UIntParameters
predicateThe predicate to filter the objects.
Return Value
Index of object or NSNotFound if the object is not found in this RLMCollection.
-
Get objects matching the given predicate in the RLMCollection.
Declaration
Objective‑C
- (nonnull RLMResults *)objectsWhere:(nonnull NSString *)predicateFormat, ...;Parameters
predicateFormatThe predicate format string which can accept variable arguments.
Return Value
An RLMResults of objects that match the given predicate
-
Get objects matching the given predicate in the RLMCollection.
Declaration
Objective‑C
- (nonnull RLMResults *)objectsWithPredicate:(nonnull NSPredicate *)predicate;Swift
func objectsWithPredicate(predicate: NSPredicate) -> RLMResultsParameters
predicateThe predicate to filter the objects.
Return Value
An RLMResults of objects that match the given predicate
-
Get a sorted RLMResults from an RLMCollection.
Declaration
Objective‑C
- (nonnull RLMResults *)sortedResultsUsingProperty:(nonnull NSString *)property ascending:(BOOL)ascending;Swift
func sortedResultsUsingProperty(property: String, ascending: Bool) -> RLMResultsParameters
propertyThe property name to sort by.
ascendingThe direction to sort by.
Return Value
An RLMResults sorted by the specified property.
-
Get a sorted RLMResults from an RLMCollection.
Declaration
Objective‑C
- (nonnull RLMResults *)sortedResultsUsingDescriptors: (nonnull NSArray<RLMSortDescriptor *> *)properties;Swift
func sortedResultsUsingDescriptors(properties: [RLMSortDescriptor]) -> RLMResultsParameters
propertiesAn array of
RLMSortDescriptors to sort by.Return Value
An RLMResults sorted by the specified properties.
-
Returns an NSArray containing the results of invoking
valueForKey:using key on each of the collection’s objects.Declaration
Objective‑C
- (nullable id)valueForKey:(nonnull NSString *)key;Swift
func valueForKey(key: String) -> AnyObject?Parameters
keyThe name of the property.
Return Value
NSArray containing the results of invoking
valueForKey:using key on each of the collection’s objects. -
Invokes
setValue:forKey:on each of the collection’s objects using the specified value and key.Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)setValue:(nullable id)value forKey:(nonnull NSString *)key;Swift
func setValue(value: AnyObject?, forKey key: String)Parameters
valueThe object value.
keyThe name of the property.
-
Register a block to be called each time the RLMCollection changes.
The block will be asynchronously called with the initial collection, and then called again after each write transaction which changes either any of the objects in the collection, or which objects are in the collection.
The change parameter will be
nilthe first time the block is called with the initial collection. For each call after that, it will contain information about which rows in the collection were added, removed or modified. If a write transaction did not modify any objects in this 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
nilfor the collection parameter and a non-nilerror. Currently the only errors that can occur are when opening the RLMRealm on the background worker thread.At the time when the block is called, the RLMCollection 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 collection. 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.
id<RLMCollection> collection = [Dog allObjects]; NSLog(@"dogs.count: %zu", dogs.count); // => 0 self.token = [collection addNotificationBlock:^(id<RLMCollection> 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 contextYou 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
-stopon 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 (^)(id<RLMCollection> _Nullable, RLMCollectionChange *_Nullable, NSError *_Nullable))block;Swift
@warn_unused_result func addNotificationBlock(block: (RLMCollection?, RLMCollectionChange?, NSError?) -> Void) -> RLMNotificationTokenParameters
blockThe block to be called with the evaluated collection.
Return Value
A token which must be held for as long as you want collection notifications to be delivered.
View on GitHub
Install in Dash
RLMCollection Protocol Reference