2
rootRef.observe(.value, with: { (snap) in
        numberOfRows = Int(snap.childrenCount)
        print(numberOfRows)
    })

This function is inside viewDidLoad(), the print statement prints 5 (value in database), but if I print it outside this method it prints 0, how can I fix this?

Itchydon
  • 2,572
  • 6
  • 19
  • 33
bcye
  • 758
  • 5
  • 22
  • Data is read from Firebase asynchronously. This means that all code that requires the data must be *inside* the completion handler. See https://stackoverflow.com/questions/28390635/can-swift-return-value-from-an-async-void-returning-block – Frank van Puffelen Aug 29 '17 at 03:09
  • try sending your value to app delegate and second use dispatch queue as it perform asynchronous way, third instead of directly writing your handler in didLoad take it in a function and call that function in didLoad() if issue still exist can you please share code file or more code like when that value turns out to be nil and declaration of numberofRows – iOS Geek Aug 29 '17 at 05:46

0 Answers0