-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
RKEntityCache.h
133 lines (104 loc) · 5.45 KB
/
RKEntityCache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// RKEntityCache.h
// RestKit
//
// Created by Blake Watters on 5/2/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
#import <CoreData/CoreData.h>
@class RKEntityByAttributeCache;
/**
Instances of RKInMemoryEntityCache provide an in-memory caching mechanism for
objects in a Core Data managed object context. Managed objects can be cached by
attribute for fast retrieval without repeatedly hitting the Core Data persistent store.
This can provide a substantial speed advantage over issuing fetch requests
in cases where repeated look-ups of the same data are performed using a small set
of attributes as the query key. Internally, the cache entries are maintained as
references to the NSManagedObjectID of corresponding cached objects.
*/
@interface RKEntityCache : NSObject
///-----------------------------------------------------------------------------
/// @name Initializing the Cache
///-----------------------------------------------------------------------------
/**
Initializes the receiver with a managed object context containing the entity instances to be cached.
@param context The managed object context containing objects to be cached.
@returns self, initialized with context.
*/
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)context;
/**
The managed object context with which the receiver is associated.
*/
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
///-----------------------------------------------------------------------------
/// @name Caching Objects by Attribute
///-----------------------------------------------------------------------------
/**
Caches all instances of an entity using the value for an attribute as the cache key.
@param entity The entity to cache all instances of.
@param attributeName The attribute to cache the instances by.
*/
- (void)cacheObjectsForEntity:(NSEntityDescription *)entity byAttribute:(NSString *)attributeName;
/**
Returns a Boolean value indicating if all instances of an entity have been cached by a given attribute name.
@param entity The entity to check the cache status of.
@param attributeName The attribute to check the cache status with.
@return YES if the cache has been loaded with instances with the given attribute, else NO.
*/
- (BOOL)isEntity:(NSEntityDescription *)entity cachedByAttribute:(NSString *)attributeName;
/**
Retrieves the first cached instance of a given entity where the specified attribute matches the given value.
@param entity The entity to search the cache for instances of.
@param attributeName The attribute to search the cache for matches with.
@param attributeValue The value of the attribute to return a match for.
@return A matching managed object instance or nil.
@raise NSInvalidArgumentException Raised if instances of the entity and attribute have not been cached.
*/
- (NSManagedObject *)objectForEntity:(NSEntityDescription *)entity withAttribute:(NSString *)attributeName value:(id)attributeValue;
/**
Retrieves all cached instances of a given entity where the specified attribute matches the given value.
@param entity The entity to search the cache for instances of.
@param attributeName The attribute to search the cache for matches with.
@param attributeValue The value of the attribute to return a match for.
@return All matching managed object instances or nil.
@raise NSInvalidArgumentException Raised if instances of the entity and attribute have not been cached.
*/
- (NSArray *)objectsForEntity:(NSEntityDescription *)entity withAttribute:(NSString *)attributeName value:(id)attributeValue;
///-----------------------------------------------------------------------------
// @name Accessing Underlying Caches
///-----------------------------------------------------------------------------
/**
Retrieves the underlying entity attribute cache for a given entity and attribute.
@param entity The entity to retrieve the entity attribute cache object for.
@param attributeName The attribute to retrieve the entity attribute cache object for.
@return The entity attribute cache for the given entity and attribute, or nil if none was found.
*/
- (RKEntityByAttributeCache *)attributeCacheForEntity:(NSEntityDescription *)entity attribute:(NSString *)attributeName;
/**
Retrieves all entity attributes caches for a given entity.
@param entity The entity to retrieve the collection of entity attribute caches for.
@return An array of entity attribute cache objects for the given entity or an empty array if none were found.
*/
- (NSArray *)attributeCachesForEntity:(NSEntityDescription *)entity;
///-----------------------------------------------------------------------------
// @name Managing the Cache
///-----------------------------------------------------------------------------
/**
Flushes the entity cache by sending a flush message to each entity attribute cache
contained within the receiver.
@see [RKEntityByAttributeCache flush]
*/
- (void)flush;
/**
Adds a given object to all entity attribute caches for the object's entity contained
within the receiver.
@param object The object to add to the appropriate entity attribute caches.
*/
- (void)addObject:(NSManagedObject *)object;
/**
Removed a given object from all entity attribute caches for the object's entity contained
within the receiver.
@param object The object to remove from the appropriate entity attribute caches.
*/
- (void)removeObject:(NSManagedObject *)object;
@end