【问题标题】:Not Upload everything at once Firebase不是一次上传所有内容 Firebase
【发布时间】:2017-09-20 00:59:39
【问题描述】:

我有一个应用程序,它在我的 firebase 数据库上搜索一个节点并从该节点中提取一堆图像 URL,然后在表格视图中显示链接到存储的图像。

问题是它会加载每张图片,可能是数百张图片。我想一次只做这么多,以防止数据过度使用。

理想情况下,当有人滚动到接近尾声时,它会感觉到,然后再上传,比如再上传 10 个。

谢谢,我才刚刚开始,所以有什么帮助。

func loadthumbimages()
{
        let ref = Database.database().reference(withPath: "ImageLocations/\(myspoits!)/\(DatesTableViewController.passdata!)")
        //observing the data changes
        ref.observe(DataEventType.value, with: { (snapshot) in
            //if the reference have some values
            if snapshot.childrenCount > 0
            {
                //iterating through all the values
                for mydata in snapshot.children.allObjects as! [DataSnapshot]
                {
                    //getting values
                    let artistObject = mydata.value as? [String: AnyObject]
                    // This is the data of the node
                    let fileURL  = artistObject?["fileURL"]
                    let thumbFileUrl  = artistObject?["thumbFileUrl"]
                    let ImageName = artistObject?["ImageName"]
                    //print(artistObject)
                    self.myarray1.append("\(ImageName!)")
                    self.myarray2.append("\(thumbFileUrl!)")
                    self.myarray3.append("\(fileURL!)")
                    //print(thumbFileUrl!)
                }
            }
        })
}

这是我的表格视图

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myarray1.count
}    
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
    let cell = tableView.dequeueReusableCell(withIdentifier: "MycustomCell", for: indexPath) as! MyimagecellTableViewCell   
   let urlString = "\(myarray2[indexPath.row])"

let url = URL(string: urlString)!
cell.myxcell.kf.setImage(with: url)
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("row: \(myarray2[indexPath.row])")
    FetchdataViewController.fullimg = "\(myarray3[indexPath.row])"
    FetchdataViewController.thumbimg = "\(myarray2[indexPath.row])"
    self.performSegue(withIdentifier: "photofull", sender: self)
}

【问题讨论】:

  • 一种方法是使用queryLimit 获取数据,第一次设置查询限制为10,然后第二次设置为20,以此类推

标签: ios firebase swift3 firebase-realtime-database


【解决方案1】:

好像您正在寻找分页 - 我有一个总是适合我的方法。

您应该在初始请求中将查询限制为分页数量,例如 20。此外,您需要按可递增索引(例如时间戳或分数)对查询进行排序。这个键将作为我们的分页光标来获取下一组图像。

我们可以通过获取图像数组中最后一个对象的可递增索引值来做到这一点——为了示例,我们将使用时间戳作为可递增索引。

当单元格即将显示在屏幕上时,会调用UITableViewDelegate 方法。我们可以使用此方法检测何时到达第 20 个单元格并使用光标获取下一批图像。

我们可以使用queryStartingAtValuequeryEndingAtValue 来检索光标之后/之前的图像并限制查询,因此我们只能得到 20 个。

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2012-10-15
    • 2018-10-05
    相关资源
    最近更新 更多