ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARIncrementalStore.h
Go to the documentation of this file.
1 // ActiveResourceKit ARIncrementalStore.h
2 //
3 // Copyright © 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import <CoreData/CoreData.h>
26 
27 @class ARService;
28 
29 /*!
30  * @brief Core Data incremental store based on active resources.
31  * @details The ARIncrementalStore class performs one simple function:
32  * translating Core Data requests to Active Resource requests, and translating
33  * Active Resource responses to Core Data responses. The class mediates between
34  * Core Data and Active Resource. You can easily access remote resources using
35  * Core Data by hooking up an ARIncrementStore-type store to your Core Data
36  * context. Specify the site URL when you add the store. The store automatically
37  * derives the resources and their properties using the managed-object model
38  * along with standard Rails-compatible conventions for naming.
39  */
40 @interface ARIncrementalStore : NSIncrementalStore<NSCacheDelegate>
41 {
42  /*!
43  * @brief Caches resources by object ID.
44  * @details This cache effectively associates Active Resource with Core
45  * Data. It bridges the difference between the Active Resource interface and
46  * the Core Data interface. When you "find" active resources, all resource
47  * attributes become available. However, when Core Data fetches managed
48  * objects incrementally, the incremental-store interface expects only
49  * object IDs at first. Subsequently, Core Data asks for attribute and
50  * relationship values, i.e. entity properties. This cache exists to buffer
51  * those values until Core Data requests them.
52  *
53  * Same goes when instantiating new object-resource pairs. The Active
54  * Resource interface creates a new resource along with its attributes. Core
55  * Data, however, only obtains permanent IDs initially. They become faulted
56  * objects. Core Data asks for properties later when needed. Again, the
57  * resources-by-object-ID cache acts as a bridging buffer.
58  */
59  NSCache *__strong _resourcesByObjectID;
60 }
61 
62 + (NSString *)storeType;
63 + (void)registerStoreClass;
64 
65 /*!
66  * @brief Derives a store's type for use when adding a store instance to your
67  * persistent store coordinator.
68  * @details You could define this explicitly but there exists an essential
69  * requirement. It is very important that the store-type string matches the
70  * class name. If not, your store will fail to successfully attach to its store
71  * coordinator. Core Data's error reason explains, "The store type in the
72  * metadata does not match the specified store type." Make them match to avoid
73  * this error. Best way to make them match? Make store type equal to class name.
74  */
75 + (NSString *)storeTypeForClass:(Class)aClass;
76 
77 /*!
78  * @brief Registers an incremental store subclass.
79  * @details You need to register the store class before using it. If your
80  * run-time loading sequence prevents you registering the store type
81  * automatically during class initialisation, that is, during @c +initialize,
82  * then in such cases you need to invoke @c +registerStoreTypeForClass:aClass
83  * explicitly @em before using the store type. Typically though, you can
84  * register the store type during class initialisation. Your sub-class interface
85  * will contain method declarations along these lines:
86  * @code
87  * + (NSString *)storeType;
88  * + (void)registerStoreClass;
89  * @endcode
90  * Along with implementations as follows:
91  * @code
92  * + (void)initialize
93  * {
94  * if (self == [MyActiveResourceIncrementalStore class])
95  * {
96  * [self registerStoreClass];
97  * }
98  * }
99  *
100  * + (NSString *)storeType
101  * {
102  * return [ARIncrementalStore storeTypeForClass:self];
103  * }
104  *
105  * + (void)registerStoreClass
106  * {
107  * [ARIncrementalStore registerStoreTypeForClass:self];
108  * }
109  * @endcode
110  */
111 + (void)registerStoreTypeForClass:(Class)aClass;
112 
113 /*!
114  * @brief Answers a suitably-connected Active Resource service for interacting
115  * with the given entity.
116  * @details Performs Core Data entity-name to Active Resource element name
117  * mapping. Uses a standard delegate-based URL connection. The connection runs
118  * asynchronously but delegates synchronously and conveniently handles
119  * authentication if needed.
120  */
121 - (ARService *)serviceForEntityName:(NSString *)entityName;
122 
123 /*!
124  * @brief Flushes the resource cache.
125  * @details Removes all resources currently retained by the incremental store's
126  * active resource-oriented cache. Note that this method flushes the resource
127  * cache only. It does not @em fault any associated non-fault managed objects
128  * which also retain client-side snapshots of attributes and
129  * relationships. Resources do not persist in the cache for any length of time;
130  * managed objects cache remote resources independently when not faulted. The
131  * cache exists as an interface bridging device for marrying Active Resource
132  * protocols with Core Data protocols.
133  */
134 - (void)evictAllResources;
135 
136 //------------------------------------------- Active Resource-to-Core Data Names
137 
138 /*!
139  * @brief Optional prefix string for entity names.
140  * @details In the Objective-C world, entity names often begin with a namespace
141  * prefix. If this is the case, specify the prefix here. The incremental store
142  * automatically adds or removes the prefix when translating between entity
143  * names and element names. For example, your element names might be `post` and
144  * `comment` when accessing some kind of RESTful blog interface. But your
145  * Objective-C entities might be `MYPost` and `MYComment`. Configure your
146  * store's entity name prefix as `MY` and it will automatically prepend `MY`
147  * when converting from element to entity names; and automatically remove it
148  * when converting from entity to element names.
149  */
150 @property(copy) NSString *entityNamePrefix;
152 // The following instance methods perform default name translations between
153 // Active Resource names and Core Data names. You might wish to override the
154 // following methods if your server-side and client-side element-entity names
155 // fall outside the simple underscored-to-camel case conversions and back.
156 //
157 // Separate method pairs exist for element-entity names and attribute-property
158 // names. The former translate between Active Resource elements and Core Data
159 // entities; the latter between Active Resource attributes and Core Data
160 // properties.
161 
162 - (NSString *)elementNameForEntityName:(NSString *)entityName;
163 - (NSString *)entityNameForElementName:(NSString *)elementName;
164 - (NSString *)attributeNameForPropertyName:(NSString *)propertyName;
165 - (NSString *)propertyNameForAttributeName:(NSString *)attributeName;
166 
167 @end
168