-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchedResultsControllerFactory.m
130 lines (109 loc) · 8.21 KB
/
FetchedResultsControllerFactory.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
//
// BloodPressureReadingsFetchedResultsControllerFactory.m
// BPTracker
//
// Created by Robert Saccone on 12/18/11.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "FetchedResultsControllerFactory.h"
#import "BPFetchRequestBuilderHelper.h"
@interface FetchedResultsControllerFactory ()
- (NSFetchedResultsController *)makeBPFetchedResultsControllerWithManagedObjectContext:(NSManagedObjectContext *)managedObjContext
predicate:(NSPredicate *)predicate
sectionNameKeyPath:(NSString *)sectionNameKey
sortAscending:(BOOL)ascending
cacheName:(NSString *)name
batchSize:(NSUInteger)fetchBatchSize;
@end
@implementation FetchedResultsControllerFactory
static FetchedResultsControllerFactory *theInstance = nil;
+ (FetchedResultsControllerFactory *)instance
{
if (theInstance == nil)
theInstance = [[FetchedResultsControllerFactory alloc] init];
return theInstance;
}
- (NSFetchedResultsController *)makeFetchedResultsControllerWithManagedObjectContext:(NSManagedObjectContext *)managedObjContext
entityName:(NSString *)entityName
predicate:(NSPredicate *)predicate
sectionNameKeyPath:(NSString *)sectionNameKey
sortkeyName:(NSString *)sortkeyName
sortAscending:(BOOL)ascending
cacheName:(NSString *)name
batchSize:(NSUInteger) fetchBatchSize
{
NSFetchedResultsController *aFetchedResultsController = nil;
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [BPFetchRequestBuilderHelper makeFetchRequestForManagedObjectContext:managedObjContext
entityName:entityName
predicate:predicate
sectionNameKeyPath:sectionNameKey
sortkeyName:sortkeyName
sortAscending:ascending
cacheName:name
batchSize:fetchBatchSize];
if (fetchRequest != nil)
{
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjContext
sectionNameKeyPath:sectionNameKey cacheName:name];
}
return aFetchedResultsController;
}
- (NSFetchedResultsController *)makeBPFetchedResultsControllerWithManagedObjectContext:(NSManagedObjectContext *)managedObjContext
predicate:(NSPredicate *)predicate
sectionNameKeyPath:(NSString *)sectionNameKey
sortAscending:(BOOL)ascending
cacheName:(NSString *)name
batchSize:(NSUInteger)fetchBatchSize
{
return [self makeFetchedResultsControllerWithManagedObjectContext:managedObjContext
entityName:@"BloodPressureReading"
predicate:predicate
sectionNameKeyPath:sectionNameKey
sortkeyName:@"readingDate"
sortAscending:ascending
cacheName:name
batchSize:fetchBatchSize];
}
- (NSFetchedResultsController *)makeBPFetchedResultsControllerWithManagedObjectContext:(NSManagedObjectContext *)managedObjContext
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
sectionNameKeyPath:(NSString *)sectionNameKey
cacheName:(NSString *)name
batchSize:(NSUInteger) fetchBatchSize
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(readingDate >= %@) AND (readingDate <= %@)", startDate, endDate];
return [self makeBPFetchedResultsControllerWithManagedObjectContext:managedObjContext
predicate:predicate
sectionNameKeyPath:sectionNameKey
sortAscending:YES
cacheName:name
batchSize:fetchBatchSize];
}
- (NSFetchedResultsController *)makeBPFetchedResultsControllerWithManagedObjectContext:(NSManagedObjectContext *)managedObjContext
sectionNameKeyPath:(NSString *)sectionNameKey
sortAscending:(BOOL)ascending
cacheName:(NSString *)name
batchSize:(NSUInteger)fetchBatchSize
{
return [self makeBPFetchedResultsControllerWithManagedObjectContext:managedObjContext
predicate:nil
sectionNameKeyPath:sectionNameKey
sortAscending:ascending
cacheName:name
batchSize:fetchBatchSize];
}
- (NSFetchedResultsController *)makeTakeReadingEventsFetchedResultsControllerWithManagedObjectContext: (NSManagedObjectContext *)managedObjContext sectionNameKeyPath:(NSString *)sectionNameKey cacheName:(NSString *)name batchSize:(NSUInteger)fetchBatchSize
{
return [self makeFetchedResultsControllerWithManagedObjectContext:managedObjContext
entityName:@"TakeReadingCalendarEventMetaData"
predicate:nil sectionNameKeyPath:nil
sortkeyName:nil
sortAscending:YES
cacheName:name
batchSize:fetchBatchSize];
}
@end