user6820041
user6820041

Reputation: 1223

Running sort on an array from Firebase on load?

On button press I'm doing certain filters on arrays I'm getting from Firebase. For example one of them is :

self.PersonalSearchesList = PersonalSearchesList.sorted{ $0.users > $1.users }
        self.tableView.reloadData()

The problem is that I'm wanting when the view initially loads I'm wanting one of the filters to be applied..but I don't know where to run the filter.

If I run the sort when the array is being appended to after the query my information/cells get all jacked up with incorrect information.

I can't run it in viewDiDLoad because the observer to firebase is being run in ViewDidLoad and at that point the array isn't going to be populated.

edit:

 currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for snap in snapshots {
                    DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
                        if snapshot.value != nil {
                            if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {
                                DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
                                  self.PersonalSearchesList.append(eachSearch)
                                })
                            }
                        }
                    })
                }
            }
        }
        initialLoadOver = true
    }

So at what point is the async over in this? After which line's closing bracket do I need to do the stuff?

Upvotes: 2

Views: 261

Answers (1)

&#214;zg&#252;r Ersil
&#214;zg&#252;r Ersil

Reputation: 7013

You can create an instance for sorted list and when its set, you can reload your data

var mySortedList = [YourListObject](){
   didSet{
      self.tableView.reloadData()
   }
}

then when your append operation is finished, just sort it.

 currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for snap in snapshots {
                    DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
                        if snapshot.value != nil {
                            if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {
                                DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
                                  self.PersonalSearchesList.append(eachSearch)
                                })
                            }
                        }
                    })
                }
               self.mySortedList = PersonalSearchesList.sort({ (element1, element2) -> Bool in
                    return element1.users! > element2.users!
               })
            }
        }
        initialLoadOver = true
    }

Upvotes: 2

Related Questions