不立即上传所有内容Firebase

时间:2017-09-20 00:59:39

标签: ios firebase swift3 firebase-realtime-database

我有一个应用程序,它搜索我的firebase数据库上的节点,从节点中提取一堆Image 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)
}

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

相关问题