Skip to content

Commit 8f19ca8

Browse files
committed
implement error handling methods for indexed data sources
1 parent 1741907 commit 8f19ca8

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,41 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23+
@class FirebaseIndexCollectionViewDataSource;
24+
25+
@protocol FirebaseIndexCollectionViewDataSourceDelegate <NSObject>
26+
@optional
27+
28+
/**
29+
* Called when an individual reference responsible for populating one cell
30+
* of the collection view has raised an error. This error is unrecoverable, but
31+
* does not have any effect on the contents of other cells.
32+
* @param dataSource The FirebaseIndexCollectionViewDataSource raising the error.
33+
* @param ref The reference that failed to load.
34+
* @param index The index (i.e. row) of the query that failed to load.
35+
* @param error The error that occurred.
36+
*/
37+
- (void)dataSource:(FirebaseIndexCollectionViewDataSource *)dataSource
38+
reference:(FIRDatabaseReference *)ref
39+
didFailLoadAtIndex:(NSUInteger)index
40+
withError:(NSError *)error;
41+
42+
/**
43+
* Called when the index query used to initialize this data source raised an error.
44+
* This error is unrecoverable, and likely indicates a bad index query.
45+
* @param dataSource The FirebaseIndexCollectionViewDataSource raising the error.
46+
* @param error The error that occurred.
47+
*/
48+
- (void)dataSource:(FirebaseIndexCollectionViewDataSource *)dataSource
49+
indexQueryDidFailWithError:(NSError *)error;
50+
51+
@end
52+
53+
2354
@interface FirebaseIndexCollectionViewDataSource : NSObject <UICollectionViewDataSource>
2455

56+
@property (nonatomic, readwrite, weak, nullable) id<FirebaseIndexCollectionViewDataSourceDelegate> delegate;
57+
2558
- (instancetype)init NS_UNAVAILABLE;
2659

2760
/**
@@ -33,7 +66,7 @@ NS_ASSUME_NONNULL_BEGIN
3366
* @param collectionView The collection view that is populated by this data source. The
3467
* data source pulls updates from Firebase database, so it must maintain a reference
3568
* to the collection view in order to update its contents as the database pushes updates.
36-
* The table view is not retained by its data source.
69+
* The collection view is not retained by its data source.
3770
* @param cellIdentifier The cell reuse identifier used to dequeue reusable cells from
3871
* the collection view.
3972
* @param populateCell The closure invoked when populating a UICollectionViewCell (or subclass).

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
5151

5252
#pragma mark - FirebaseIndexArrayDelegate
5353

54+
- (void)array:(FirebaseIndexArray *)array
55+
reference:(FIRDatabaseReference *)ref
56+
atIndex:(NSUInteger)index
57+
didFailLoadWithError:(NSError *)error {
58+
if ([self.delegate respondsToSelector:@selector(dataSource:reference:didFailLoadAtIndex:withError:)]) {
59+
[self.delegate dataSource:self reference:ref didFailLoadAtIndex:index withError:error];
60+
}
61+
}
62+
5463
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error {
55-
// TODO: implement actual error handling
64+
if ([self.delegate respondsToSelector:@selector(dataSource:indexQueryDidFailWithError:)]) {
65+
[self.delegate dataSource:self indexQueryDidFailWithError:error];
66+
}
5667
NSLog(@"%@ Error: Firebase query cancelled with error %@", self, error);
5768
}
5869

FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,43 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23+
@class FirebaseIndexTableViewDataSource;
24+
25+
@protocol FirebaseIndexTableViewDataSourceDelegate <NSObject>
26+
@optional
27+
28+
/**
29+
* Called when an individual reference responsible for populating one cell
30+
* of the table view has raised an error. This error is unrecoverable, but
31+
* does not have any effect on the contents of other cells.
32+
* @param dataSource The FirebaseIndexTableViewDataSource raising the error.
33+
* @param ref The reference that failed to load.
34+
* @param index The index (i.e. row) of the query that failed to load.
35+
* @param error The error that occurred.
36+
*/
37+
- (void)dataSource:(FirebaseIndexTableViewDataSource *)dataSource
38+
reference:(FIRDatabaseReference *)ref
39+
didFailLoadAtIndex:(NSUInteger)index
40+
withError:(NSError *)error;
41+
42+
/**
43+
* Called when the index query used to initialize this data source raised an error.
44+
* This error is unrecoverable, and likely indicates a bad index query.
45+
* @param dataSource The FirebaseIndexTableViewDataSource raising the error.
46+
* @param error The error that occurred.
47+
*/
48+
- (void)dataSource:(FirebaseIndexTableViewDataSource *)dataSource
49+
indexQueryDidFailWithError:(NSError *)error;
50+
51+
@end
52+
2353
@interface FirebaseIndexTableViewDataSource : NSObject <UITableViewDataSource>
2454

55+
/**
56+
* The delegate that will receive events from this data source.
57+
*/
58+
@property (nonatomic, readwrite, weak, nullable) id<FirebaseIndexTableViewDataSourceDelegate> delegate;
59+
2560
- (instancetype)init NS_UNAVAILABLE;
2661

2762
/**
@@ -42,6 +77,7 @@ NS_ASSUME_NONNULL_BEGIN
4277
data:(FIRDatabaseReference *)dataQuery
4378
tableView:(UITableView *)tableView
4479
cellReuseIdentifier:(NSString *)cellIdentifier
80+
delegate:(nullable id<FirebaseIndexTableViewDataSourceDelegate>)delegate
4581
populateCell:(void (^)(UITableViewCell *cell,
4682
FIRDataSnapshot *_Nullable))populateCell NS_DESIGNATED_INITIALIZER;
4783

FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
4242
data:(FIRDatabaseReference *)dataQuery
4343
tableView:(UITableView *)tableView
4444
cellReuseIdentifier:(NSString *)cellIdentifier
45+
delegate:(nullable id<FirebaseIndexTableViewDataSourceDelegate>)delegate
4546
populateCell:(nonnull void (^)(UITableViewCell *cell,
4647
FIRDataSnapshot *data))populateCell {
4748
self = [super init];
@@ -59,8 +60,19 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
5960

6061
#pragma mark - FirebaseIndexArrayDelegate
6162

63+
- (void)array:(FirebaseIndexArray *)array
64+
reference:(FIRDatabaseReference *)ref
65+
atIndex:(NSUInteger)index
66+
didFailLoadWithError:(NSError *)error {
67+
if ([self.delegate respondsToSelector:@selector(dataSource:reference:didFailLoadAtIndex:withError:)]) {
68+
[self.delegate dataSource:self reference:ref didFailLoadAtIndex:index withError:error];
69+
}
70+
}
71+
6272
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error {
63-
// TODO: implement actual error handling
73+
if ([self.delegate respondsToSelector:@selector(dataSource:indexQueryDidFailWithError:)]) {
74+
[self.delegate dataSource:self indexQueryDidFailWithError:error];
75+
}
6476
NSLog(@"%@ Error: Firebase query cancelled with error %@", self, error);
6577
}
6678

0 commit comments

Comments
 (0)