-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickFetch.m
139 lines (120 loc) · 4.73 KB
/
QuickFetch.m
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
134
135
136
137
138
139
//
// QuickFetch.m
// QuickFetch
//
// Created by Daniel Child on 3/20/09.
// Copyright 2009 EideticVisions LLC. All rights reserved.
//
#import "QuickFetch.h"
@implementation NSManagedObjectContext (QuickFetch)
// returns the number of entities of a certain type
- (NSUInteger) quickCount: (NSString *) entityName
{
NSArray *allObjects = [self fetchAll: entityName];
NSUInteger numObjects = [allObjects count];
return numObjects;
}
// returns the number of entities of a certain type matching the predicate
- (NSUInteger) quickCount: (NSString *) entityName
withPredicate: (NSPredicate *) predicate
{
NSArray *allObjects = [self quickFetch: entityName withPredicate: predicate];
NSUInteger numMatches = [allObjects count];
return numMatches;
}
/*********************************************************************************
** deleteAllObjectsOfType
**
** Deletes all the objects in the managed object context of type specified
**********************************************************************************/
- (void) deleteAllObjectsOfType: (NSString *) entityName
{
NSArray *allObjects = [self quickFetch: entityName withPredicate: nil];
NSUInteger count = 0;
for (id object in allObjects) {
[self deleteObject: object];
count++;
if (count%200 == 0) {
[self save: nil];
NSLog(@"Deleted another 200 objects of type %@\n", entityName);
}
}
[self save: nil];
// NEW APPROACH
/* DOES NOT WORK YET
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName: entityName];
NSBatchDeleteRequest *delete = [[NSBatchDeleteRequest alloc] initWithFetchRequest: request];
NSError *deleteError = nil;
[myPersistentStoreCoordinator executeRequest:delete withContext:myContext error:&deleteError]; */
}
/*********************************************************************************
** fetchAll
**
** Fetches all the objects in the managed object context of type specified
**********************************************************************************/
- (NSArray *) fetchAll: (NSString *) entityName
{
NSArray *allObjects = [self quickFetch: entityName withPredicate: nil];
return allObjects;
}
/***********************************************************************************
** quickFetch: withPredicate:
**
** Returns an array without sorting. Note that the predicate and entity should be
** correct. You should be able to adapt this within subclasses of NSManagedObject.
** For example,
**
** fetchFantiWithPredicate:
** fetchKanjiWithPredicate:
** fetchJiantiWithPredicate:
**
** so that the entities are wired in. This makes the whole process much more usable.
***********************************************************************************/
- (NSArray *) quickFetch: (NSString *) entityName
withPredicate: (NSPredicate *) predicate
{
NSEntityDescription *entity = [NSEntityDescription entityForName: entityName
inManagedObjectContext: self];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: entity];
[request setPredicate: predicate];
NSError *error = nil;
NSArray *results = [self executeFetchRequest: request error: &error];
if (error != nil)
{
[NSException raise: NSGenericException format: @""];
}
return results;
}
/***********************************************************************************
** quickFetch: withPredicate: andSortDescriptor:
**
** Returns an array with sorting. Note that the predicate, entity, and sortDescriptors
** should be correct. You should be able to adapt this within subclasses of
** NSManagedObject. For example,
**
** fetchFantiMatching: orderBy:
** fetchKanjiMatching: orderBy:
** fetchJiantiMatching: orderBy:
**
** so that the entities are wired in. This makes the whole process much more usable.
***********************************************************************************/
- (NSArray *) quickFetch: (NSString *) entityName
withPredicate: (NSPredicate *) predicate
andSortDescriptor: (NSArray *) sortDescriptors
{
NSEntityDescription *entity = [NSEntityDescription entityForName: entityName
inManagedObjectContext: self];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: entity];
[request setPredicate: predicate];
[request setSortDescriptors: sortDescriptors];
NSError *error = nil;
NSArray *results = [self executeFetchRequest: request error: &error];
if (error != nil)
{
[NSException raise: NSGenericException format: @""];
}
return results;
}
@end