简体   繁体   中英

How do I make it such that .observe in Firebase retrieves all its data before calling reload collection view

How do I set it such that the function below knows when it has retrieved all data before calling reload collection view rather than reloading everytime a new URL is appended in the array.

func generateDataForRecents()  {
        if URLArrayStringRecents.count == 0 {
            self.activityIndicator2.isHidden = false
            self.activityIndicator2.startAnimating()
        }

        let queryGallery2 = FIRDatabase.database().reference().child("palettes")

        queryGallery2.observe(.childAdded, with: {(snapshot) in

            let URL = snapshot.childSnapshot(forPath: "URL").value as! String
            self.URLArrayStringRecents.append(URL)


            self.activityIndicator2.stopAnimating()
            self.activityIndicator2.isHidden = true

            self.whatsNewCollectionView.reloadData()
        })


    }

The reason why I need such a method is because I wish to implement pull to refresh to reload the collection view in my view controller.

Try :-

If you are calling for .value as FIRDataEventType only then its possible for you to check the count

let count = snapshot.childrenCount
if self.URLArrayStringRecents.count == Int(count){
       self.whatsNewCollectionView.reloadData()
    }

Otherwise if you are calling for .childAdded as FIRDataEventType , it will return single snapshot at a time , you can either fix up a NSTimer which waits for a certain time period after calling a .reloadData() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM