I have this code to fetch data from Firebase Database:

    func fetchVinylsData(){
    guard let currentUID = Auth.auth().currentUser?.uid else { return }

    self.vinyls = [Vinyl]()
    dbRef.child("vinyls").child(currentUID).observe(.childAdded) { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String,AnyObject> else { return }
        let vinyl = Vinyl(dictionary: dictionary)

        self.vinyls.sort(by: { (vinyl1, vinyl2) -> Bool in
            if vinyl1.artist < vinyl2.artist {
                return true
            } else if vinyl1.artist == vinyl2.artist {
                return vinyl1.title < vinyl2.title
            } else {
                return false
        DispatchQueue.main.async {
            self.navigationItem.title = "List of Vinyls(\(self.vinyls.count))"

I call it when I launch my app on viewDidAppear and when I return to main page from another ViewController this function is executed and also when I delete an item from database or update an item. But since I get a lot of records inside my database (900 records and going up to approximately 4.000) I was wondering what is the best approach to handle large amounts of data inside firebase and swift to avoid slowing down my app.

0 Answers