1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2014 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #import <Realm/RLMObjectBase_Dynamic.h>
21 NS_ASSUME_NONNULL_BEGIN
23 @class RLMProperty, RLMArray, RLMSwiftPropertyMetadata;
24 typedef NS_ENUM(int32_t, RLMPropertyType);
26 // RLMObject accessor and read/write realm
27 @interface RLMObjectBase () {
30 __unsafe_unretained RLMObjectSchema *_objectSchema;
33 // unmanaged initializer
34 - (instancetype)initWithValue:(id)value schema:(RLMSchema *)schema NS_DESIGNATED_INITIALIZER;
36 // live accessor initializer
37 - (instancetype)initWithRealm:(__unsafe_unretained RLMRealm *const)realm
38 schema:(RLMObjectSchema *)schema NS_DESIGNATED_INITIALIZER;
40 // shared schema for this class
41 + (nullable RLMObjectSchema *)sharedSchema;
43 // provide injection point for alternative Swift object util class
44 + (Class)objectUtilClass:(BOOL)isSwift;
48 @interface RLMObject ()
50 // unmanaged initializer
51 - (instancetype)initWithValue:(id)value schema:(RLMSchema *)schema NS_DESIGNATED_INITIALIZER;
53 // live accessor initializer
54 - (instancetype)initWithRealm:(__unsafe_unretained RLMRealm *const)realm
55 schema:(RLMObjectSchema *)schema NS_DESIGNATED_INITIALIZER;
59 @interface RLMDynamicObject : RLMObject
63 // A reference to an object's row that doesn't keep the object accessor alive.
64 // Used by some Swift property types, such as LinkingObjects, to avoid retain cycles
65 // with their containing object.
66 @interface RLMWeakObjectHandle : NSObject<NSCopying>
68 - (instancetype)initWithObject:(RLMObjectBase *)object;
70 // Consumes the row, so can only usefully be called once.
71 @property (nonatomic, readonly) RLMObjectBase *object;
75 // Calls valueForKey: and re-raises NSUndefinedKeyExceptions
76 FOUNDATION_EXTERN id _Nullable RLMValidatedValueForProperty(id object, NSString *key, NSString *className);
78 // Compare two RLObjectBases
79 FOUNDATION_EXTERN BOOL RLMObjectBaseAreEqual(RLMObjectBase * _Nullable o1, RLMObjectBase * _Nullable o2);
81 typedef void (^RLMObjectNotificationCallback)(NSArray<NSString *> *_Nullable propertyNames,
82 NSArray *_Nullable oldValues,
83 NSArray *_Nullable newValues,
84 NSError *_Nullable error);
85 FOUNDATION_EXTERN RLMNotificationToken *RLMObjectAddNotificationBlock(RLMObjectBase *obj, RLMObjectNotificationCallback block);
87 // Returns whether the class is a descendent of RLMObjectBase
88 FOUNDATION_EXTERN BOOL RLMIsObjectOrSubclass(Class klass);
90 // Returns whether the class is an indirect descendant of RLMObjectBase
91 FOUNDATION_EXTERN BOOL RLMIsObjectSubclass(Class klass);
93 // For unit testing purposes, allow an Objective-C class named FakeObject to also be used
94 // as the base class of managed objects. This allows for testing invalid schemas.
95 FOUNDATION_EXTERN void RLMSetTreatFakeObjectAsRLMObject(BOOL flag);
97 // Get ObjectUil class for objc or swift
98 FOUNDATION_EXTERN Class RLMObjectUtilClass(BOOL isSwift);
100 FOUNDATION_EXTERN const NSUInteger RLMDescriptionMaxDepth;
102 @interface RLMObjectUtil : NSObject
104 + (nullable NSArray<NSString *> *)ignoredPropertiesForClass:(Class)cls;
105 + (nullable NSArray<NSString *> *)indexedPropertiesForClass:(Class)cls;
106 + (nullable NSDictionary<NSString *, NSDictionary<NSString *, NSString *> *> *)linkingObjectsPropertiesForClass:(Class)cls;
108 // Precondition: these must be returned in ascending order.
109 + (nullable NSArray<RLMSwiftPropertyMetadata *> *)getSwiftProperties:(id)obj;
111 + (nullable NSDictionary<NSString *, NSNumber *> *)getOptionalProperties:(id)obj;
112 + (nullable NSArray<NSString *> *)requiredPropertiesForClass:(Class)cls;
116 typedef NS_ENUM(NSUInteger, RLMSwiftPropertyKind) {
117 RLMSwiftPropertyKindList,
118 RLMSwiftPropertyKindLinkingObjects,
119 RLMSwiftPropertyKindOptional,
120 RLMSwiftPropertyKindNilLiteralOptional, // For Swift optional properties that reflect as nil
121 RLMSwiftPropertyKindOther,
124 // Metadata that describes a Swift generic property.
125 @interface RLMSwiftPropertyMetadata : NSObject
127 @property (nonatomic, strong) NSString *propertyName;
128 @property (nullable, nonatomic, strong) NSString *className;
129 @property (nullable, nonatomic, strong) NSString *linkedPropertyName;
130 @property (nonatomic) RLMPropertyType propertyType;
131 @property (nonatomic) RLMSwiftPropertyKind kind;
133 + (instancetype)metadataForOtherProperty:(NSString *)propertyName;
135 + (instancetype)metadataForListProperty:(NSString *)propertyName;
137 + (instancetype)metadataForLinkingObjectsProperty:(NSString *)propertyName
138 className:(NSString *)className
139 linkedPropertyName:(NSString *)linkedPropertyName;
141 + (instancetype)metadataForOptionalProperty:(NSString *)propertyName type:(RLMPropertyType)type;
143 + (instancetype)metadataForNilLiteralOptionalProperty:(NSString *)propertyName;
147 NS_ASSUME_NONNULL_END