RLMCollection
@protocol RLMCollection <NSFastEnumeration, RLMThreadConfined>
A homogenous collection of Realm-managed objects. Examples of conforming types
include RLMArray
, RLMResults
, and RLMLinkingObjects
.
-
The number of objects in the collection.
Declaration
Objective-C
@required @property (readonly, assign, nonatomic) NSUInteger count;
-
The type of the objects in the collection.
Declaration
Objective-C
@required @property (readonly, assign, nonatomic) RLMPropertyType type;
-
Indicates whether the objects in the collection can be
nil
.Declaration
Objective-C
@required @property (readonly, getter=isOptional, nonatomic) BOOL optional;
-
The class name of the objects contained in the collection.
Will be
nil
iftype
is not RLMPropertyTypeObject.Declaration
Objective-C
@required @property (readonly, copy, nonatomic, nullable) NSString *objectClassName;
-
The Realm which manages the collection, or
nil
for unmanaged collections.Declaration
Objective-C
@required @property (readonly, nonatomic) RLMRealm *_Nonnull realm;
-
Returns the object at the index specified.
Declaration
Objective-C
- (nonnull id)objectAtIndex:(NSUInteger)index;
Parameters
index
The index to look up.
Return Value
An object of the type contained in the collection.
-
Returns the first object in the collection.
Returns
nil
if called on an empty collection.Declaration
Objective-C
- (nullable id)firstObject;
Return Value
An object of the type contained in the collection.
-
Returns the last object in the collection.
Returns
nil
if called on an empty collection.Declaration
Objective-C
- (nullable id)lastObject;
Return Value
An object of the type contained in the collection.
-
Returns the index of an object in the collection.
Returns
NSNotFound
if the object is not found in the collection.Declaration
Objective-C
- (NSUInteger)indexOfObject:(nonnull id)object;
Parameters
object
An object (of the same type as returned from the
objectClassName
selector). -
Returns the index of the first object in the 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 collection. -
Returns the index of the first object in the collection matching the predicate.
Declaration
Objective-C
- (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;
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 collection. -
Returns all objects matching the given predicate in the collection.
Declaration
Objective-C
- (nonnull RLMResults *)objectsWhere:(nonnull NSString *)predicateFormat, ...;
Parameters
predicateFormat
A predicate format string, optionally followed by a variable number of arguments.
Return Value
An
RLMResults
containing objects that match the given predicate. -
Returns all objects matching the given predicate in the collection.
Declaration
Objective-C
- (nonnull RLMResults *)objectsWithPredicate:(nonnull NSPredicate *)predicate;
Parameters
predicate
The predicate with which to filter the objects.
Return Value
An
RLMResults
containing objects that match the given predicate. -
Returns a sorted
RLMResults
from the collection.Declaration
Objective-C
- (nonnull RLMResults *)sortedResultsUsingKeyPath:(nonnull NSString *)keyPath ascending:(BOOL)ascending;
Parameters
keyPath
The keyPath to sort by.
ascending
The direction to sort in.
Return Value
An
RLMResults
sorted by the specified key path. -
Returns a sorted
RLMResults
from the collection.Declaration
Objective-C
- (nonnull RLMResults *)sortedResultsUsingDescriptors: (nonnull NSArray<RLMSortDescriptor *> *)properties;
Parameters
properties
An array of
RLMSortDescriptor
s to sort by.Return Value
An
RLMResults
sorted by the specified properties. -
Returns an
NSArray
containing the results of invokingvalueForKey:
usingkey
on each of the collection’s objects.Declaration
Objective-C
- (nullable id)valueForKey:(nonnull NSString *)key;
Parameters
key
The name of the property.
Return Value
An
NSArray
containing results. -
Invokes
setValue:forKey:
on each of the collection’s objects using the specifiedvalue
andkey
.Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)setValue:(nullable id)value forKey:(nonnull NSString *)key;
Parameters
value
The object value.
key
The name of the property.
-
Registers a block to be called each time the collection 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 benil
the first time the block is called. 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 theRLMCollectionChange
documentation for information on how the changes are reported and an example of updating aUITableView
.If an error occurs the block will be called with
nil
for the collection parameter and a non-nil
error. Currently the only errors that can occur are when opening the Realm on the background worker thread.At the time when the block is called, the collection 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 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 (^)(id<RLMCollection> _Nullable, RLMCollectionChange *_Nullable, NSError *_Nullable))block;
Parameters
block
The block to be called each time the collection changes.
Return Value
A token which must be held for as long as you want collection notifications to be delivered.
-
Returns the minimum (lowest) value of the given property among all the objects in the collection.
NSNumber *min = [results minOfProperty:@"age"];
Declaration
Objective-C
- (nullable id)minOfProperty:(nonnull NSString *)property;
Parameters
property
The property whose minimum value is desired. Only properties of types
int
,float
,double
, andNSDate
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 in the collection.
NSNumber *max = [results maxOfProperty:@"age"];
Declaration
Objective-C
- (nullable id)maxOfProperty:(nonnull NSString *)property;
Parameters
property
The property whose maximum value is desired. Only properties of types
int
,float
,double
, andNSDate
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 in the collection.
NSNumber *sum = [results sumOfProperty:@"age"];
Declaration
Objective-C
- (nonnull NSNumber *)sumOfProperty:(nonnull NSString *)property;
Parameters
property
The property whose values should be summed. Only properties of types
int
,float
, anddouble
are supported.Return Value
The sum of the given property.
-
Returns the average value of a given property over the objects in the collection.
NSNumber *average = [results averageOfProperty:@"age"];
Declaration
Objective-C
- (nullable NSNumber *)averageOfProperty:(nonnull NSString *)property;
Parameters
property
The property whose average value should be calculated. Only properties of types
int
,float
, anddouble
are supported.Return Value
The average value of the given property, or
nil
if the Results are empty.