首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在刷新之前,第一个Firebase查询项出现问题

在刷新之前,第一个Firebase查询项出现问题
EN

Stack Overflow用户
提问于 2018-06-07 03:17:51
回答 1查看 50关注 0票数 2

我有一个firebase数据库来拉取50个具有最大整数值的用户,并从最高到最低显示它们。当我第一次进入排行榜视图时,就会出现这个问题。顺序应该在顶部显示jlewallen18,然后显示appledev。但在第一次加载时,appledev在顶部,直到我退出并再次打开排行榜(底部的代码)。

排行榜代码:

代码语言:javascript
复制
class LeaderboardViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var leaderboardTableView: UITableView!

    var userModel : [User] = [User]()

    let pipeline = ImagePipeline {
    //config settings for image display removed for brevity
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        leaderboardTableView.delegate = self
        leaderboardTableView.dataSource = self

        fetchUsers()
    }

    func fetchUsers() {
        let queryRef = Database.database().reference().child("users").queryOrdered(byChild: "ranking").queryLimited(toLast: 50)
        queryRef.observe(.childAdded, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String : AnyObject]{
                let user = User(dictionary: dictionary)
                self.userModel.append(user)
            }
            DispatchQueue.main.async(execute: {
                self.leaderboardTableView.reloadData()
            })
        })
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userModel.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "leaderboardTableCell", for: indexPath) as! LeaderboardTableCell
        if userModel.count > indexPath.row {
            if let profileImageURL = userModel[indexPath.row].photoURL {
                let url = URL(string: profileImageURL)!
                var options = ImageLoadingOptions()
                options.pipeline = pipeline
                options.transition = .fadeIn(duration: 0.25)
                Nuke.loadImage(
                    with: ImageRequest(url: url).processed(with: _ProgressiveBlurImageProcessor()),
                    options: options,
                    into: cell.userImage
                )
            }
        }
        cell.userName.text = userModel[indexPath.row].username
        cell.watchTime.text = "\(String(describing: userModel[indexPath.row].watchTime!))"
        cell.ranking.text = "\(indexPath.row + 1)"
        cell.userImage.layer.cornerRadius = cell.userImage.frame.size.width/2
        return cell
    }
}

我想这可能是因为我在我的个人资料页面视图和排行榜视图中都使用了相同的模型名称userModel,但是当我在排行榜视图中更改模型名称时,并没有发生任何变化。我还能分享些什么来帮助你?谢谢!

编辑:打印出watchTime后,这是我的控制台输出,它是我有排名的整数:

代码语言:javascript
复制
HERES WHERE I OPEN LEADERBOARD PAGE FIRST:
Optional(28)
Optional(247)
Optional(0)
Optional(0)
Optional(0)
Optional(0)
AFTER I GO BACK AND CLICK TO VIEW LEADERBOARD AGAIN:
Optional(247)
Optional(28)
Optional(0)
Optional(0)
Optional(0)
Optional(0)
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 05:08:32

这里的问题与这行代码有关...

代码语言:javascript
复制
let queryRef = Database.database().reference().child("users").queryOrdered(byChild: "ranking").queryLimited(toLast: 50)

将此限制更改为10会使应用程序按预期工作,这是一个临时的“修复”。

如果我们找出了这个限制导致问题的原因,我一定会更新这个答案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50728023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档