-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRZDatabaseViewMapper.m
410 lines (359 loc) · 16.4 KB
/
FRZDatabaseViewMapper.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// FRZDatabaseViewMapper.m
// Forza Football
//
// Created by Joel Ekström on 2018-02-01.
// Copyright © 2018 FootballAddicts. All rights reserved.
//
#import "FRZDatabaseViewMapper.h"
#import <YapDatabase/YapDatabase.h>
#import <YapDatabase/YapDatabaseView.h>
#import <YapDatabase/YapDatabaseViewMappings.h>
@interface FRZDatabaseViewMapper()
@property (nonatomic, strong) YapDatabaseConnection *connection;
@property (nonatomic, strong) NSMutableDictionary<NSIndexPath *, YapCollectionKey *> *cache;
@end
@implementation FRZDatabaseViewMapper
- (instancetype)initWithDatabase:(YapDatabase *)database
{
if (self = [super init]) {
_cache = [NSMutableDictionary new];
_connection = [database newConnection];
[_connection beginLongLivedReadTransaction];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yapDatabaseModified:) name:YapDatabaseModifiedNotification object:database];
}
return self;
}
- (instancetype)initWithConnection:(YapDatabaseConnection *)connection updateNotificationName:(NSNotificationName)updateNotificationName
{
if (self = [super init]) {
if (!connection.isInLongLivedReadTransaction) {
[NSException raise:NSInternalInconsistencyException format:@"%@ requires a connection in a long lived read transaction", NSStringFromClass(self.class)];
}
_cache = [NSMutableDictionary new];
_connection = connection;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(databaseConnectionDidUpdate:) name:updateNotificationName object:connection];
}
return self;
}
#pragma mark - Public
- (void)setShouldPauseUpdates:(BOOL)shouldPauseUpdates
{
if (_shouldPauseUpdates == YES && shouldPauseUpdates == NO) {
[self fastForwardActiveViewMappingsAndViewAnimated:NO];
}
_shouldPauseUpdates = shouldPauseUpdates;
}
- (void)setView:(id<FRZDatabaseMappable>)view
{
_view = view;
if (self.activeViewMappings.count > 0) {
[_view reloadData];
}
}
- (NSUInteger)numberOfSections
{
return [[self.activeViewMappings valueForKeyPath:@"@sum.numberOfSections"] integerValue];
}
- (NSUInteger)numberOfItemsInSection:(NSUInteger)section
{
YapDatabaseViewMappings *mappings = nil;
NSUInteger actualSection = [self getGroup:nil mappings:&mappings forSection:section];
return [mappings numberOfItemsInSection:actualSection];
}
- (NSString *)groupForSection:(NSUInteger)section
{
NSString *group = nil;
[self getGroup:&group mappings:nil forSection:section];
return group;
}
- (YapDatabaseViewMappings *)mappingsForSection:(NSUInteger)section withInternalSection:(NSUInteger *)internalSection
{
YapDatabaseViewMappings *mappings = nil;
NSUInteger sectionWithinMappings = [self getGroup:nil mappings:&mappings forSection:section];
if (internalSection) {
*internalSection = sectionWithinMappings;
}
return mappings;
}
- (NSRange)sectionRangeForMappings:(YapDatabaseViewMappings *)mappings
{
NSAssert([self.activeViewMappings containsObject:mappings], @"Attempted to get sections for non-managed mappings");
NSInteger index = [self.activeViewMappings indexOfObject:mappings];
NSArray<YapDatabaseViewMappings *> *mappingsBefore = [self.activeViewMappings subarrayWithRange:NSMakeRange(0, index)];
return NSMakeRange([[mappingsBefore valueForKeyPath:@"@sum.numberOfSections"] integerValue], mappings.numberOfSections);
}
- (id)objectAtIndexPath:(NSIndexPath *)indexPath
{
id object = nil;
[self getObject:&object collection:nil key:nil metadata:nil atIndexPath:indexPath];
return object;
}
- (void)getObject:(__autoreleasing id *)object collection:(NSString *__autoreleasing *)collection key:(NSString *__autoreleasing *)key metadata:(__autoreleasing id *)metadata atIndexPath:(NSIndexPath *)indexPath
{
[self.connection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
YapCollectionKey *collectionKey = self.cache[indexPath];
if (collectionKey == nil) {
YapDatabaseViewMappings *mappings = nil;
NSString *collection = nil;
NSString *key = nil;
NSInteger actualSection = [self getGroup:nil mappings:&mappings forSection:indexPath.section];
[[transaction extension:mappings.view] getKey:&key collection:&collection forRow:indexPath.row inSection:actualSection withMappings:mappings];
collectionKey = [[YapCollectionKey alloc] initWithCollection:collection key:key];
self.cache[indexPath] = collectionKey;
}
if (key) *key = collectionKey.key;
if (object) *object = [transaction objectForKey:collectionKey.key inCollection:collectionKey.collection];
if (metadata) *metadata = [transaction metadataForKey:collectionKey.key inCollection:collectionKey.collection];
if (collection) *collection = collectionKey.collection;
}];
}
- (void)setActiveViewMappings:(NSArray<YapDatabaseViewMappings *> *)activeViewMappings
{
[self setActiveViewMappings:activeViewMappings animated:NO];
}
- (void)setActiveViewMappings:(NSArray<YapDatabaseViewMappings *> *)activeViewMappings animated:(BOOL)animated
{
_activeViewMappings = activeViewMappings;
[self fastForwardActiveViewMappingsAndViewAnimated:animated];
}
- (void)removeMappings:(YapDatabaseViewMappings *)mappings animated:(BOOL)animated
{
NSRange sectionRange = [self sectionRangeForMappings:mappings];
NSMutableArray *mutableMappings = [self.activeViewMappings mutableCopy];
[mutableMappings removeObject:mappings];
_activeViewMappings = [mutableMappings copy];
if (animated) {
[self.view deleteSections:[[NSIndexSet alloc] initWithIndexesInRange:sectionRange]];
} else {
[self.view reloadData];
}
}
- (void)insertMappings:(YapDatabaseViewMappings *)mappings atIndex:(NSUInteger)index animated:(BOOL)animated
{
[self.connection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[mappings updateWithTransaction:transaction];
}];
NSMutableArray *mutableMappings = [self.activeViewMappings mutableCopy];
[mutableMappings insertObject:mappings atIndex:index];
_activeViewMappings = [mutableMappings copy];
if (animated) {
[self.view insertSections:[[NSIndexSet alloc] initWithIndexesInRange:[self sectionRangeForMappings:mappings]]];
} else {
[self.view reloadData];
}
}
#pragma mark - Private
/**
Get the group and/or mappings controlling an external section. Also returns the
actual section within these mappings.
*/
- (NSUInteger)getGroup:(NSString **)group mappings:(YapDatabaseViewMappings **)managingMappings forSection:(NSUInteger)section
{
NSInteger sectionOffset = 0;
for (YapDatabaseViewMappings *mappings in self.activeViewMappings) {
NSInteger numberOfSections = mappings.numberOfSections;
if (section < numberOfSections + sectionOffset) {
if (managingMappings) {
*managingMappings = mappings;
}
NSInteger actualSection = section - sectionOffset;
if (group) {
*group = mappings.visibleGroups[actualSection];
}
return actualSection;
}
sectionOffset += numberOfSections;
}
[NSException raise:NSInternalInconsistencyException format:@"Attempted to get a group and/or view mappings for a section that is out of range: %i", (int)section];
return NSNotFound;
}
#pragma mark - Database update handling
- (void)willBeginUpdates
{
if ([self.delegate respondsToSelector:@selector(databaseViewMapperWillBeginUpdatingView:)]) {
[self.delegate databaseViewMapperWillBeginUpdatingView:self];
}
}
- (void)didEndUpdates
{
if ([self.delegate respondsToSelector:@selector(databaseViewMapperDidEndUpdatingView:)]) {
[self.delegate databaseViewMapperDidEndUpdatingView:self];
}
}
/**
This method is only called if the view mapper is managing its own, internal database connection.
It begins a new long-lived read transaction and forwards the change notifications to the normal handler
*/
- (void)yapDatabaseModified:(NSNotification *)notification
{
NSArray<NSNotification *> *changeNotifications = [self.connection beginLongLivedReadTransaction];
[self updateWithNotifications:changeNotifications];
}
/**
This method is called when an external YapDatabaseConnection is updated, with the registered
NSNotificationName. Used when the client app manages the connection.
*/
- (void)databaseConnectionDidUpdate:(NSNotification *)notification
{
NSParameterAssert(notification.userInfo[@"notifications"]);
[self updateWithNotifications:notification.userInfo[@"notifications"]];
}
- (void)updateWithNotifications:(NSArray<NSNotification *> *)notifications
{
if (self.activeViewMappings.count == 0 || self.shouldPauseUpdates) {
return;
}
if (![self hasAnyChangesForNotifications:notifications]) {
[self updateActiveViewMappings];
return;
}
[self willBeginUpdates];
if (self.animationStyle != FRZDatabaseViewMapperAnimationStyleFull) {
[self fastForwardActiveViewMappingsAndViewAnimated:self.animationStyle == FRZDatabaseViewMapperAnimationStyleCrossDissolve];
[self didEndUpdates];
return;
}
/**
This is a crash fix for a bug in UITableView and UICollectionView. Reloads are not compatible
with other types of updates inside the same block of batch updates, so we run them independently
before. We must make sure that objectAtIndexPath: etc returns objects from the cache during reload,
since the view mappings will not contain the correct rowid's.
More info here: https://github.com/yapstudios/YapDatabase/issues/489
*/
NSSet<YapCollectionKey *> *collectionKeys = [self updatedCollectionKeysInNotifications:notifications];
NSSet<NSIndexPath *> *updatedIndexPaths = [self.cache keysOfEntriesPassingTest:^BOOL(NSIndexPath *indexPath, YapCollectionKey *collectionKey, BOOL *stop) {
return [collectionKeys containsObject:collectionKey];
}];
if (updatedIndexPaths.count > 0) [self.view reloadItemsAtIndexPaths:updatedIndexPaths.allObjects];
// Clear the cache, since now we will be doing actual changes to the view, and items will receive new index paths
[self.cache removeAllObjects];
[self.view frz_performBatchUpdates:^{
[self performInsertsDeletesAndMovesForNotifications:notifications];
} completion:^(BOOL finished) {
[self didEndUpdates];
}];
}
- (FRZDatabaseViewMapperAnimationStyle)animationStyle
{
// Optimization: Don't perform animated updates if the view is not in the window hierarchy
if ([self.view isKindOfClass:UIView.class] && [(UIView *)self.view window] == nil) {
return FRZDatabaseViewMapperAnimationStyleNone;
}
return _animationStyle;
}
/**
A quick check to see if any of the views currently being managed have any changes since
the latest update of the connection
*/
- (BOOL)hasAnyChangesForNotifications:(NSArray<NSNotification *> *)notifications
{
for (YapDatabaseViewMappings *mappings in self.activeViewMappings) {
YapDatabaseViewConnection *viewConnection = [self.connection extension:mappings.view];
if ([viewConnection hasChangesForNotifications:notifications]) {
return YES;
}
}
return NO;
}
/**
Updates active view mappings to the latest commit on connection, without
calculating any changeset
*/
- (void)updateActiveViewMappings
{
[self.connection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
for (YapDatabaseViewMappings *mappings in self.activeViewMappings) {
[mappings updateWithTransaction:transaction];
}
}];
}
/**
Fast forwards all active view mappings to the latest database commit,
and reflects the changes in the view, with an optional animation.
The animation is a simpler transition, it doesn't actually do
propert updates/inserts/deletes/moves.
*/
- (void)fastForwardActiveViewMappingsAndViewAnimated:(BOOL)animated
{
[self.cache removeAllObjects];
[self updateActiveViewMappings];
if (animated && [self.view isKindOfClass:UIView.class]) {
[UIView transitionWithView:(UIView *)self.view
duration:0.22
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
animations:^{ [self.view reloadData]; }
completion:nil];
} else {
[self.view reloadData];
}
}
/**
Since section changes and row changes are per view mappings, this function will map the changes from individual mappings
into the actual sections seen by the table view.
This function will update activeViewMappings to the current database commit, and run
inserts/deletes/moves but skip updates.
*/
- (void)performInsertsDeletesAndMovesForNotifications:(NSArray *)notifications
{
// Deletes and work on the table view states _before_ any other updates have happened,
// while inserts work on the state after the deletes have happened. Therefore, we must keep
// both the previous and new offsets for each view mapping when we map the changes.
__block NSInteger sectionOffsetBeforeDeletions = 0;
__block NSInteger sectionOffsetAfterDeletions = 0;
[self.activeViewMappings enumerateObjectsUsingBlock:^(YapDatabaseViewMappings *mappings, NSUInteger index, BOOL *stop) {
NSArray *sectionChanges = nil;
NSArray *rowChanges = nil;
NSInteger numberOfSectionsBeforeUpdates = mappings.numberOfSections;
[[self.connection extension:mappings.view] getSectionChanges:§ionChanges rowChanges:&rowChanges forNotifications:notifications withMappings:mappings];
for (YapDatabaseViewSectionChange *change in sectionChanges) {
if (change.type == YapDatabaseViewChangeDelete) {
[self.view deleteSections:[NSIndexSet indexSetWithIndex:change.index + sectionOffsetBeforeDeletions]];
} else {
[self.view insertSections:[NSIndexSet indexSetWithIndex:change.index + sectionOffsetAfterDeletions]];
}
}
for (YapDatabaseViewRowChange *change in rowChanges) {
switch (change.type) {
case YapDatabaseViewChangeDelete: {
NSUInteger mappedSection = change.indexPath.section + sectionOffsetBeforeDeletions;
[self.view deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:change.indexPath.item inSection:mappedSection]]];
break;
}
case YapDatabaseViewChangeInsert: {
NSUInteger mappedSection = change.newIndexPath.section + sectionOffsetAfterDeletions;
[self.view insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:change.newIndexPath.item inSection:mappedSection]]];
break;
}
case YapDatabaseViewChangeUpdate: {
// Do nothing here - reloads were handled independently to fix crashes
break;
}
case YapDatabaseViewChangeMove: {
NSUInteger mappedFromSection = change.indexPath.section + sectionOffsetBeforeDeletions;
NSUInteger mappedToSection = change.newIndexPath.section + sectionOffsetAfterDeletions;
[self.view moveItemAtIndexPath:[NSIndexPath indexPathForItem:change.indexPath.item inSection:mappedFromSection]
toIndexPath:[NSIndexPath indexPathForItem:change.newIndexPath.item inSection:mappedToSection]];
break;
}
}
}
sectionOffsetBeforeDeletions += numberOfSectionsBeforeUpdates;
sectionOffsetAfterDeletions += mappings.numberOfSections;
}];
}
- (NSSet<YapCollectionKey *> *)updatedCollectionKeysInNotifications:(NSArray<NSNotification *> *)notifications
{
NSMutableSet<YapCollectionKey *> *collectionKeys = [NSMutableSet new];
for (NSNotification *notification in notifications) {
for (NSDictionary *extension in [notification.userInfo[YapDatabaseExtensionsKey] allValues]) {
for (id change in extension[YapDatabaseViewChangesKey]) {
if ([change isKindOfClass:YapDatabaseViewRowChange.class] && [(YapDatabaseViewRowChange *)change type] == YapDatabaseViewChangeUpdate) {
[collectionKeys addObject:[(YapDatabaseViewRowChange *)change collectionKey]];
}
}
}
}
return collectionKeys;
}
@end