So I am currently trying to get the number of "Posts" within my Firebase Database, but for some reason, it says that the amount equal to one (which is true) but then resets itself when calling "postNumber". I also want to keep the data in the array for later use so I kept it all in an array rather than reading straight off the Database and not storing any data at all.
viewDidLoad() Function
var postNumber = 0
override func viewDidLoad() {
super.viewDidLoad()
updateFeed()
print("PostNumber in ViewDidLoad: \(self.postNumber)") //equals 0
collectionView?.backgroundColor = .white
}
updateFeed Function
func updateFeed(){
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference(fromURL: "https://firebaseio.com/")
ref.child("users").child(uid!).child("Posts").observe(.value, with: { (snapshot) in
for item in snapshot.children{
let snapshotValue = snapshot.value as? NSDictionary
var snapString = String(describing: item)
var snapInt = Int(snapString)
self.postCollection.append(snapString)
let firstName = (snapshot.value as! NSDictionary)["First Person"] as? String
print("SnapShot: \(snapString)") //gives proper data
self.postNumber = self.postCollection.count
print("PostNumber: \(self.postNumber)") //equals 1
print("PostSource: \(self.postCollection)")
}
})
print("Number of Posts (After Adding): \(self.postCollection.count)") //equals 0
collectionView?.reloadData()
}