RLMResults
@interface RLMResults <RLMObjectType : RLMObject *> : NSObject<RLMCollection,NSFastEnumeration>
RLMResults is an auto-updating container type in Realm returned from object queries.
RLMResults can be queried with the same predicates as RLMObject and RLMArray and you can chain queries to further filter query results.
RLMResults always reflect the current state of the Realm on the current thread,
including during write transactions on the current thread. The one exception to
this is when using for...in
fast enumeration, which will always enumerate
over the objects which matched the query when the enumeration is begun, even if
some of them are deleted or modified to be excluded by the filter during the
enumeration.
RLMResults are initially lazily evaluated, and only run queries when the result of the query is requested. This means that chaining several temporary RLMResults to sort and filter your data does not perform any extra work processing the intermediate state.
Once the results have been evaluated or a notification block has been added, the results are eagerly kept up-to-date, with the work done to keep them up-to-date done on a background thread whenever possible.
RLMResults cannot be created directly.
-
Number of objects in the results.
Declaration
Objective‑C
@property (readonly, assign, nonatomic) NSUInteger count;
Swift
var count: UInt { get }
-
The class name (i.e. type) of the RLMObjects contained in this RLMResults.
Declaration
Objective‑C
@property (readonly, copy, nonatomic) NSString *_Nonnull objectClassName;
Swift
var objectClassName: String { get }
-
The Realm this
RLMResults
is associated with.Declaration
Objective‑C
@property (readonly, nonatomic) RLMRealm *_Nonnull realm;
Swift
var realm: RLMRealm { get }
-
Indicates if the results can no longer be accessed.
The results can no longer be accessed if
invalidate
is called on the containingrealm
.Declaration
Objective‑C
@property (readonly, getter=isInvalidated, nonatomic) BOOL invalidated;
Swift
var invalidated: Bool { get }
-
Returns the object at the index specified.
Declaration
Objective‑C
- (nonnull RLMObjectType)objectAtIndex:(NSUInteger)index;
Swift
func objectAtIndex(index: UInt) -> RLMObject
Parameters
index
The index to look up.
Return Value
An RLMObject of the type contained in this RLMResults.
-
Returns the first object in the results.
Returns
nil
if called on an empty RLMResults.Declaration
Objective‑C
- (nullable RLMObjectType)firstObject;
Swift
func firstObject() -> RLMObject?
Return Value
An RLMObject of the type contained in this RLMResults.
-
Returns the last object in the results.
Returns
nil
if called on an empty RLMResults.Declaration
Objective‑C
- (nullable RLMObjectType)lastObject;
Swift
func lastObject() -> RLMObject?
Return Value
An RLMObject of the type contained in this RLMResults.
-
Gets the index of an object.
Returns NSNotFound if the object is not found in this RLMResults.
Declaration
Objective‑C
- (NSUInteger)indexOfObject:(nonnull RLMObjectType)object;
Swift
func indexOfObject(object: RLMObject) -> UInt
Parameters
object
An 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
predicateFormat
The predicate format string which can accept variable arguments.
Return Value
Index of object or NSNotFound if the object is not found in this RLMResults.
-
Gets the index of the first object matching the predicate.
Declaration
Objective‑C
- (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;
Swift
func indexOfObjectWithPredicate(predicate: NSPredicate) -> UInt
Parameters
predicate
The predicate to filter the objects.
Return Value
Index of object or NSNotFound if the object is not found in this RLMResults.
-
Get objects matching the given predicate in the RLMResults.
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)objectsWhere: (nonnull NSString *)predicateFormat, ...;
Parameters
predicateFormat
The 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 RLMResults.
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)objectsWithPredicate: (nonnull NSPredicate *)predicate;
Swift
func objectsWithPredicate(predicate: NSPredicate) -> RLMResults
Parameters
predicate
The predicate to filter the objects.
Return Value
An RLMResults of objects that match the given predicate
-
Get a sorted
RLMResults
from an existingRLMResults
sorted by a property.Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *) sortedResultsUsingProperty:(nonnull NSString *)property ascending:(BOOL)ascending;
Swift
func sortedResultsUsingProperty(property: String, ascending: Bool) -> RLMResults
Parameters
property
The property name to sort by.
ascending
The direction to sort by.
Return Value
An RLMResults sorted by the specified property.
-
Get a sorted
RLMResults
from an existingRLMResults
sorted by anNSArray
of
RLMSortDescriptor`s.Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors: (nonnull NSArray *)properties;
Swift
func sortedResultsUsingDescriptors(properties: [AnyObject]) -> RLMResults
Parameters
properties
An array of
RLMSortDescriptor
s to sort by.Return Value
An RLMResults sorted by the specified properties.
-
Register a block to be called each time the RLMResults changes.
The block will be asynchronously called with the initial results, 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 with the initial results. For each call after that, it will contain information about which rows in the results were added, removed or modified. If a write transaction did not modify any objects in this results, 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
nil
for the results parameter and a non-nil
error. 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 RLMResults 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 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<Dog *> *results = [Dog allObjects]; NSLog(@"dogs.count: %zu", dogs.count); // => 0 self.token = [results addNotificationBlock:^(RLMResults *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
-stop
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 (^)(RLMResults<RLMObjectType> *_Nullable, RLMCollectionChange *_Nullable, NSError *_Nullable))block;
Swift
@warn_unused_result func addNotificationBlock(block: (RLMResults?, UnsafeMutablePointer
Parameters
block
The block to be called with the evaluated results.
Return Value
A token which must be held for as long as you want query results to be delivered.
-
Returns the minimum (lowest) value of the given property
NSNumber *min = [results minOfProperty:@"age"];
Warning
You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declaration
Objective‑C
- (nullable id)minOfProperty:(nonnull NSString *)property;
Swift
func minOfProperty(property: String) -> AnyObject?
Parameters
property
The property to look for a minimum on. Only properties of type int, float, double and NSDate are supported.
Return Value
The minimum value for the property amongst objects in an RLMResults.
-
Returns the maximum (highest) value of the given property of objects in an RLMResults
NSNumber *max = [results maxOfProperty:@"age"];
Warning
You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declaration
Objective‑C
- (nullable id)maxOfProperty:(nonnull NSString *)property;
Swift
func maxOfProperty(property: String) -> AnyObject?
Parameters
property
The property to look for a maximum on. Only properties of type int, float, double and NSDate are supported.
Return Value
The maximum value for the property amongst objects in an RLMResults
-
Returns the sum of the given property for objects in an RLMResults.
NSNumber *sum = [results sumOfProperty:@"age"];
Warning
You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declaration
Objective‑C
- (nonnull NSNumber *)sumOfProperty:(nonnull NSString *)property;
Swift
func sumOfProperty(property: String) -> NSNumber
Parameters
property
The property to calculate sum on. Only properties of type int, float and double are supported.
Return Value
The sum of the given property over all objects in an RLMResults.
-
Returns the average of a given property for objects in an RLMResults.
NSNumber *average = [results averageOfProperty:@"age"];
Warning
You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declaration
Objective‑C
- (nullable NSNumber *)averageOfProperty:(nonnull NSString *)property;
Swift
func averageOfProperty(property: String) -> NSNumber?
Parameters
property
The property to calculate average on. Only properties of type int, float and double are supported.
Return Value
The average for the given property amongst objects in an RLMResults. This will be of type double for both float and double properties.
-
-[RLMResults init] is not available because RLMResults cannot be created directly. RLMResults can be obtained by querying a Realm.
Declaration
Objective‑C
- (nonnull instancetype)init;
-
+[RLMResults new] is not available because RLMResults cannot be created directly. RLMResults can be obtained by querying a Realm.
Declaration
Objective‑C
+ (nonnull instancetype) new;