如何在iOS应用程序上更快地查询Firebase数据库



我正在使用Firebase数据库向我的应用程序查询gif帖子。这些帖子需要花费大量时间才能加载到收藏视图中的应用程序上。我不清楚问题是在应用程序还是数据库中。

POSTS_REF.queryOrderedByKey().queryLimited(toLast: 9).observeSingleEvent(of: .value, with: {  [weak self] (snapshot) in
self?.tableView.refreshControl?.endRefreshing()
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.map({(snapshot) in
let postId = snapshot.key
self?.fetchPost(withPostId: postId)
})
self?.currentKey = first.key
})
func fetchPost(withPostId postId: String) {
Database.fetchPost(with: postId) { (post) in
self.posts.append(post)
self.posts.sort(by: { (post1, post2) -> Bool in
return post1.creationDate > post2.creationDate
})
self.collectionView?.reloadData()
}
}

将用于查询的field添加到实时数据库规则中。

例如,userName字段是user表的键。

{
"rules": {
".read": "auth.uid != null",
".write": "auth.uid != null",
"v1": {
"user": {
".indexOn": "userName"
}   
}
}
}

您可以像".indexOn": ["userName", "createdAt"]一样为.indexOn字段使用数组

最新更新