0

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 )

    }
Zash__
  • 293
  • 4
  • 16
  • You'll need to move the code that needs the variable **into** the function, or pass in a callback block. See https://stackoverflow.com/questions/37244952/how-can-i-access-firebase-variable-outside-firebase-function or one of the other answers in this list https://www.google.com/search?q=site:stackoverflow.com+firebase+swift+variable+outside+of+function – Frank van Puffelen Jun 20 '17 at 16:29
  • Thanks for the quick answer, I am still lost haha, could you please show me one example? I moved the code into function but this does not work as well... – Zash__ Jun 20 '17 at 21:30
  • I mean I still get the correct number but I still can't assign it to the VideoArray – Zash__ Jun 20 '17 at 22:25

0 Answers0