0

I have a function I am trying to call that collects all my users from firebase and then divided them into two groups by looking at each user and checking a child node. If the child node is one for example they are put into group one. If their child node is two, then they are put into group 2. Here is the current code I am working with.

var allUsersArray = [User]()
var macroUsersArray = [User]()
var microUsersArray = [User]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadAllUsersAndPopulateArray()
    }

func loadAllUsersAndPopulateArray() {
    let ref = Database.database().reference().child("Users")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        let allUsersSnapshot = snapshot.children.allObjects as! [DataSnapshot]
        for userSnap in allUsersSnapshot {
            let user = User(from: userSnap)
            self.allUsersArray.append(user!)
        }
        self.macroUsersArray = self.allUsersArray.filter { $0.Tag == "Macro" }
        self.microUsersArray = self.allUsersArray.filter { $0.Tag == "Micro" }

       // print(A)     
       print(self.allUsersArray)       

        self.observeChangeInUserProperty()
    })

       //print(B)
       print(self.allUsersArray)

}

func observeChangeInUserProperty() {
    let ref = Database.database().reference().child("Users")
    ref.observe(.childChanged, with: { snapshot in
        let key = snapshot.key

        let tag = snapshot.childSnapshot(forPath: "Tag").value as! String // ! = never optional
        //get the user from the allUsersArray by its key
        if let user = self.allUsersArray.first(where: { $0.Tag == key }) {
            if user.Tag != tag { //if the tag changed, handle it
                user.Tag = tag //update the allUsersArray
                if tag == "Macro" { //if the new tag is Macro remove the user from the Micro array
                    if let userIndex = self.microUsersArray.firstIndex(where: { $0.Tag == key }) {
                        self.microUsersArray.remove(at: userIndex)
                        self.macroUsersArray.append(user) //add user to macro array
                    }
                } else { //new type is micro so remove from macro array
                    if let userIndex = self.macroUsersArray.firstIndex(where: { $0.Tag == key }) {
                        self.macroUsersArray.remove(at: userIndex)
                        self.microUsersArray.append(user)
                    }
                }
                //reload the tableviews to reflect the changes
                self.tableView.reloadData()
                self.microTableView.reloadData()

            }
        }
    })
}

Here is what the User Class looks like:

import UIKit
import Firebase

class User: NSObject {
    var Name: String?
    var Email: String?
    var UID: String?
    var Tag: String?

init?(from snapshot: DataSnapshot) {

        let dictionary = snapshot.value as? [String: Any]
    
        self.Name = dictionary!["Name"] as? String
        self.Email = dictionary!["Email"] as? String
        self.UID = dictionary!["UID"] as? String
        self.Tag = dictionary!["Tag"] as? String
        
    
    }

 }

Whenever I print(self.allUsersArray) from within the ref.observeSingleEvent{} within the loadAllUsersAndPopulateArray(), I can see the list of users. When I move the print(self.allUsersArray) just within the func loadAllUsersAndPopulateArray(), I get an empty array. When I print the arrays in the viewDidLoad() I also get empty arrays. I really need to figure out how to append these arrays permanently so I can start to set up tables.

Edit:

Whenever I print(A) I get the arrays with their appropriate users.

Whenever I print(B) I get empty arrays meaning that the arrays are still not being appended.

NewCoder
  • 35
  • 6
  • Could you not use just `print(self.allUsersArray)`; but instead `print(allUsersArray in ZzZ: \(self.allUsersArray)`), where ZzZ would be "within the ref.observeSingleEvent{}", "within the loadAllUsersAndPopulateArray()" etc accordingly. Could you put them all (from yours tests), give us the exact output (where it's empty, where it's not), AND tell us in which order it's printed. (and which order did you think it would have been printed). I think you are missing the asynchrone concept, BUT since you don't differentiates your prints, you don't see it. – Larme Dec 28 '20 at 08:48
  • Hey Larme. I apologize. You are correct. I should have labeled the prints. I have edited the question to show the new labels. I am having a problem with the asynchrone and need help getting my arrays to be appended outside the closure. – NewCoder Dec 28 '20 at 19:20
  • I have answered the question here in my previous question https://stackoverflow.com/questions/65136645/change-in-firebase-doesnt-refresh-table-tables-not-displaying-user-nodes-from/65483770#65483770 – NewCoder Dec 28 '20 at 20:39

0 Answers0