ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARIncrementalStore+Private.m
Go to the documentation of this file.
1 // ActiveResourceKit ARIncrementalStore+Private.m
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 
26 
28 #import <ActiveSupportKit/ActiveSupportKit.h>
29 
30 @implementation ARIncrementalStore(Private)
31 
32 - (NSManagedObjectID *)objectIDForResource:(ARResource *)resource withContext:(NSManagedObjectContext *)context
33 {
34  NSString *entityName = [self entityNameForElementName:[[resource serviceLazily] elementNameLazily]];
35  NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
36  return [self newObjectIDForEntity:entity referenceObject:[resource ID]];
37 }
38 
39 - (NSManagedObjectID *)objectIDForCachedResource:(ARResource *)resource withContext:(NSManagedObjectContext *)context
40 {
41  NSManagedObjectID *objectID = [self objectIDForResource:resource withContext:context];
42  [_resourcesByObjectID setObject:resource forKey:objectID];
43  return objectID;
44 }
45 
46 - (ARResource *)cachedResourceForObjectID:(NSManagedObjectID *)objectID error:(NSError **)outError
47 {
48  ARResource *__block resource = [_resourcesByObjectID objectForKey:objectID];
49  if (resource == nil)
50  {
51  NSNumber *ID = [self referenceObjectForObjectID:objectID];
52  ARService *service = [self serviceForEntityName:[[objectID entity] name]];
53  [service findSingleWithID:ID options:nil completionHandler:^(ARHTTPResponse *response, ARResource *aResource, NSError *error) {
54  if (aResource)
55  {
56  [_resourcesByObjectID setObject:resource = aResource forKey:objectID];
57  }
58  else
59  {
60  if (outError && *outError == nil)
61  {
62  *outError = error;
63  }
64  }
65  }];
66  }
67  return resource;
68 }
69 
70 - (uint64_t)versionForResource:(ARResource *)resource
71 {
72  uint64_t version;
73  NSDate *updatedAt = ASDateFromRFC3339String(ASNilForNull([[resource attributes] objectForKey:@"updated_at"]));
74  if (updatedAt)
75  {
76  version = [updatedAt timeIntervalSinceReferenceDate] * 1000;
77  }
78  else
79  {
80  version = 0;
81  }
82  return version;
83 }
84 
85 - (void)refreshObject:(NSManagedObject *)object
86 {
87  [_resourcesByObjectID removeObjectForKey:[object objectID]];
88  [[object managedObjectContext] refreshObject:object mergeChanges:NO];
89 }
90 
91 - (NSDictionary *)foreignKeysForObject:(NSManagedObject *)object resource:(ARResource *)resource
92 {
93  NSMutableDictionary *foreignKeys = [NSMutableDictionary dictionary];
94  for (NSPropertyDescription *property in [[object entity] properties])
95  {
96  if ([property isKindOfClass:[NSRelationshipDescription class]] && [(NSRelationshipDescription *)property maxCount] == 1)
97  {
98  NSString *attributeName = [self attributeNameForPropertyName:[(NSRelationshipDescription *)property name]];
99  NSString *foreignKey = [[ASInflector defaultInflector] foreignKey:attributeName separateClassNameAndIDWithUnderscore:YES];
100  NSManagedObject *destination = [object valueForKey:[(NSRelationshipDescription *)property name]];
101  if (ASNilForNull([[resource attributes] objectForKey:foreignKey]) == nil && destination)
102  {
103  NSNumber *destinationID = [self referenceObjectForObjectID:[destination objectID]];
104  [foreignKeys setObject:destinationID forKey:foreignKey];
105  }
106  }
107  }
108  return [foreignKeys copy];
109 }
110 
111 @end