No Description

AWSMTLManagedObjectAdapter.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // MTLManagedObjectAdapter.h
  3. // Mantle
  4. //
  5. // Created by Justin Spahr-Summers on 2013-03-29.
  6. // Copyright (c) 2013 GitHub. All rights reserved.
  7. //
  8. #import <CoreData/CoreData.h>
  9. @class AWSMTLModel;
  10. // A MTLModel object that supports being serialized to and from Core Data as an
  11. // NSManagedObject.
  12. @protocol AWSMTLManagedObjectSerializing
  13. @required
  14. // The name of the Core Data entity that the receiver serializes to and
  15. // deserializes from.
  16. //
  17. // This method must not return nil.
  18. + (NSString *)managedObjectEntityName;
  19. // Specifies how to map property keys to different keys on the receiver's
  20. // +managedObjectEntity.
  21. //
  22. // Entity attributes will be mapped to and from the receiver's properties using
  23. // +entityAttributeTransformerForKey:. Entity relationships will be mapped to
  24. // and from MTLModel objects using +relationshipModelClassesByPropertyKey.
  25. // Fetched properties are not supported.
  26. //
  27. // Subclasses overriding this method should combine their values with those of
  28. // `super`.
  29. //
  30. // Any property keys not present in the dictionary are assumed to match the
  31. // entity key that should be used. Any keys associated with NSNull will not
  32. // participate in managed object serialization.
  33. //
  34. // Returns a dictionary mapping property keys to entity keys (as strings) or
  35. // NSNull values.
  36. + (NSDictionary *)managedObjectKeysByPropertyKey;
  37. @optional
  38. // Specifies a set of property keys used by the adapter to check for an already
  39. // existing managed object when converting the MTLModel to its related
  40. // NSManagedObject.
  41. //
  42. // The adapter will first map any keys provided by this method to the correct
  43. // keys in managedObjectKeysByPropertyKey.
  44. //
  45. // The adapter will then perform a fetch request in the provided context for a
  46. // managed object that matches the MTLModel's managedObjectEntityName and has
  47. // equal values set for the property keys on the MTLModel.
  48. //
  49. // The managed object returned by the fetch request will then be set with all
  50. // values from the MTLModel that the managed object is being converted from.
  51. //
  52. // If a property value of our MTLModel is yet another MTLModel which needs to be
  53. // converted to a managed object, the class for that MTLModel can also implement
  54. // this method to perform its own uniqing.
  55. //
  56. // For example:
  57. // 1. An MTLModel subclass has id_number = 10.
  58. // 2. An NSManagedObject accessible to the adapter's context has idnumber = 10.
  59. // 3. managedObjectKeysByPropertyKey returns @{@"id_number" : @"idnumber"}
  60. // 4. propertyKeysForManagedObjectUniquing returns
  61. // [NSSet setWithObject:@"id_number"];
  62. // 5. Then our fetch request may return this managed object (or another managed
  63. // object with idnumber = 10).
  64. //
  65. // NOTE: If multiple managed objects follow the same uniquing criteria only one
  66. // of them will be set with our MTLModel's values.
  67. + (NSSet *)propertyKeysForManagedObjectUniquing;
  68. // Specifies how to convert the given property key to a managed object
  69. // attribute. If reversible, the transformer will also be used to convert the
  70. // managed object attribute back to the property.
  71. //
  72. // If the receiver implements a `+<key>EntityAttributeTransformer` method,
  73. // MTLManagedObjectAdapter will use the result of that method instead.
  74. //
  75. // Returns a value transformer, or nil if no transformation should be performed.
  76. + (NSValueTransformer *)entityAttributeTransformerForKey:(NSString *)key;
  77. // Specifies the MTLModel subclasses that should be deserialized to the
  78. // receiver's property keys when a property key corresponds to an entity
  79. // relationship.
  80. //
  81. // In other words, the dictionary returned by this method is used to decode
  82. // managed object relationships into MTLModels (or NSArrays or NSSets thereof)
  83. // set on the receiver.
  84. //
  85. // If a property key is omitted from the returned dictionary, but present in
  86. // +managedObjectKeysByPropertyKey, and the receiver's +managedObjectEntity has
  87. // a relationship at the corresponding key, an exception will be thrown during
  88. // deserialization.
  89. //
  90. // Subclasses overriding this method should combine their values with those of
  91. // `super`.
  92. //
  93. // Returns a dictionary mapping property keys to the Class objects that should
  94. // be used.
  95. + (NSDictionary *)relationshipModelClassesByPropertyKey;
  96. // Overridden to deserialize a different class instead of the receiver, based on
  97. // information in the provided object.
  98. //
  99. // This is mostly useful for class clusters, where the abstract base class would
  100. // be passed into +[MTLManagedObjectAdapter
  101. // modelOfClass:fromManagedObject:error:], but a subclass should be instantiated
  102. // instead.
  103. //
  104. // managedObject - The object that will be deserialized.
  105. //
  106. // Returns the class that should be instantiated (which may be the receiver), or
  107. // nil to abort parsing (e.g., if the data is invalid).
  108. + (Class)classForDeserializingManagedObject:(NSManagedObject *)managedObject;
  109. // Overriden when merging the value of the given key on the receiver with the
  110. // value of the same key from the given `NSManagedObject` requires custom
  111. // handling.
  112. //
  113. // By default, this method is not implemented, and precedence will be given to
  114. // the value of the receiving model implicitly.
  115. //
  116. // When implemented, this method is called when an existing `NSManagedObject`
  117. // is found for the receiving model, before updating the `NSManagedObject`'s
  118. // properties.
  119. //
  120. // When implementing, you should use `+managedObjectKeysByPropertyKey` to map
  121. // the given `key` to the appropriate `NSManagedObject` property.
  122. - (void)mergeValueForKey:(NSString *)key fromManagedObject:(NSManagedObject *)managedObject;
  123. // Overriden when merging values on the receiver with the given
  124. // `NSManagedObject` requires custom handling.
  125. //
  126. // By default, this method is not implemented, and precedence will be given to
  127. // the values of the receiving model implicitly.
  128. //
  129. // When implemented, this method is called when an existing `NSManagedObject`
  130. // is found for the receiving model, before updating the `NSManagedObject`'s
  131. // properties.
  132. //
  133. // When implementing, you should use `+managedObjectKeysByPropertyKey` to map
  134. // the given `key` to the appropriate `NSManagedObject` property.
  135. //
  136. // If you have also implemented `mergeValueForKey:fromManagedObject:` you have
  137. // to make sure to call `mergeValueForKey:fromManagedObject:` from this method
  138. // when appropriate.
  139. - (void)mergeValuesForKeysFromManagedObject:(NSManagedObject *)managedObject;
  140. @end
  141. // The domain for errors originating from MTLManagedObjectAdapter.
  142. extern NSString * const AWSMTLManagedObjectAdapterErrorDomain;
  143. // +classForDeserializingManagedObject: returned nil for the given object.
  144. extern const NSInteger AWSMTLManagedObjectAdapterErrorNoClassFound;
  145. // An NSManagedObject failed to initialize.
  146. extern const NSInteger AWSMTLManagedObjectAdapterErrorInitializationFailed;
  147. // The managed object key specified by +managedObjectKeysByPropertyKey does not
  148. // exist in the NSEntityDescription.
  149. extern const NSInteger AWSMTLManagedObjectAdapterErrorInvalidManagedObjectKey;
  150. // The managed object property specified has a type that isn't supported by
  151. // MTLManagedObjectAdapter.
  152. extern const NSInteger AWSMTLManagedObjectAdapterErrorUnsupportedManagedObjectPropertyType;
  153. // The fetch request to find an existing managed object based on
  154. // `+propertyKeysForManagedObjectUniquing` failed.
  155. extern const NSInteger AWSMTLManagedObjectAdapterErrorUniqueFetchRequestFailed;
  156. // A MTLModel property cannot be serialized to or deserialized from an
  157. // NSManagedObject relationship.
  158. //
  159. // For a to-one relationship, this means that the property does not contain
  160. // a MTLModel, or the MTLModel does not conform to <MTLManagedObjectSerializing>.
  161. //
  162. // For a to-many relationship, this means that the property does not contain an
  163. // NSArray or NSSet of MTLModel<MTLManagedObjectSerializing> instances.
  164. extern const NSInteger AWSMTLManagedObjectAdapterErrorUnsupportedRelationshipClass;
  165. // The model's implementation of +managedObjectKeysByPropertyKey included a key
  166. // which does not actually exist in +propertyKeys.
  167. extern const NSInteger AWSMTLManagedObjectAdapterErrorInvalidManagedObjectMapping;
  168. // Converts a MTLModel object to and from an NSManagedObject.
  169. @interface AWSMTLManagedObjectAdapter : NSObject
  170. // Attempts to deserialize an NSManagedObject into a MTLModel object.
  171. //
  172. // modelClass - The MTLModel subclass to return. This class must conform to
  173. // <MTLManagedObjectSerializing>. This argument must not be nil.
  174. // managedObject - The managed object to deserialize. If this argument is nil,
  175. // the method returns nil.
  176. // error - If not NULL, this may be set to an error that occurs during
  177. // deserialization or initializing an instance of `modelClass`.
  178. //
  179. // Returns an instance of `modelClass` upon success, or nil if an error
  180. // occurred.
  181. + (id)modelOfClass:(Class)modelClass fromManagedObject:(NSManagedObject *)managedObject error:(NSError **)error;
  182. // Serializes a MTLModel into an NSManagedObject.
  183. //
  184. // model - The model object to serialize. This argument must not be nil.
  185. // context - The context into which to insert the created managed object. This
  186. // argument must not be nil.
  187. // error - If not NULL, this may be set to an error that occurs during
  188. // serialization or insertion.
  189. + (id)managedObjectFromModel:(AWSMTLModel<AWSMTLManagedObjectSerializing> *)model insertingIntoContext:(NSManagedObjectContext *)context error:(NSError **)error;
  190. @end