RLMObject_Private.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  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
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  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.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Realm/RLMObjectBase_Dynamic.h>
  19. NS_ASSUME_NONNULL_BEGIN
  20. @class RLMProperty, RLMArray, RLMSwiftPropertyMetadata;
  21. typedef NS_ENUM(int32_t, RLMPropertyType);
  22. // RLMObject accessor and read/write realm
  23. @interface RLMObjectBase () {
  24. @public
  25. RLMRealm *_realm;
  26. __unsafe_unretained RLMObjectSchema *_objectSchema;
  27. }
  28. // unmanaged initializer
  29. - (instancetype)initWithValue:(id)value schema:(RLMSchema *)schema NS_DESIGNATED_INITIALIZER;
  30. // live accessor initializer
  31. - (instancetype)initWithRealm:(__unsafe_unretained RLMRealm *const)realm
  32. schema:(RLMObjectSchema *)schema NS_DESIGNATED_INITIALIZER;
  33. // shared schema for this class
  34. + (nullable RLMObjectSchema *)sharedSchema;
  35. // provide injection point for alternative Swift object util class
  36. + (Class)objectUtilClass:(BOOL)isSwift;
  37. @end
  38. @interface RLMObject ()
  39. // unmanaged initializer
  40. - (instancetype)initWithValue:(id)value schema:(RLMSchema *)schema NS_DESIGNATED_INITIALIZER;
  41. // live accessor initializer
  42. - (instancetype)initWithRealm:(__unsafe_unretained RLMRealm *const)realm
  43. schema:(RLMObjectSchema *)schema NS_DESIGNATED_INITIALIZER;
  44. @end
  45. @interface RLMDynamicObject : RLMObject
  46. @end
  47. // A reference to an object's row that doesn't keep the object accessor alive.
  48. // Used by some Swift property types, such as LinkingObjects, to avoid retain cycles
  49. // with their containing object.
  50. @interface RLMWeakObjectHandle : NSObject<NSCopying>
  51. - (instancetype)initWithObject:(RLMObjectBase *)object;
  52. // Consumes the row, so can only usefully be called once.
  53. @property (nonatomic, readonly) RLMObjectBase *object;
  54. @end
  55. // Calls valueForKey: and re-raises NSUndefinedKeyExceptions
  56. FOUNDATION_EXTERN id _Nullable RLMValidatedValueForProperty(id object, NSString *key, NSString *className);
  57. // Compare two RLObjectBases
  58. FOUNDATION_EXTERN BOOL RLMObjectBaseAreEqual(RLMObjectBase * _Nullable o1, RLMObjectBase * _Nullable o2);
  59. typedef void (^RLMObjectNotificationCallback)(NSArray<NSString *> *_Nullable propertyNames,
  60. NSArray *_Nullable oldValues,
  61. NSArray *_Nullable newValues,
  62. NSError *_Nullable error);
  63. FOUNDATION_EXTERN RLMNotificationToken *RLMObjectAddNotificationBlock(RLMObjectBase *obj, RLMObjectNotificationCallback block);
  64. // Returns whether the class is a descendent of RLMObjectBase
  65. FOUNDATION_EXTERN BOOL RLMIsObjectOrSubclass(Class klass);
  66. // Returns whether the class is an indirect descendant of RLMObjectBase
  67. FOUNDATION_EXTERN BOOL RLMIsObjectSubclass(Class klass);
  68. // For unit testing purposes, allow an Objective-C class named FakeObject to also be used
  69. // as the base class of managed objects. This allows for testing invalid schemas.
  70. FOUNDATION_EXTERN void RLMSetTreatFakeObjectAsRLMObject(BOOL flag);
  71. // Get ObjectUil class for objc or swift
  72. FOUNDATION_EXTERN Class RLMObjectUtilClass(BOOL isSwift);
  73. FOUNDATION_EXTERN const NSUInteger RLMDescriptionMaxDepth;
  74. @interface RLMObjectUtil : NSObject
  75. + (nullable NSArray<NSString *> *)ignoredPropertiesForClass:(Class)cls;
  76. + (nullable NSArray<NSString *> *)indexedPropertiesForClass:(Class)cls;
  77. + (nullable NSDictionary<NSString *, NSDictionary<NSString *, NSString *> *> *)linkingObjectsPropertiesForClass:(Class)cls;
  78. // Precondition: these must be returned in ascending order.
  79. + (nullable NSArray<RLMSwiftPropertyMetadata *> *)getSwiftProperties:(id)obj;
  80. + (nullable NSDictionary<NSString *, NSNumber *> *)getOptionalProperties:(id)obj;
  81. + (nullable NSArray<NSString *> *)requiredPropertiesForClass:(Class)cls;
  82. @end
  83. typedef NS_ENUM(NSUInteger, RLMSwiftPropertyKind) {
  84. RLMSwiftPropertyKindList,
  85. RLMSwiftPropertyKindLinkingObjects,
  86. RLMSwiftPropertyKindOptional,
  87. RLMSwiftPropertyKindNilLiteralOptional, // For Swift optional properties that reflect as nil
  88. RLMSwiftPropertyKindOther,
  89. };
  90. // Metadata that describes a Swift generic property.
  91. @interface RLMSwiftPropertyMetadata : NSObject
  92. @property (nonatomic, strong) NSString *propertyName;
  93. @property (nullable, nonatomic, strong) NSString *className;
  94. @property (nullable, nonatomic, strong) NSString *linkedPropertyName;
  95. @property (nonatomic) RLMPropertyType propertyType;
  96. @property (nonatomic) RLMSwiftPropertyKind kind;
  97. + (instancetype)metadataForOtherProperty:(NSString *)propertyName;
  98. + (instancetype)metadataForListProperty:(NSString *)propertyName;
  99. + (instancetype)metadataForLinkingObjectsProperty:(NSString *)propertyName
  100. className:(NSString *)className
  101. linkedPropertyName:(NSString *)linkedPropertyName;
  102. + (instancetype)metadataForOptionalProperty:(NSString *)propertyName type:(RLMPropertyType)type;
  103. + (instancetype)metadataForNilLiteralOptionalProperty:(NSString *)propertyName;
  104. @end
  105. NS_ASSUME_NONNULL_END