在開發項目中,咱們會常常用到數據查詢和分頁加載。咱們先了解一下 Query 的幾個方法,而後實現分頁加載。關於分頁加載,在傳統(有後端服務)的開發中,通常得須要後端同窗的協助,給前端同窗返回數據,再讓前端同窗實現功能,這樣會費事費人力。目前,給你們介紹一下 Wilddog 無後端開發分頁加載,你能夠自定製實現該功能。前端
1.1 經過 ID 選 user (WHERE id = x)ios
將咱們的用戶存到了 /user 節點下面,因此,咱們能夠這樣獲取用戶信息:sql
Wilddog *ref = [[Wilddog alloc]initWithUrl:@"https://example-data-sql.wilddogio.com/user/1"]; [ref observeSingleEventOfType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) { NSLog(@"I fetched a user %@",snapshot.value); }];
1.2 經過 email address 尋找 user (WHERE email = x)數據庫
Wilddog *ref = [[Wilddog alloc]initWithUrl:@"https://example-data-sql.wilddogio.com/user"]; WQuery *query = [ref queryEqualToValue:@"kato@wilddog.com"]; [query observeSingleEventOfType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) { NSLog(@"accounts matching email address %@",snapshot.value); }];
1.3 獲取一段時間發送的信息(WHERE timestamp BETWEEN x AND y)後端
Wilddog *ref = [[Wilddog alloc]initWithUrl:@"https://example-data-sql.wilddogio.com/messages"]; Query *query = [ref queryStartingAtValue:startTime]; query = [query queryEndingAtValue:endTime]; [query observeSingleEventOfType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) { NSLog(@"messages in range %@",snapshot.value); }];
2.1 – queryStartingAtValue:返回一個WQuery引用,這個引用用來監測數據的變化,這些被監測的數據的值均大於或等於startValueapi
//篩選出高度大於等於3m的恐龍 Wilddog *ref = [[Wilddog alloc] initWithUrl:@"https://dinosaur-facts.wilddogio.com/dinosaurs"]; WQuery *query = [ref queryOrderedByChild:@"height"]; query =[query queryStartingAtValue:@3]; [query observeEventType:WEventTypeChildAdded withBlock:^(WDataSnapshot *snapshot) { NSLog(@"%@",snapshot.key); }];
2.2 - queryEndingAtValue:返回一個WQuery引用,這個引用用來監測數據的變化,這些被監測的數據的值均小於或者等於endValueapp
//篩選出高度小於等於1m的恐龍 Wilddog *ref = [[Wilddog alloc] initWithUrl:@"https://dinosaur-facts.wilddogio.com/dinosaurs"]; WQuery *query = [ref queryOrderedByChild:@"height"]; query =[query queryEndingAtValue:@1]; [query observeEventType:WEventTypeChildAdded withBlock:^(WDataSnapshot *snapshot) { NSLog(@"%@",snapshot.key); }];
2.3 - queryLimitedToFirst:返回一個新WQuery引用,獲取從第一條開始的指定數量的數據fetch
//篩選出數據庫前100只恐龍 Wilddog *ref = [[Wilddog alloc] initWithUrl:@"https://dinosaur-facts.wilddogio.com/dinosaurs"]; WQuery *query = [ref queryLimitedToFirst:100]; [query observeEventType:WEventTypeChildAdded withBlock:^(WDataSnapshot *snapshot) { NSLog(@"%@",snapshot.key); }];
2.4 - observeEventType:withBlock:獲取初始數據和監聽某一節點處數據的變化atom
//獲取初始的那些恐龍和監聽實時增長的恐龍 Wilddog *ref = [[Wilddog alloc] initWithUrl:@"https://dinosaur-facts.wilddogio.com/dinosaurs"]; [ref observeEventType:WEventTypeChildAdded withBlock:^(WDataSnapshot *snapshot) { NSLog(@"%@",snapshot.key); }];
如今用一個筆記本的實例來舉例實現下拉加載,結合 - queryOrderedByChild: 、– queryStartingAtValue: 、– queryLimitedToFirst: 等方法實現分頁加載。
在 .h 文件中spa
@interface NoteService () @property (nonatomic,strong) Wilddog *wilddog; @property (nonatomic,strong) NSNumber *lastUpdatetimeOnPrevPage; @end @implementation NoteService -(instancetype)init { if (self = [super init]) { _wilddog = [[Wilddog alloc] initWithUrl: @"https://<your-noteapp-name>.wilddogio.com/note"]; _lastUpdatetimeOnPrevPage = [NSNumber numberWithLong:0]; } return self; }
假如咱們按照最後編輯時間做爲排序,一次性把全部的筆記都拉下來,咱們能夠這樣寫(這兒不是分頁):
-(void)getAllNotes:(void (^)(NSArray *))complete { WQuery *query = [self.wilddog queryOrderedByChild:@"lastUpdate"]; [query observeSingleEventOfType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) { if (snapshot) { NSMutableArray *noteList = [NSMutableArray new]; [snapshot.value enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NoteModel *model = [[NoteModel alloc] initWithDict:obj]; [noteList addObject:model]; }]; if (complete) { complete(noteList); } } }]; }
回到重點,咱們實現分頁加載:
-(void)getNotesWithPate:(int)page complete:(void (^)(NSArray *))complete { BOOL isFirstPath = page == 0; NSString *orderKey = @"lastUpdate"; WQuery *query = [self.wilddog queryOrderedByChild:orderKey]; query = [query queryStartingAtValue:_lastUpdatetimeOnPrevPage]; // kDefaultLimitPerPage 一次定義拉取多少條 query = [query queryLimitedToFirst:kDefaultLimitPerPage + (isFirstPath ? 0 : 1)]; //queryStartingAtValue 是大於等於上次頁最後一個的值,因此要 kDefaultLimitPerPage + 1個,而後去掉這個上一頁最後一個。 [query observeSingleEventOfType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) { if (snapshot) { NSMutableArray *noteList = [NSMutableArray new]; WDataSnapshot *child = nil; NSEnumerator *enumerator = snapshot.children; while (child = [enumerator nextObject]) { NoteModel *model = [[NoteModel alloc] initWithDict:[child value]]; if ([self.lastUpdatetimeOnPrevPage longLongValue] == model.lastUpdate) { continue; //去掉重複的一個 } NSLog(@"model.lastUpdate %lld",model.lastUpdate); [noteList addObject:model]; } if (noteList.count > 0) { self.lastUpdatetimeOnPrevPage = @([(NoteModel *)noteList.lastObject lastUpdate]); } if (complete) { complete(noteList); } } } withCancelBlock:^(NSError *error) { }]; }
在上拉加載的時候,只要將參數 page 自加傳入便可。當每一次數據加載完成以後,咱們用 complete block 將參數回調。
數據的查詢和加載,更多須要的接口能夠參考 iOS API