/AZCoreRecord/NSManagedObject+AZCoreRecordImport.h

https://github.com/zwaldowski/AZCoreRecord · C Header · 211 lines · 19 code · 17 blank · 175 comment · 0 complexity · 3c3ae3203301e073c64397ac3b250a6c MD5 · raw file

  1. //
  2. // NSManagedObject+AZCoreRecordImport.h
  3. // AZCoreRecord
  4. //
  5. // Created by Saul Mora on 6/28/11.
  6. // Copyright 2010-2011 Magical Panda Software, LLC. All rights reserved.
  7. // Copyright 2012 Alexsander Akers & Zachary Waldowski. All rights reserved.
  8. //
  9. #import <CoreData/CoreData.h>
  10. /** Importing for Core Data.
  11. These utilities do their best to safely, securely, and efficiently import Core
  12. Data objects using NSDictionary and NSArray to do the bulk of the work. This
  13. allows you to simply import to your data model from JSON, XML, or HTTP requests
  14. without worrying about finding and keeping track of existing objects yourself.
  15. On the whole, the update methods are slower because they always look for
  16. existing objects for relationships. However, it is only recommended to import
  17. when you are sure the new objects (and all relationships they contain) aren't
  18. already in the model, unless you plan on writing yourself a grand old garbage
  19. collection/duplicate resolution algorithm.
  20. AZCoreRecord will use a number of user info keys on your entities, their
  21. attributes, and their relationships to import. They are as follows:
  22. *Entities*
  23. - `className` (`AZCoreRecordImportClassNameKey`): The value of this is used
  24. to determine what key in the dictionary should be used to determine the class
  25. name/entity name of the model the dictionary correlates to. This is especially
  26. useful for importing sub-entities in relationships.
  27. - `primaryAttribute` (`AZCoreRecordImportPrimaryAttributeKey`): The value for
  28. this key is used for comparing and locating model objects. If no value is
  29. provided for this key, AZCoreRecord will search for a property with the name
  30. `xID`, where `x` is the first letter of the entity name in lowercase.
  31. *Attributes*
  32. - `mappedKey` (`AZCoreRecordImportMapKey`): The value of this is used to
  33. determine what a key in the dictionary should be inserted into the model as. To
  34. set the property "lastModify" on an entity using a dictionary that has a key
  35. "lastModifiedDate", set `mappedKey` to "lastModifiedDate" on that attribute.
  36. - `className` (`AZCoreRecordImportClassNameKey`): Similar to its use at the
  37. entity level, this forces the class of the imported object into that of the
  38. value for this key. Note that it is recommended to use value transformers
  39. instead.
  40. - `dateFormat` (`AZCoreRecordImportCustomDateFormat`): For a date attribute,
  41. AZCoreRecord can automatically format a string into a date object. The value
  42. of this key is used in the date formatter. If it is not set,
  43. "yyyy-MM-dd'T'HH:mm:ss'Z'" (`AZCoreRecordImportDefaultDateFormat`) is used by
  44. default.
  45. *Relationships*
  46. - `mappedKey` (`AZCoreRecordImportMapKey`): Same as for an attribute.
  47. - `primaryKey` (`AZCoreRecordImportRelationshipPrimaryKey`): Compare to
  48. `primaryAttribute`. This key is used in relationships to define what objects to
  49. search for when associating different model objects using relationships in
  50. imported dictionaries.
  51. **/
  52. extern NSString *const AZCoreRecordImportCustomDateFormat;
  53. extern NSString *const AZCoreRecordImportDefaultDateFormat;
  54. extern NSString *const AZCoreRecordImportMapKey;
  55. extern NSString *const AZCoreRecordImportClassNameKey;
  56. extern NSString *const AZCoreRecordImportPrimaryAttributeKey;
  57. extern NSString *const AZCoreRecordImportRelationshipPrimaryKey;
  58. @interface NSManagedObject (AZCoreRecordImport)
  59. /** Imports values into a managed object by using
  60. the contents of a dictionary, creating new model
  61. objects for all relationships.
  62. @param objectData A dictionary of values.
  63. @see updateValuesFromDictionary:
  64. */
  65. - (void) importValuesFromDictionary: (NSDictionary *) objectData;
  66. /** Updates the values of a managed object using
  67. the contents of a dictionary by finding objects for
  68. relationships, creating them if not found, and
  69. associating them.
  70. @param objectData A dictionary of values
  71. @see importValuesFromDictionary:
  72. */
  73. - (void) updateValuesFromDictionary: (NSDictionary *) objectData;
  74. /** Creates a new model object for the specified entity
  75. and sets its values using the contents of a dictionary
  76. in the default context.
  77. @see importFromDictionary:inContext:
  78. @see importValuesFromDictionary:
  79. @param data A dictionary of values;
  80. @return A new managed object.
  81. */
  82. + (instancetype) importFromDictionary: (NSDictionary *) data;
  83. /** Creates a new model object for the specified entity
  84. and sets its values using the contents of a dictionary.
  85. @see importFromDictionary:
  86. @see importValuesFromDictionary:
  87. @param data A dictionary of values;
  88. @param context A managed object context.
  89. @return A new managed object.
  90. */
  91. + (instancetype) importFromDictionary: (NSDictionary *) data inContext: (NSManagedObjectContext *) context;
  92. /** Finds, and if not found creates, a model object
  93. for the specified entity and sets its values using
  94. the contents of a dictionary in the default context.
  95. Remember that existing objects are discovered
  96. using the value of the attribute named xID, where
  97. x is the first letter of the entity name, or the
  98. value of the attribute named by the user-info
  99. key primaryAttribute.
  100. @see updateFromDictionary:inContext:
  101. @see updateValuesFromDictionary:
  102. @param objectData A dictionary of values.
  103. @return A managed object.
  104. */
  105. + (instancetype) updateFromDictionary: (NSDictionary *) objectData;
  106. /** Finds, and if not found creates, a model object
  107. for the specified entity and sets its values using
  108. the contents of a dictionary in the given context.
  109. Remember that existing objects are discovered
  110. using the value of the attribute named xID, where
  111. x is the first letter of the entity name, or the
  112. value of the attribute named by the user-info
  113. key primaryAttribute.
  114. @see updateFromDictionary:inContext:
  115. @see updateValuesFromDictionary:
  116. @param objectData A dictionary of values.
  117. @param context A managed object context.
  118. @return A managed object.
  119. */
  120. + (instancetype) updateFromDictionary: (NSDictionary *) objectData inContext: (NSManagedObjectContext *) context;
  121. /** Imports values into a Core Data model by
  122. creating new instances of the specified entity
  123. and sets their values using the given dictionaries
  124. in the defai;t context.
  125. @see importFromArray:inContext:
  126. @see importValuesFromDictionary:
  127. @param listOfObjectData An array of dictionaries.
  128. @return An array of new objects.
  129. */
  130. + (NSArray *) importFromArray: (NSArray *) listOfObjectData;
  131. /** Imports values into a Core Data model by
  132. creating new instances of the specified entity
  133. and sets their values using the given dictionaries
  134. in the specified context.
  135. @see importFromArray:inContext:
  136. @see importValuesFromDictionary:
  137. @param context A managed object context.
  138. @param listOfObjectData An array of dictionaries.
  139. @return An array of updated managed objects.
  140. */
  141. + (NSArray *) importFromArray: (NSArray *) listOfObjectData inContext: (NSManagedObjectContext *) context;
  142. /** Updates a Core Data model with an array of
  143. dictionary objects by locating objects, creating
  144. them if not found, and saving asynchronously on the
  145. main managed object context.
  146. Whereas importing will always create new model objects,
  147. updating will only create new model objects that
  148. cannot be found.
  149. @param listOfObjectData An array of dictionary objects.
  150. @see updateFromArray:inContext:
  151. @see updateValuesFromDictionary:
  152. @see updateFromDictionary:
  153. @return An array of updated managed objects.
  154. */
  155. + (NSArray *) updateFromArray: (NSArray *) listOfObjectData;
  156. /** Updates a Core Data model with an array of
  157. dictionary objects by locating objects, and creating
  158. them if not found, on the given context.
  159. Remember that, while importing will always create new model objects,
  160. updating will only create new model objects that
  161. cannot be found.
  162. @param listOfObjectData An array of dictionary objects.
  163. @param localContext A managed object context that is preferably not the main one.
  164. @see updateFromArray:
  165. @see updateValuesFromDictionary:
  166. @see updateFromDictionary:inContext:
  167. @return An array of updated managed objects.
  168. */
  169. + (NSArray *) updateFromArray: (NSArray *) listOfObjectData inContext: (NSManagedObjectContext *) localContext;
  170. @end