RLMArray
@interface RLMArray <RLMObjectType : RLMObject *> : NSObject<RLMCollection,NSFastEnumeration>
RLMArray is the container type in Realm used to define to-many relationships.
Unlike an NSArray, RLMArrays hold a single type, specified by the objectClassName property.
This is referred to in these docs as the “type” of the array.
When declaring an RLMArray property, the type must be marked as conforming to a
protocol by the same name as the objects it should contain (see the
RLM_ARRAY_TYPE macro). In addition, the property can be declared using Objective-C
generics for better compile-time type safety.
RLM_ARRAY_TYPE(ObjectType)
...
@property RLMArray<ObjectType *><ObjectType> *arrayOfObjectTypes;
RLMArrays can be queried with the same predicates as RLMObject and RLMResults.
RLMArrays cannot be created directly. RLMArray properties on RLMObjects are
lazily created when accessed, or can be obtained by querying a Realm.
Key-Value Observing
RLMArray supports array key-value observing on RLMArray properties on RLMObject
subclasses, and the invalidated property on RLMArray instances themselves is
key-value observing compliant when the RLMArray is attached to a managed
RLMObject (RLMArrays on unmanaged RLMObjects will never become invalidated).
Because RLMArrays are attached to the object which they are a property of, they
do not require using the mutable collection proxy objects from
-mutableArrayValueForKey: or KVC-compatible mutation methods on the containing
object. Instead, you can call the mutation methods on the RLMArray directly.
-
The number of objects in the array.
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 the array.Declaration
Objective-C
@property (readonly, copy, nonatomic) NSString *_Nonnull objectClassName;Swift
var objectClassName: String { get } -
Indicates if the array can no longer be accessed.
Declaration
Objective-C
@property (readonly, getter=isInvalidated, nonatomic) BOOL invalidated;Swift
var isInvalidated: Bool { get }
-
Returns the object at the index specified.
Declaration
Objective-C
- (nonnull RLMObjectType)objectAtIndex:(NSUInteger)index;Swift
func object(at index: UInt) -> RLMObjectTypeParameters
indexThe index to look up.
Return Value
An
RLMObjectof the type contained in the array. -
Returns the first object in the array.
Returns
nilif called on an empty array.Declaration
Objective-C
- (nullable RLMObjectType)firstObject;Swift
func firstObject() -> RLMObjectType?Return Value
An
RLMObjectof the type contained in the array. -
Returns the last object in the array.
Returns
nilif called on an empty array.Declaration
Objective-C
- (nullable RLMObjectType)lastObject;Swift
func lastObject() -> RLMObjectType?Return Value
An
RLMObjectof the type contained in the array.
-
Adds an object to the end of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)addObject:(nonnull RLMObjectType)object;Swift
func add(_ object: RLMObjectType)Parameters
objectAn
RLMObjectof the type contained in the array. -
Adds an array of objects to the end of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)addObjects:(nonnull id<NSFastEnumeration>)objects;Swift
func addObjects(_ objects: NSFastEnumeration)Parameters
objectsAn enumerable object such as
NSArrayorRLMResultswhich contains objects of the same class as the array. -
Inserts an object at the given index.
Throws an exception if the index exceeds the bounds of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)insertObject:(nonnull RLMObjectType)anObject atIndex:(NSUInteger)index;Swift
func insert(_ anObject: RLMObjectType, at index: UInt)Parameters
anObjectAn
RLMObjectof the type contained in the array.indexThe index at which to insert the object.
-
Removes an object at the given index.
Throws an exception if the index exceeds the bounds of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)removeObjectAtIndex:(NSUInteger)index;Swift
func removeObject(at index: UInt)Parameters
indexThe array index identifying the object to be removed.
-
Removes the last object in the array.
Warning
This method may only be called during a write transaction.Declaration
Objective-C
- (void)removeLastObject;Swift
func removeLastObject() -
Removes all objects from the array.
Warning
This method may only be called during a write transaction.Declaration
Objective-C
- (void)removeAllObjects;Swift
func removeAllObjects() -
Replaces an object at the given index with a new object.
Throws an exception if the index exceeds the bounds of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(nonnull RLMObjectType)anObject;Swift
func replaceObject(at index: UInt, with anObject: RLMObjectType)Parameters
indexThe index of the object to be replaced.
anObjectAn object (of the same type as returned from the
objectClassNameselector). -
Moves the object at the given source index to the given destination index.
Throws an exception if the index exceeds the bounds of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)moveObjectAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)destinationIndex;Swift
func moveObject(at sourceIndex: UInt, to destinationIndex: UInt)Parameters
sourceIndexThe index of the object to be moved.
destinationIndexThe index to which the object at
sourceIndexshould be moved. -
Exchanges the objects in the array at given indices.
Throws an exception if either index exceeds the bounds of the array.
Warning
This method may only be called during a write transaction.
Declaration
Objective-C
- (void)exchangeObjectAtIndex:(NSUInteger)index1 withObjectAtIndex:(NSUInteger)index2;Swift
func exchangeObject(at index1: UInt, withObjectAt index2: UInt)Parameters
index1The index of the object which should replace the object at index
index2.index2The index of the object which should replace the object at index
index1.
-
Returns the index of an object in the array.
Returns
NSNotFoundif the object is not found in the array.Declaration
Objective-C
- (NSUInteger)indexOfObject:(nonnull RLMObjectType)object;Swift
func index(of object: RLMObjectType) -> UIntParameters
objectAn object (of the same type as returned from the
objectClassNameselector). -
Returns the index of the first object in the array matching the predicate.
Declaration
Objective-C
- (NSUInteger)indexOfObjectWhere:(nonnull NSString *)predicateFormat, ...;Parameters
predicateFormatA predicate format string, optionally followed by a variable number of arguments.
Return Value
The index of the object, or
NSNotFoundif the object is not found in the array. -
Returns the index of the first object in the array matching the predicate.
Declaration
Objective-C
- (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;Swift
func indexOfObject(with predicate: NSPredicate) -> UIntParameters
predicateThe predicate with which to filter the objects.
Return Value
The index of the object, or
NSNotFoundif the object is not found in the array. -
Returns all the objects matching the given predicate in the array.
Declaration
Objective-C
- (nonnull RLMResults<RLMObjectType> *)objectsWhere: (nonnull NSString *)predicateFormat, ...;Parameters
predicateFormatA predicate format string, optionally followed by a variable number of arguments.
Return Value
An
RLMResultsof objects that match the given predicate. -
Returns all the objects matching the given predicate in the array.
Declaration
Objective-C
- (nonnull RLMResults<RLMObjectType> *)objectsWithPredicate: (nonnull NSPredicate *)predicate;Swift
func objects(with predicate: NSPredicate) -> RLMResultsParameters
predicateThe predicate with which to filter the objects.
Return Value
An
RLMResultsof objects that match the given predicate -
Returns a sorted
RLMResultsfrom the array.Declaration
Objective-C
- (nonnull RLMResults<RLMObjectType> *) sortedResultsUsingKeyPath:(nonnull NSString *)keyPath ascending:(BOOL)ascending;Swift
func sortedResults(usingKeyPath keyPath: String, ascending: Bool) -> RLMResultsParameters
keyPathThe key path to sort by.
ascendingThe direction to sort in.
Return Value
An
RLMResultssorted by the specified key path. -
Returns a sorted
RLMResultsfrom the array.Declaration
Objective-C
- (nonnull RLMResults<RLMObjectType> *) sortedResultsUsingProperty:(nonnull NSString *)property ascending:(BOOL)ascending;Swift
func sortedResults(usingProperty property: String, ascending: Bool) -> RLMResultsParameters
propertyThe property name to sort by.
ascendingThe direction to sort in.
Return Value
An
RLMResultssorted by the specified property. -
Returns a sorted
RLMResultsfrom the array.Declaration
Objective-C
- (nonnull RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors: (nonnull NSArray<RLMSortDescriptor *> *)properties;Swift
func sortedResults(using properties: [RLMSortDescriptor]) -> RLMResultsParameters
propertiesAn array of
RLMSortDescriptors to sort by.Return Value
An
RLMResultssorted by the specified properties.
-
Registers a block to be called each time the array changes.
The block will be asynchronously called with the initial array, and then called again after each write transaction which changes any of the objects in the array, which objects are in the results, or the order of the objects in the array.
The
changesparameter will benilthe first time the block is called. For each call after that, it will contain information about which rows in the array were added, removed or modified. If a write transaction did not modify any objects in the array, the block is not called at all. See theRLMCollectionChangedocumentation for information on how the changes are reported and an example of updating aUITableView.If an error occurs the block will be called with
nilfor the results parameter and a non-nilerror. Currently the only errors that can occur are when opening the Realm on the background worker thread.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.
Person *person = [[Person allObjectsInRealm:realm] firstObject]; NSLog(@"person.dogs.count: %zu", person.dogs.count); // => 0 self.token = [person.dogs addNotificationBlock(RLMArray<Dog *> *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"; [person.dogs 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.Warning
This method may only be called on a managed array.
Declaration
Objective-C
- (nonnull RLMNotificationToken *)addNotificationBlock: (nonnull void (^)(RLMArray<RLMObjectType> *_Nullable, RLMCollectionChange *_Nullable, NSError *_Nullable))block;Swift
func addNotificationBlock(_ block: @escaping (RLMArrayParameters
blockThe block to be called each time the array changes.
Return Value
A token which must be held for as long as you want updates to be delivered.
-
Returns the minimum (lowest) value of the given property among all the objects in the array.
NSNumber *min = [object.arrayProperty minOfProperty:@"age"];Warning
You cannot use this method on
RLMObject,RLMArray, andNSDataproperties.Declaration
Objective-C
- (nullable id)minOfProperty:(nonnull NSString *)property;Swift
func min(ofProperty property: String) -> Any?Parameters
propertyThe property whose minimum value is desired. Only properties of types
int,float,double, andNSDateare supported.Return Value
The minimum value of the property, or
nilif the array is empty. -
Returns the maximum (highest) value of the given property among all the objects in the array.
NSNumber *max = [object.arrayProperty maxOfProperty:@"age"];Warning
You cannot use this method on
RLMObject,RLMArray, andNSDataproperties.Declaration
Objective-C
- (nullable id)maxOfProperty:(nonnull NSString *)property;Swift
func max(ofProperty property: String) -> Any?Parameters
propertyThe property whose maximum value is desired. Only properties of types
int,float,double, andNSDateare supported.Return Value
The maximum value of the property, or
nilif the array is empty. -
Returns the sum of the values of a given property over all the objects in the array.
NSNumber *sum = [object.arrayProperty sumOfProperty:@"age"];Warning
You cannot use this method on
RLMObject,RLMArray, andNSDataproperties.Declaration
Objective-C
- (nonnull NSNumber *)sumOfProperty:(nonnull NSString *)property;Swift
func sum(ofProperty property: String) -> NSNumberParameters
propertyThe property whose values should be summed. Only properties of types
int,float, anddoubleare supported.Return Value
The sum of the given property.
-
Returns the average value of a given property over the objects in the array.
NSNumber *average = [object.arrayProperty averageOfProperty:@"age"];Warning
You cannot use this method on
RLMObject,RLMArray, andNSDataproperties.Declaration
Objective-C
- (nullable NSNumber *)averageOfProperty:(nonnull NSString *)property;Swift
func average(ofProperty property: String) -> NSNumber?Parameters
propertyThe property whose average value should be calculated. Only properties of types
int,float, anddoubleare supported.Return Value
The average value of the given property, or
nilif the array is empty.
-
-[RLMArray init]is not available becauseRLMArrays cannot be created directly.RLMArrayproperties onRLMObjects are lazily created when accessed.Declaration
Objective-C
- (nonnull instancetype)init; -
+[RLMArray new]is not available becauseRLMArrays cannot be created directly.RLMArrayproperties onRLMObjects are lazily created when accessed.Declaration
Objective-C
+ (nonnull instancetype) new;
View on GitHub
Install in Dash
RLMArray Class Reference