Blame view

Pods/Realm/include/RLMEmbeddedObject.h 15.2 KB
75d24c15   yangbin   123
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
365
366
367
368
369
  ////////////////////////////////////////////////////////////////////////////
  //
  // Copyright 2020 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/RLMObjectBase.h>
  #import <Realm/RLMThreadSafeReference.h>
  
  NS_ASSUME_NONNULL_BEGIN
  
  @class RLMObjectSchema, RLMPropertyDescriptor, RLMRealm, RLMNotificationToken, RLMPropertyChange;
  typedef void (^RLMObjectChangeBlock)(BOOL deleted,
                                       NSArray<RLMPropertyChange *> *_Nullable changes,
                                       NSError *_Nullable error);
  /**
   `RLMEmbeddedObject` is a base class used to define Realm model objects.
  
   Embedded objects work similarly to normal objects, but are owned by a single
   parent Object (which itself may be embedded). Unlike normal top-level objects,
   embedded objects cannot be directly created in or added to a Realm. Instead,
   they can only be created as part of a parent object, or by assigning an
   unmanaged object to a parent object's property. Embedded objects are
   automatically deleted when the parent object is deleted or when the parent is
   modified to no longer point at the embedded object, either by reassigning an
   RLMObject property or by removing the embedded object from the array containing
   it.
  
   Embedded objects can only ever have a single parent object which links to them,
   and attempting to link to an existing managed embedded object will throw an
   exception.
  
   The property types supported on `RLMEmbeddedObject` are the same as for
   `RLMObject`, except for that embedded objects cannot link to top-level objects,
   so `RLMObject` and `RLMArray<RLMObject>` properties are not supported
   (`RLMEmbeddedObject` and `RLMArray<RLMEmbeddedObject>` *are*).
  
   Embedded objects cannot have primary keys or indexed properties.
   */
  
  @interface RLMEmbeddedObject : RLMObjectBase <RLMThreadConfined>
  
  #pragma mark - Creating & Initializing Objects
  
  /**
   Creates an unmanaged instance of a Realm object.
  
   Unmanaged embedded objects can be added to a Realm by assigning them to an
   object property of a managed Realm object or by adding them to a managed
   RLMArray.
   */
  - (instancetype)init NS_DESIGNATED_INITIALIZER;
  
  /**
   Creates an unmanaged instance of a Realm object.
  
   Pass in an `NSArray` or `NSDictionary` instance to set the values of the object's properties.
  
   Unmanaged embedded objects can be added to a Realm by assigning them to an
   object property of a managed Realm object or by adding them to a managed
   RLMArray.
   */
  - (instancetype)initWithValue:(id)value;
  
  /**
   Returns the class name for a Realm object subclass.
  
   @warning Do not override. Realm relies on this method returning the exact class
            name.
  
   @return  The class name for the model class.
   */
  + (NSString *)className;
  
  #pragma mark - Properties
  
  /**
   The Realm which manages the object, or `nil` if the object is unmanaged.
   */
  @property (nonatomic, readonly, nullable) RLMRealm *realm;
  
  /**
   The object schema which lists the managed properties for the object.
   */
  @property (nonatomic, readonly) RLMObjectSchema *objectSchema;
  
  /**
   Indicates if the object can no longer be accessed because it is now invalid.
  
   An object can no longer be accessed if the object has been deleted from the Realm that manages it, or
   if `invalidate` is called on that Realm.
   */
  @property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
  
  /**
   Indicates if this object is frozen.
  
   @see `-[RLMEmbeddedObject freeze]`
   */
  @property (nonatomic, readonly, getter = isFrozen) BOOL frozen;
  
  #pragma mark - Customizing your Objects
  
  /**
   Override this method to specify the default values to be used for each property.
  
   @return    A dictionary mapping property names to their default values.
   */
  + (nullable NSDictionary *)defaultPropertyValues;
  
  /**
   Override this method to specify the names of properties to ignore. These properties will not be managed by the Realm
   that manages the object.
  
   @return    An array of property names to ignore.
   */
  + (nullable NSArray<NSString *> *)ignoredProperties;
  
  /**
   Override this method to specify the names of properties that are non-optional (i.e. cannot be assigned a `nil` value).
  
   By default, all properties of a type whose values can be set to `nil` are
   considered optional properties. To require that an object in a Realm always
   store a non-`nil` value for a property, add the name of the property to the
   array returned from this method.
  
   Properties of `RLMEmbeddedObject` type cannot be non-optional. Array and
   `NSNumber` properties can be non-optional, but there is no reason to do so:
   arrays do not support storing nil, and if you want a non-optional number you
   should instead use the primitive type.
  
   @return    An array of property names that are required.
   */
  + (NSArray<NSString *> *)requiredProperties;
  
  /**
   Override this method to provide information related to properties containing linking objects.
  
   Each property of type `RLMLinkingObjects` must have a key in the dictionary returned by this method consisting
   of the property name. The corresponding value must be an instance of `RLMPropertyDescriptor` that describes the class
   and property that the property is linked to.
  
       return @{ @"owners": [RLMPropertyDescriptor descriptorWithClass:Owner.class propertyName:@"dogs"] };
  
   @return     A dictionary mapping property names to `RLMPropertyDescriptor` instances.
   */
  + (NSDictionary<NSString *, RLMPropertyDescriptor *> *)linkingObjectsProperties;
  
  #pragma mark - Notifications
  
  /**
   Registers a block to be called each time the object changes.
  
   The block will be asynchronously called after each write transaction which
   deletes the object or modifies any of the managed properties of the object,
   including self-assignments that set a property to its existing value.
  
   For write transactions performed on different threads or in differen
   processes, the block will be called when the managing Realm is
   (auto)refreshed to a version including the changes, while for local write
   transactions it will be called at some point in the future after the write
   transaction is committed.
  
   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.
  
   Unlike with `RLMArray` and `RLMResults`, there is no "initial" callback made
   after you add a new notification block.
  
   Only objects which are managed by a Realm can be observed in this way. You
   must retain the returned token for as long as you want updates to be sent to
   the block. To stop receiving updates, call `-invalidate` on the token.
  
   It is safe to capture a strong reference to the observed object within the
   callback block. There is no retain cycle due to that the callback is retained
   by the returned token and not by the object itself.
  
   @warning This method cannot be called during a write transaction, when the
            containing Realm is read-only, or on an unmanaged object.
  
   @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:(RLMObjectChangeBlock)block;
  
  /**
   Registers a block to be called each time the object changes.
  
   The block will be asynchronously called after each write transaction which
   deletes the object or modifies any of the managed properties of the object,
   including self-assignments that set a property to its existing value.
  
   For write transactions performed on different threads or in different
   processes, the block will be called when the managing Realm is
   (auto)refreshed to a version including the changes, while for local write
   transactions it will be called at some point in the future after the write
   transaction is committed.
  
   Notifications are delivered on the given queue. If the queue is blocked and
   notifications can't be delivered instantly, multiple notifications may be
   coalesced into a single notification.
  
   Unlike with `RLMArray` and `RLMResults`, there is no "initial" callback made
   after you add a new notification block.
  
   Only objects which are managed by a Realm can be observed in this way. You
   must retain the returned token for as long as you want updates to be sent to
   the block. To stop receiving updates, call `-invalidate` on the token.
  
   It is safe to capture a strong reference to the observed object within the
   callback block. There is no retain cycle due to that the callback is retained
   by the returned token and not by the object itself.
  
   @warning This method cannot be called during a write transaction, when the
            containing Realm is read-only, or on an unmanaged object.
   @warning The queue must be a serial queue.
  
   @param block The block to be called whenever a change occurs.
   @param queue The serial queue to deliver notifications to.
   @return A token which must be held for as long as you want updates to be delivered.
   */
  - (RLMNotificationToken *)addNotificationBlock:(RLMObjectChangeBlock)block queue:(dispatch_queue_t)queue;
  
  /**
   Registers a block to be called each time the object changes.
  
   The block will be asynchronously called after each write transaction which
   deletes the object or modifies any of the managed properties of the object,
   including self-assignments that set a property to its existing value.
  
   For write transactions performed on different threads or in different
   processes, the block will be called when the managing Realm is
   (auto)refreshed to a version including the changes, while for local write
   transactions it will be called at some point in the future after the write
   transaction is committed.
  
   Notifications are delivered on the given queue. If the queue is blocked and
   notifications can't be delivered instantly, multiple notifications may be
   coalesced into a single notification.
  
   Unlike with `RLMArray` and `RLMResults`, there is no "initial" callback made
   after you add a new notification block.
  
   Only objects which are managed by a Realm can be observed in this way. You
   must retain the returned token for as long as you want updates to be sent to
   the block. To stop receiving updates, call `-invalidate` on the token.
  
   It is safe to capture a strong reference to the observed object within the
   callback block. There is no retain cycle due to that the callback is retained
   by the returned token and not by the object itself.
  
   @warning This method cannot be called during a write transaction, when the
            containing Realm is read-only, or on an unmanaged object.
   @warning The queue must be a serial queue.
  
   @param block The block to be called whenever a change occurs.
   @param keyPaths The block will be called for changes occuring on these keypaths. If no
   key paths are given, notifications are delivered for every property key path.
   @param queue The serial queue to deliver notifications to.
   @return A token which must be held for as long as you want updates to be delivered.
   */
  - (RLMNotificationToken *)addNotificationBlock:(RLMObjectChangeBlock)block keyPaths:(NSArray<NSString *> *)keyPaths queue:(dispatch_queue_t)queue;
  
  /**
   Registers a block to be called each time the object changes.
  
   The block will be asynchronously called after each write transaction which
   deletes the object or modifies any of the managed properties of the object,
   including self-assignments that set a property to its existing value.
  
   For write transactions performed on different threads or in different
   processes, the block will be called when the managing Realm is
   (auto)refreshed to a version including the changes, while for local write
   transactions it will be called at some point in the future after the write
   transaction is committed.
  
   Notifications are delivered on the given queue. If the queue is blocked and
   notifications can't be delivered instantly, multiple notifications may be
   coalesced into a single notification.
  
   Unlike with `RLMArray` and `RLMResults`, there is no "initial" callback made
   after you add a new notification block.
  
   Only objects which are managed by a Realm can be observed in this way. You
   must retain the returned token for as long as you want updates to be sent to
   the block. To stop receiving updates, call `-invalidate` on the token.
  
   It is safe to capture a strong reference to the observed object within the
   callback block. There is no retain cycle due to that the callback is retained
   by the returned token and not by the object itself.
  
   @warning This method cannot be called during a write transaction, when the
            containing Realm is read-only, or on an unmanaged object.
   @warning The queue must be a serial queue.
  
   @param block The block to be called whenever a change occurs.
   @param keyPaths The block will be called for changes occuring on these keypaths. If no
   key paths are given, notifications are delivered for every property key path.
   @return A token which must be held for as long as you want updates to be delivered.
   */
  - (RLMNotificationToken *)addNotificationBlock:(RLMObjectChangeBlock)block keyPaths:(NSArray<NSString *> *)keyPaths;
  
  #pragma mark - Other Instance Methods
  
  /**
   Returns YES if another Realm object instance points to the same object as the
   receiver in the Realm managing the receiver.
  
   For frozen objects and object types with a primary key, `isEqual:` is
   overridden to use the same logic as this method (along with a corresponding
   implementation for `hash`). Non-frozen objects without primary keys use
   pointer identity for `isEqual:` and `hash`.
  
   @param object  The object to compare the receiver to.
  
   @return    Whether the object represents the same object as the receiver.
   */
  - (BOOL)isEqualToObject:(RLMEmbeddedObject *)object;
  
  /**
   Returns a frozen (immutable) snapshot of this object.
  
   The frozen copy is an immutable object which contains the same data as this
   object currently contains, but will not update when writes are made to the
   containing Realm. Unlike live objects, frozen objects can be accessed from any
   thread.
  
   - warning: Holding onto a frozen object for an extended period while performing write
   transaction on the Realm may result in the Realm file growing to large sizes. See
   `Realm.Configuration.maximumNumberOfActiveVersions` for more information.
   - warning: This method can only be called on a managed object.
   */
  - (instancetype)freeze NS_RETURNS_RETAINED;
  
  /**
   Returns a live (mutable) reference of this object.
  
   This method creates a managed accessor to a live copy of the same frozen object.
   Will return self if called on an already live object.
   */
  - (instancetype)thaw;
  
  #pragma mark - Dynamic Accessors
  
  /// :nodoc:
  - (nullable id)objectForKeyedSubscript:(NSString *)key;
  
  /// :nodoc:
  - (void)setObject:(nullable id)obj forKeyedSubscript:(NSString *)key;
  
  @end
  
  NS_ASSUME_NONNULL_END