-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSFetchedResultsControllerCollection.m
executable file
·156 lines (110 loc) · 4.99 KB
/
NSFetchedResultsControllerCollection.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// NSFetchedResultsControllerCollection.m
// ISM Catalog
//
// Created by Robert Codd-Downey on 2013-07-24.
// Copyright (c) 2013 Robert Codd-Downey. All rights reserved.
//
#import "NSFetchedResultsControllerCollection.h"
@interface NSFetchedResultsControllerCollection ()
@property (nonatomic, strong) NSArray *controllers;
@property (nonatomic, strong) NSArray *sections;
@end
@implementation NSFetchedResultsControllerCollection
- (id)initWithFetchedResultsControllers:(NSArray*)controllers
{
self = [super init];
if (self) {
self.controllers = controllers;
self.sections = [NSArray new];
self.startingSection = 0;
}
return self;
}
- (BOOL)performFetch:(NSError **)error
{
NSError *err = nil;
_sections = [NSArray new];
for (NSFetchedResultsController *fetchResultsController in _controllers) {
if (![fetchResultsController performFetch:&err]) {
if (error != nil)
*error = err;
return NO;
}
self.sections = [_sections arrayByAddingObjectsFromArray:[fetchResultsController sections]];
fetchResultsController.delegate = self;
}
return YES;
}
- (id)objectAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = self.startingSection;
if (indexPath.section >= self.startingSection) {
for (NSFetchedResultsController *fetchResultsController in _controllers) {
NSUInteger count = [[fetchResultsController sections] count];
if (indexPath.section < section + count) {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section - section];
// id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchResultsController sections] objectAtIndex:indexPath.section];
// if (newIndexPath.row < [sectionInfo numberOfObjects]) {
return [fetchResultsController objectAtIndexPath:newIndexPath];
// } else {
// return nil;
// }
}
section += count;
}
}
return nil;
}
- (NSIndexPath *)indexPathForObject:(id)object
{
NSUInteger section = self.startingSection;
for (NSFetchedResultsController *fetchResultsController in _controllers) {
NSUInteger count = [[fetchResultsController sections] count];
NSIndexPath *indexPath = [fetchResultsController indexPathForObject:object];
if (indexPath != nil) {
return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + section];
}
section += count;
}
return nil;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
if (_delegate != nil) {
[_delegate controllerWillChangeContent:controller];
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
if (_delegate != nil) {
NSUInteger section = self.startingSection;
for (NSFetchedResultsController *fetchResultsController in _controllers) {
NSUInteger count = [[fetchResultsController sections] count];
if ([fetchResultsController isEqual:controller]) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + section];
NSIndexPath *tmpNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section + section];
[_delegate controller:controller didChangeObject:anObject atIndexPath:tmpIndexPath forChangeType:type newIndexPath:tmpNewIndexPath];
break;
}
section += count;
}
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
if (_delegate != nil) {
NSUInteger section = self.startingSection;
for (NSFetchedResultsController *fetchResultsController in _controllers) {
NSUInteger count = [[fetchResultsController sections] count];
if ([fetchResultsController isEqual:controller]) {
NSUInteger tmpSectionIndex = sectionIndex + section;
[_delegate controller:controller didChangeSection:sectionInfo atIndex:tmpSectionIndex forChangeType:type];
}
section += count;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
if (_delegate != nil) {
[_delegate controllerDidChangeContent:controller];
}
}
@end