I want to fetch the count of posts and then assign the returned value to a variable.
I get the correct count of posts but the variable does not change.
here is how I fetch the count ( which works fine)
Note: this in my api
func fetchVideosFailsCount( completion: @escaping (Int) -> Void) {
API.Kategorien.REF_FAILS_VIDEOS.observe(.value, with: {
snapshot in
let count = Int(snapshot.childrenCount)
completion(count)
})
}
now I want to assign this to a variable in a different class
var anzahlInFails : String = ""
print("anzahl ist", anzahlInFails)
API.Kategorien.fetchVideosFailsCount() { (count) in
let one = "\(count)"
anzahlInFails = one
print(one)
}
print("anzahl ist", anzahlInFails)
I want anzahlInFails to be manipulated by the value one represents.
Instead both print statements send me a "" as value.
i have problems with this because I have no label I can assign it to (did that without problem) because I am working in a collection view.
The class videos is following
class Videos {
var title = ""
var featuredImage: UIImage
var color: UIColor
var anzahlBeiträge : String
init(title: String, featuredImage: UIImage, color: UIColor, anzahlBeiträge: String)
{
self.anzahlBeiträge = anzahlBeiträge
self.title = title
self.featuredImage = featuredImage
self.color = color
}
static func fetchInterests() -> [Videos]
{
var anzahlInFails : String = ""
print("anzahl ist", anzahlInFails)
API.Kategorien.fetchVideosFailsCount() { (count) in
let one = "\(count)"
anzahlInFails = one
print(one)
}
print("anzahl ist", anzahlInFails)
return [
Videos(title: "Cat1", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: " Beiträge geteil"),
Videos(title: "Cat2", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "142 Beiträge geteil"),
Videos(title: "Cat3", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "39432 Beiträge geteil"),
Videos(title: "Cat4", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "2482 Beiträge geteil"),
Videos(title: "Cat15", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "67912 Beiträge geteil"),
]
}
}
I want each {anzahlBeiträge} to represent the actual value of the function I use.
Any help? thanks in advance :)
Update : Current code for fetching the number of posts
func counterVideosFails(completion: @escaping (_ count: Int)->()) {
API.Kategorien.REF_FAILS_VIDEOS.observe(.value, with: { (snapshot) in
let count = Int(snapshot.childrenCount)
completion(count)
})
}
counterVideosFails() { (count)
in
let countOfFailsVideos = count
print("countOfFailsVideos", countOfFailsVideos )
}