RLMArray.h
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <Realm/RLMCollection.h>
#import <Realm/RLMDefines.h>
RLM_ASSUME_NONNULL_BEGIN
@class RLMObject, RLMRealm, RLMResults RLM_GENERIC_COLLECTION, RLMNotificationToken;
/**
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). RLMArray properties can also use Objective-C generics
if available. For example:
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 persisted
RLMObject (RLMArrays on standalone 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.
*/
@interface RLMArray RLM_GENERIC_COLLECTION : NSObject<RLMCollection, NSFastEnumeration>
#pragma mark - Properties
/**
Number of objects in the array.
*/
@property (nonatomic, readonly, assign) NSUInteger count;
/**
The class name (i.e. type) of the RLMObjects contained in this RLMArray.
*/
@property (nonatomic, readonly, copy) NSString *objectClassName;
/**
The Realm in which this array is persisted. Returns nil for standalone arrays.
*/
@property (nonatomic, readonly, nullable) RLMRealm *realm;
/**
Indicates if an array can no longer be accessed.
*/
@property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
#pragma mark - Accessing Objects from an Array
/**
Returns the object at the index specified.
@param index The index to look up.
@return An RLMObject of the type contained in this RLMArray.
*/
- (RLMObjectType)objectAtIndex:(NSUInteger)index;
/**
Returns the first object in the array.
Returns `nil` if called on an empty RLMArray.
@return An RLMObject of the type contained in this RLMArray.
*/
- (nullable RLMObjectType)firstObject;
/**
Returns the last object in the array.
Returns `nil` if called on an empty RLMArray.
@return An RLMObject of the type contained in this RLMArray.
*/
- (nullable RLMObjectType)lastObject;
#pragma mark - Adding, Removing, and Replacing Objects in an Array
/**
Adds an object to the end of the array.
@warning This method can only be called during a write transaction.
@param object An RLMObject of the type contained in this RLMArray.
*/
- (void)addObject:(RLMObjectArgument)object;
/**
Adds an array of objects at the end of the array.
@warning This method can only be called during a write transaction.
@param objects An enumerable object such as NSArray or RLMResults which contains objects of the
same class as this RLMArray.
*/
- (void)addObjects:(id<NSFastEnumeration>)objects;
/**
Inserts an object at the given index.
Throws an exception when the index exceeds the bounds of this RLMArray.
@warning This method can only be called during a write transaction.
@param anObject An RLMObject of the type contained in this RLMArray.
@param index The array index at which the object is inserted.
*/
- (void)insertObject:(RLMObjectArgument)anObject atIndex:(NSUInteger)index;
/**
Removes an object at a given index.
Throws an exception when the index exceeds the bounds of this RLMArray.
@warning This method can only be called during a write transaction.
@param index The array index identifying the object to be removed.
*/
- (void)removeObjectAtIndex:(NSUInteger)index;
/**
Removes the last object in an RLMArray.
@warning This method can only be called during a write transaction.
*/
- (void)removeLastObject;
/**
Removes all objects from an RLMArray.
@warning This method can only be called during a write transaction.
*/
- (void)removeAllObjects;
/**
Replaces an object at the given index with a new object.
Throws an exception when the index exceeds the bounds of this RLMArray.
@warning This method can only be called during a write transaction.
@param index The array index of the object to be replaced.
@param anObject An object (of the same type as returned from the objectClassName selector).
*/
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(RLMObjectArgument)anObject;
/**
Moves the object at the given source index to the given destination index.
Throws an exception when the index exceeds the bounds of this RLMArray.
@warning This method can only be called during a write transaction.
@param sourceIndex The index of the object to be moved.
@param destinationIndex The index to which the object at `sourceIndex` should be moved.
*/
- (void)moveObjectAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)destinationIndex;
/**
Exchanges the objects in the array at given indexes.
Throws an exception when either index exceeds the bounds of this RLMArray.
@warning This method can only be called during a write transaction.
@param index1 The index of the object with which to replace the object at index `index2`.
@param index2 The index of the object with which to replace the object at index `index1`.
*/
- (void)exchangeObjectAtIndex:(NSUInteger)index1 withObjectAtIndex:(NSUInteger)index2;
#pragma mark - Querying an Array
/**
Gets the index of an object.
Returns NSNotFound if the object is not found in this RLMArray.
@param object An object (of the same type as returned from the objectClassName selector).
*/
- (NSUInteger)indexOfObject:(RLMObjectArgument)object;
/**
Gets the index of the first object matching the predicate.
@param predicateFormat The predicate format string which can accept variable arguments.
@return Index of object or NSNotFound if the object is not found in this RLMArray.
*/
- (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat, ...;
/// :nodoc:
- (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat args:(va_list)args;
/**
Gets the index of the first object matching the predicate.
@param predicate The predicate to filter the objects.
@return Index of object or NSNotFound if the object is not found in this RLMArray.
*/
- (NSUInteger)indexOfObjectWithPredicate:(NSPredicate *)predicate;
/**
Get objects matching the given predicate in the RLMArray.
@param predicateFormat The predicate format string which can accept variable arguments.
@return An RLMResults of objects that match the given predicate
*/
- (RLMResults RLM_GENERIC_RETURN*)objectsWhere:(NSString *)predicateFormat, ...;
/// :nodoc:
- (RLMResults RLM_GENERIC_RETURN*)objectsWhere:(NSString *)predicateFormat args:(va_list)args;
/**
Get objects matching the given predicate in the RLMArray.
@param predicate The predicate to filter the objects.
@return An RLMResults of objects that match the given predicate
*/
- (RLMResults RLM_GENERIC_RETURN*)objectsWithPredicate:(NSPredicate *)predicate;
/**
Get a sorted RLMResults from an RLMArray
@param property The property name to sort by.
@param ascending The direction to sort by.
@return An RLMResults sorted by the specified property.
*/
- (RLMResults RLM_GENERIC_RETURN*)sortedResultsUsingProperty:(NSString *)property ascending:(BOOL)ascending;
/**
Get a sorted RLMResults from an RLMArray
@param properties An array of `RLMSortDescriptor`s to sort by.
@return An RLMResults sorted by the specified properties.
*/
- (RLMResults RLM_GENERIC_RETURN*)sortedResultsUsingDescriptors:(NSArray *)properties;
/// :nodoc:
- (RLMObjectType)objectAtIndexedSubscript:(NSUInteger)index;
/// :nodoc:
- (void)setObject:(RLMObjectType)newValue atIndexedSubscript:(NSUInteger)index;
#pragma mark - Notifications
/**
Register a block to be called each time the RLMArray changes.
The block will be asynchronously called with the initial array, and then
called again after each write transaction which changes the array or any
items contained in the array. You must retain the returned token for as long as
you want the block to continue to be called. To stop receiving updates, call
`-stop` on the token.
The error parameter will always be `nil`, and is present only for compatiblity
with the RLMResults version of this method, which can potentially fail.
@param block The block to be called each time the array changes.
@return A token which must be held for as long as you want notifications to be delivered.
*/
- (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMArray RLM_GENERIC_RETURN *array, NSError *))block RLM_WARN_UNUSED_RESULT;
#pragma mark - Unavailable Methods
/**
-[RLMArray init] is not available because RLMArrays cannot be created directly.
RLMArray properties on RLMObjects are lazily created when accessed, or can be obtained by querying a Realm.
*/
- (instancetype)init __attribute__((unavailable("RLMArrays cannot be created directly")));
/**
+[RLMArray new] is not available because RLMArrays cannot be created directly.
RLMArray properties on RLMObjects are lazily created when accessed, or can be obtained by querying a Realm.
*/
+ (instancetype)new __attribute__((unavailable("RLMArrays cannot be created directly")));
@end
/**
An RLMSortDescriptor stores a property name and a sort order for use with
`sortedResultsUsingDescriptors:`. It is similar to NSSortDescriptor, but supports
only the subset of functionality which can be efficiently run by the query
engine. RLMSortDescriptor instances are immutable.
*/
@interface RLMSortDescriptor : NSObject
#pragma mark - Properties
/**
The name of the property which this sort descriptor orders results by.
*/
@property (nonatomic, readonly) NSString *property;
/**
Whether this descriptor sorts in ascending or descending order.
*/
@property (nonatomic, readonly) BOOL ascending;
#pragma mark - Methods
/**
Returns a new sort descriptor for the given property name and order.
*/
+ (instancetype)sortDescriptorWithProperty:(NSString *)propertyName ascending:(BOOL)ascending;
/**
Returns a copy of the receiver with the sort order reversed.
*/
- (instancetype)reversedSortDescriptor;
@end
/// :nodoc:
@interface RLMArray (Swift)
// for use only in Swift class definitions
- (instancetype)initWithObjectClassName:(NSString *)objectClassName;
@end
RLM_ASSUME_NONNULL_END