Skip to content

Commit 890b5f5

Browse files
committed
Moving CoreData stack from thread confinement to main/private queue
DEV-451 - Using NSConfinementConcurrencyType with NSManagedObjectContext has been deprecated since iOS 9 - First step towards completely replacing the RestKit dependency in our apps - Replaces the CoreData stack with contexts using Main and Private queue concurrency types - Also using NSPersistentContainer to setup the CoreData stack - Removes the "contextForCurrentThread" helper as not suitable for Main/Private queue contexts - NSManagedObject ActiveRecord helpers require context to be passed in now
1 parent 8db79a6 commit 890b5f5

17 files changed

Lines changed: 213 additions & 753 deletions

Code/CoreData/NSManagedObject+ActiveRecord.h

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
@interface NSManagedObjectContext (ActiveRecord)
1717

18-
+ (NSManagedObjectContext *)defaultContext;
19-
+ (void)setDefaultContext:(NSManagedObjectContext *)context;
20-
+ (NSManagedObjectContext *)contextForCurrentThread;
18+
+ (NSManagedObjectContext *)contextForMainThread;
19+
+ (NSManagedObjectContext *)contextForBackgroundThread;
2120

2221
@end
2322

@@ -27,91 +26,57 @@
2726
*/
2827
@interface NSManagedObject (ActiveRecord)
2928

30-
/**
31-
* The NSEntityDescription for the Subclass
32-
* defaults to the subclass className, may be overridden
33-
*/
34-
+ (NSEntityDescription*)MR_entity;
35-
36-
/**
37-
* Returns an initialized NSFetchRequest for the entity, with no predicate
38-
*/
39-
+ (NSFetchRequest*)MR_fetchRequest;
40-
4129
/**
4230
* Fetches all objects from the persistent store identified by the fetchRequest
4331
*/
44-
+ (NSArray*)objectsWithFetchRequest:(NSFetchRequest*)fetchRequest;
32+
+ (NSArray*)objectsWithFetchRequest:(NSFetchRequest*)fetchRequest inContext:(NSManagedObjectContext*)context;
4533

4634
/**
4735
* Retrieves the number of objects that would be retrieved by the fetchRequest,
4836
* if executed
4937
*/
50-
+ (NSUInteger)countOfObjectsWithFetchRequest:(NSFetchRequest*)fetchRequest;
51-
52-
/**
53-
* Fetches all objects from the persistent store via a set of fetch requests and
54-
* returns all results in a single array.
55-
*/
56-
+ (NSArray*)objectsWithFetchRequests:(NSArray*)fetchRequests;
38+
+ (NSUInteger)countOfObjectsWithFetchRequest:(NSFetchRequest*)fetchRequest inContext:(NSManagedObjectContext*)context;
5739

5840
/**
5941
* Fetches the first object identified by the fetch request. A limit of one will be
6042
* applied to the fetch request before dispatching.
6143
*/
62-
+ (id)objectWithFetchRequest:(NSFetchRequest*)fetchRequest;
44+
+ (id)objectWithFetchRequest:(NSFetchRequest*)fetchRequest inContext:(NSManagedObjectContext*)context;
6345

6446
/**
6547
* Fetches all objects from the persistent store by constructing a fetch request and
6648
* applying the predicate supplied. A short-cut for doing filtered searches on the objects
6749
* of this class under management.
6850
*/
69-
+ (NSArray*)objectsWithPredicate:(NSPredicate*)predicate;
51+
+ (NSArray*)objectsWithPredicate:(NSPredicate*)predicate inContext:(NSManagedObjectContext*)context;
7052

7153
/**
7254
* Fetches the first object matching a predicate from the persistent store. A fetch request
7355
* will be constructed for you and a fetch limit of 1 will be applied.
7456
*/
75-
+ (id)objectWithPredicate:(NSPredicate*)predicate;
57+
+ (id)objectWithPredicate:(NSPredicate*)predicate inContext:(NSManagedObjectContext*)context;
7658

7759
/**
7860
* Fetches all managed objects of this class from the persistent store as an array
7961
*/
80-
+ (NSArray*)allObjects;
62+
+ (NSArray*)allObjectsInContext:(NSManagedObjectContext*)context;
8163

8264
/**
8365
* Returns a count of all managed objects of this class in the persistent store. On
8466
* error, will populate the error argument
8567
*/
86-
+ (NSUInteger)count:(NSError**)error;
87-
88-
/**
89-
* Returns a count of all managed objects of this class in the persistent store. Deprecated
90-
* use the error form above
91-
*
92-
* @deprecated
93-
*/
94-
+ (NSUInteger)count DEPRECATED_ATTRIBUTE;
68+
+ (NSUInteger)countInContext:(NSManagedObjectContext*)context error:(NSError**)error;
9569

9670
/**
9771
* Creates a new managed object and inserts it into the managedObjectContext.
9872
*/
99-
+ (id)object;
73+
+ (id)objectInContext:(NSManagedObjectContext *)context;
10074

10175
/**
10276
* Returns YES when an object has not been saved to the managed object context yet
10377
*/
10478
- (BOOL)isNew;
10579

106-
/**
107-
Finds the instance of the receiver's entity with the given value for the primary key attribute
108-
in the managed object context for the current thread.
109-
110-
@param primaryKeyValue The value for the receiving entity's primary key attribute.
111-
@return The object with the primary key attribute equal to the given value or nil.
112-
*/
113-
+ (id)findByPrimaryKey:(id)primaryKeyValue;
114-
11580
/**
11681
Finds the instance of the receiver's entity with the given value for the primary key attribute in
11782
the given managed object context.
@@ -124,90 +89,53 @@
12489

12590
////////////////////////////////////////////////////////////////////////////////////////////////////
12691

127-
+ (NSManagedObjectContext*)currentContext;
128-
12992
+ (void)handleErrors:(NSError *)error;
13093

131-
+ (NSArray *)executeFetchRequest:(NSFetchRequest *)request;
13294
+ (NSArray *)executeFetchRequest:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context;
133-
+ (NSFetchRequest *)createFetchRequest;
13495
+ (NSFetchRequest *)createFetchRequestInContext:(NSManagedObjectContext *)context;
135-
+ (NSEntityDescription *)entityDescription;
13696
+ (NSEntityDescription *)entityDescriptionInContext:(NSManagedObjectContext *)context;
137-
+ (NSArray *)propertiesNamed:(NSArray *)properties;
13897

139-
+ (id)createEntity;
14098
+ (id)createInContext:(NSManagedObjectContext *)context;
141-
- (BOOL)deleteEntity;
14299
- (BOOL)deleteInContext:(NSManagedObjectContext *)context;
143100

144-
+ (BOOL)truncateAll;
145101
+ (BOOL)truncateAllInContext:(NSManagedObjectContext *)context;
146102

147103
+ (NSArray *)ascendingSortDescriptors:(id)attributesToSortBy, ...;
148104
+ (NSArray *)descendingSortDescriptors:(id)attributesToSortyBy, ...;
149105

150-
+ (NSNumber *)numberOfEntities;
151106
+ (NSNumber *)numberOfEntitiesWithContext:(NSManagedObjectContext *)context;
152-
+ (NSNumber *)numberOfEntitiesWithPredicate:(NSPredicate *)searchTerm;
153107
+ (NSNumber *)numberOfEntitiesWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
154108

155-
+ (BOOL) hasAtLeastOneEntity;
156109
+ (BOOL) hasAtLeastOneEntityInContext:(NSManagedObjectContext *)context;
157110

158-
+ (NSFetchRequest *)requestAll;
159111
+ (NSFetchRequest *)requestAllInContext:(NSManagedObjectContext *)context;
160-
+ (NSFetchRequest *)requestAllWhere:(NSString *)property isEqualTo:(id)value;
161112
+ (NSFetchRequest *)requestAllWhere:(NSString *)property isEqualTo:(id)value inContext:(NSManagedObjectContext *)context;
162-
+ (NSFetchRequest *)requestFirstWithPredicate:(NSPredicate *)searchTerm;
163113
+ (NSFetchRequest *)requestFirstWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
164-
+ (NSFetchRequest *)requestFirstByAttribute:(NSString *)attribute withValue:(id)searchValue;
165114
+ (NSFetchRequest *)requestFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context;
166-
+ (NSFetchRequest *)requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending;
167115
+ (NSFetchRequest *)requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
168-
+ (NSFetchRequest *)requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm;
169116
+ (NSFetchRequest *)requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
170117

171-
+ (NSArray *)findAll;
172118
+ (NSArray *)findAllInContext:(NSManagedObjectContext *)context;
173-
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending;
174119
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
175-
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm;
176120
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
177-
+ (NSArray *)findAllWithPredicate:(NSPredicate *)searchTerm;
178121
+ (NSArray *)findAllWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
179122

180-
+ (NSNumber *)maxValueFor:(NSString *)property;
181-
+ (id) objectWithMinValueFor:(NSString *)property;
182-
+ (id) objectWithMinValueFor:(NSString *)property inContext:(NSManagedObjectContext *)context;
183-
184-
+ (id)findFirst;
185123
+ (id)findFirstInContext:(NSManagedObjectContext *)context;
186-
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm;
187124
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context;
188-
+ (id)findFirstWithPredicate:(NSPredicate *)searchterm sortedBy:(NSString *)property ascending:(BOOL)ascending;
189125
+ (id)findFirstWithPredicate:(NSPredicate *)searchterm sortedBy:(NSString *)property ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
190-
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(NSArray *)attributes;
191126
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(NSArray *)attributes inContext:(NSManagedObjectContext *)context;
192-
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending andRetrieveAttributes:(id)attributes, ...;
193127
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context andRetrieveAttributes:(id)attributes, ...;
194128

195-
+ (id)findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue;
196129
+ (id)findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context;
197-
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue;
198130
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context;
199-
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;
200131
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
201132

202133
#if TARGET_OS_IPHONE
203134

204-
+ (NSFetchedResultsController *)fetchAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm groupBy:(NSString *)groupingKeyPath;
205135
+ (NSFetchedResultsController *)fetchAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm groupBy:(NSString *)groupingKeyPath inContext:(NSManagedObjectContext *)context;
206136

207-
+ (NSFetchedResultsController *)fetchRequest:(NSFetchRequest *)request groupedBy:(NSString *)group;
208137
+ (NSFetchedResultsController *)fetchRequest:(NSFetchRequest *)request groupedBy:(NSString *)group inContext:(NSManagedObjectContext *)context;
209138

210-
+ (NSFetchedResultsController *)fetchRequestAllGroupedBy:(NSString *)group withPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending;
211139
+ (NSFetchedResultsController *)fetchRequestAllGroupedBy:(NSString *)group withPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
212140

213141
#endif

0 commit comments

Comments
 (0)