我正在使用Firebase Real Database准备iOS本机应用程序。
我大约有10万条数据。我需要显示最近的50条帖子,并在加载更多的下50条帖子之后,因此我实施了50条记录的分页仍然花费了很长时间(大约20-30秒) )。
我还尝试使用索引在Firebase中使用规则。
仅供参考:可以处理1000条记录。
如何提高性能?
let post_messagesReference = self.databaseRef.child(FirebaseTablePostMessages)
var query = post_messagesReference.queryOrdered(byChild: kCreated)
var lastKey = String()
var createdVal = Int()
if let last = self.endKeyCommunity.first {
lastKey = last.id
createdVal = Int(last.created)
}
//get first 50 records
if self.endKeyCommunity.count == 0{
query = query.queryLimited(toLast:50)
self.showLoaderView()
} else { //get next 50 records
query = query.queryEnding(atValue: createdVal).queryLimited(toLast: 50)
}
query.observeSingleEvent(of: .value, with: { (snapshot) in
guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return }
if snapshot.childrenCount > 0 {
for child in snapshot.children.allObjects as! [DataSnapshot]{
guard case let communityChatDictionary as NSDictionary = child.value else { return }
if lastKey != child.key{
}
guard case let communityChatDictionary as NSDictionary = children.value else { return }
}
}) { (error) in
}
答案 0 :(得分:1)
尝试更改Firebase规则,以便进行设置:
".read": "query.orderByKey &&
query.limitToFirst <= 100"
删除此代码var query = post_messagesReference.queryOrdered(byChild: kCreated)
。只需这样调用您的分页请求即可:
post_messagesReference.limitToFirst(50)
.orderByKey(kCreated)