forked from realm/realm-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRLMResults.h
332 lines (243 loc) · 12.4 KB
/
RLMResults.h
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
////////////////////////////////////////////////////////////////////////////
//
// 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>
NS_ASSUME_NONNULL_BEGIN
@class RLMObject, RLMRealm, RLMNotificationToken;
/**
`RLMResults` is an auto-updating container type in Realm returned from object
queries. It represents the results of the query in the form of a collection of objects.
`RLMResults` can be queried using the same predicates as `RLMObject` and `RLMArray`,
and you can chain queries to further filter 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 lazily evaluated the first time they are accessed; they 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 directly instantiated.
*/
@interface RLMResults<RLMObjectType: RLMObject *> : NSObject<RLMCollection, NSFastEnumeration>
#pragma mark - Properties
/**
The number of objects in the results collection.
*/
@property (nonatomic, readonly, assign) NSUInteger count;
/**
The class name (i.e. type) of the `RLMObject`s contained in the results collection.
*/
@property (nonatomic, readonly, copy) NSString *objectClassName;
/**
The Realm which manages this results collection.
*/
@property (nonatomic, readonly) RLMRealm *realm;
/**
Indicates if the results collection is no longer valid.
The results collection becomes invalid if `invalidate` is called on the containing `realm`.
An invalidated results collection can be accessed, but will always be empty.
*/
@property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
#pragma mark - Accessing Objects from an RLMResults
/**
Returns the object at the index specified.
@param index The index to look up.
@return An `RLMObject` of the type contained in the results collection.
*/
- (RLMObjectType)objectAtIndex:(NSUInteger)index;
/**
Returns the first object in the results collection.
Returns `nil` if called on an empty results collection.
@return An `RLMObject` of the type contained in the results collection.
*/
- (nullable RLMObjectType)firstObject;
/**
Returns the last object in the results collection.
Returns `nil` if called on an empty results collection.
@return An `RLMObject` of the type contained in the results collection.
*/
- (nullable RLMObjectType)lastObject;
#pragma mark - Querying Results
/**
Returns the index of an object in the results collection.
Returns `NSNotFound` if the object is not found in the results collection.
@param object An object (of the same type as returned from the `objectClassName` selector).
*/
- (NSUInteger)indexOfObject:(RLMObjectType)object;
/**
Returns the index of the first object in the results collection matching the predicate.
@param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
@return The index of the object, or `NSNotFound` if the object is not found in the results collection.
*/
- (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat, ...;
/// :nodoc:
- (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat args:(va_list)args;
/**
Returns the index of the first object in the results collection matching the predicate.
@param predicate The predicate with which to filter the objects.
@return The index of the object, or `NSNotFound` if the object is not found in the results collection.
*/
- (NSUInteger)indexOfObjectWithPredicate:(NSPredicate *)predicate;
/**
Returns all the objects matching the given predicate in the results collection.
@param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
@return An `RLMResults` of objects that match the given predicate.
*/
- (RLMResults<RLMObjectType> *)objectsWhere:(NSString *)predicateFormat, ...;
/// :nodoc:
- (RLMResults<RLMObjectType> *)objectsWhere:(NSString *)predicateFormat args:(va_list)args;
/**
Returns all the objects matching the given predicate in the results collection.
@param predicate The predicate with which to filter the objects.
@return An `RLMResults` of objects that match the given predicate.
*/
- (RLMResults<RLMObjectType> *)objectsWithPredicate:(NSPredicate *)predicate;
/**
Returns a sorted `RLMResults` from an existing results collection.
@param property The property name to sort by.
@param ascending The direction to sort in.
@return An `RLMResults` sorted by the specified property.
*/
- (RLMResults<RLMObjectType> *)sortedResultsUsingProperty:(NSString *)property ascending:(BOOL)ascending;
/**
Returns a sorted `RLMResults` from an existing results collection.
@param properties An array of `RLMSortDescriptor`s to sort by.
@return An `RLMResults` sorted by the specified properties.
*/
- (RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors:(NSArray *)properties;
#pragma mark - Notifications
/**
Registers a block to be called each time the results collection changes.
The block will be asynchronously called with the initial results collection,
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.
For each call after that, it will contain information about
which rows in the results collection were added, removed or modified. If a
write transaction did not modify any objects in the results 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 `nil` for the results
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 `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.
@param block The block to be called whenever a change occurs.
@return A token which must be held for as long as you want updates to be delivered.
*/
- (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMResults<RLMObjectType> *__nullable results,
RLMCollectionChange *__nullable change,
NSError *__nullable error))block __attribute__((warn_unused_result));
#pragma mark - Aggregating Property Values
/**
Returns the minimum (lowest) value of the given property among all the objects
represented by the results collection.
NSNumber *min = [results minOfProperty:@"age"];
@warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
@param property The property whose minimum value is desired. Only properties of types `int`, `float`, `double`, and
`NSDate` are supported.
@return The minimum value of the property.
*/
- (nullable id)minOfProperty:(NSString *)property;
/**
Returns the maximum (highest) value of the given property among all the objects represented by the results collection.
NSNumber *max = [results maxOfProperty:@"age"];
@warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
@param property The property whose maximum value is desired. Only properties of types `int`, `float`, `double`, and
`NSDate` are supported.
@return The maximum value of the property.
*/
- (nullable id)maxOfProperty:(NSString *)property;
/**
Returns the sum of the values of a given property over all the objects represented by the results collection.
NSNumber *sum = [results sumOfProperty:@"age"];
@warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
@param property The property whose values should be summed. Only properties of types `int`, `float`, and `double` are
supported.
@return The sum of the given property.
*/
- (NSNumber *)sumOfProperty:(NSString *)property;
/**
Returns the average value of a given property over the objects represented by the results collection.
NSNumber *average = [results averageOfProperty:@"age"];
@warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
@param property The property whose average value should be calculated. Only properties of types `int`, `float`, and
`double` are supported.
@return The average value of the given property. This will be of type `double` for both `float` and `double`
properties.
*/
- (nullable NSNumber *)averageOfProperty:(NSString *)property;
/// :nodoc:
- (RLMObjectType)objectAtIndexedSubscript:(NSUInteger)index;
#pragma mark - Unavailable Methods
/**
`-[RLMResults init]` is not available because `RLMResults` cannot be created directly.
`RLMResults` can be obtained by querying a Realm.
*/
- (instancetype)init __attribute__((unavailable("RLMResults cannot be created directly")));
/**
`+[RLMResults new]` is not available because `RLMResults` cannot be created directly.
`RLMResults` can be obtained by querying a Realm.
*/
+ (instancetype)new __attribute__((unavailable("RLMResults cannot be created directly")));
@end
/**
`RLMLinkingObjects` is an auto-updating container type. It represents a collection of objects that link to its
parent object.
For more information, please see the "Inverse Relationships" section in the
[documentation](https://realm.io/docs/objc/latest/#relationships).
*/
@interface RLMLinkingObjects<RLMObjectType: RLMObject *> : RLMResults
@end
NS_ASSUME_NONNULL_END