首页 > 解决方案 > 如何使 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()
        }
    }

标签: iosswiftfirebase-realtime-databasegif

解决方案


推荐阅读