Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a pagination query on Firebase's iOS SDK?

Tags:

Firebase Model

This is my model.

messagesRef = Firebase(url: "https://"+CONSTANTS.FirebaseDB+".firebaseio.com/messages/1:1000")     messagesRef.queryLimitedToLast(5).observeEventType(FEventType.ChildAdded, withBlock: { (snapshot) in         self.message_list.append(snapshot) // it works.     }); }); 

My code works -- it gets the last 5 messages (8-12).

However, what if I have a function query the next 5 messages (2-6)? With a start and offset. How can I query this?

like image 262
TIMEX Avatar asked Apr 10 '15 19:04

TIMEX


2 Answers

messagesRef = Firebase(url: "https://"+CONSTANTS.FirebaseDB+".firebaseio.com/messages/1:1000")messagesRef .queryOrderedByKey() .queryStartingAtValue(5) .queryEndingAtValue(10) .observeEventType(FEventType.ChildAdded, withBlock: { (snapshot) in self.message_list.append(snapshot) }); 

This is kind of a shot in the dark but it seems like it should work based on documentation here https://www.firebase.com/docs/ios-api/Classes/Firebase.html#//api/name/queryStartingAtValue:

like image 111
kpie Avatar answered Oct 14 '22 07:10

kpie


On spending too much time I have figured it out and here is the solution. This is Objective-C code you can convert it into swift. Call below function for paging purpose.

- (void)loadMoreMessages {          if (!lastMessageKey) {         // Loading messages first time         [[[msgsReference queryOrderedByKey] queryLimitedToLast:K_MESSAGES_PER_PAGE] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {             if (snapshot.exists) {                                  for (FIRDataSnapshot *child in snapshot.children) {                                         NSMutableDictionary *dict = [child.value mutableCopy];                     [dict setObject:child.key forKey:@"id"];                     [messages addObject:dict];                 }                                  lastMessageKey = [[snapshot.children.allObjects firstObject] key];                 NSLog(@"%@", messages);             }         }];     }     else {         // Paging started         [[[[msgsReference queryOrderedByKey] queryLimitedToLast:K_MESSAGES_PER_PAGE + 1] queryEndingAtValue:lastMessageKey] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {                         if (snapshot.exists) {                         NSInteger count = 0;                 NSMutableArray *newPage = [NSMutableArray new];                 for (FIRDataSnapshot *child in snapshot.children) {                                          // Ignore last object because this is duplicate of last page                     if (count == snapshot.childrenCount - 1) {                         break;                     }                                          count += 1;                     NSMutableDictionary *dict = [child.value mutableCopy];                     [dict setObject:child.key forKey:@"id"];                     [newPage addObject:dict];                 }                                  lastMessageKey = [[snapshot.children.allObjects firstObject] key];                  // Insert new messages at top of old array                 NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [newPage count])];                 [messages insertObjects:newPage atIndexes:indexes];                 NSLog(@"%@", messages);             }         }];     } } 

And here is description of objects you needed:

#define K_MESSAGES_PER_PAGE 50 // A macro defining the numbers in one request msgsReference // Firebase database messages node reference I'm also attaching the screenshot of my db structure for make you more clear lastMessageKey // Is a NSString object which store the first key of last page messages // Is a NSMutableArray storing the result 

Good Luck!! (y)

enter image description here

like image 20
TheTiger Avatar answered Oct 14 '22 08:10

TheTiger