У меня есть эта группа кода, я хочу запустить только: self.performSegue
ПОСЛЕ того, как все циклы for и все асинхронные задачи Firebase завершили работу:
getFeaturedPost(withCompletion: startNext)
func getFeaturedPost(withCompletion completion: () -> Void ) {
print("Getting featured posts...")
ref.child("featured").child("amount").observeSingleEvent(of: .value, with: { (snapshot) in
self.numberOfPosts = snapshot.value as! Int
print("There's \(self.numberOfPosts) posts avaliable.")
for pos in 0..<self.numberOfPosts{
print("Getting reference names for post: \(pos + 1)")
self.ref.child("featured").child("post\(pos + 1)").observeSingleEvent(of: .value, with: { (snapshot) in
let postID = (snapshot.value as? NSDictionary)?["postID"] as? String ?? ""
let userOfPost = (snapshot.value as? NSDictionary)?["userOfPost"] as? String ?? ""
self.customValues.append(("/users/public/\(userOfPost)/posts/\(postID)"))
})
}
})
print("Done! The posts are: \(customValues)")
completion()
}
func startNext()
{
getPostData(withCompletion: {
print("Finished getting data, going to main screen.")
self.performSegue(withIdentifier: "showHome", sender: nil)
})
}
func getPostData(withCompletion completion: () -> Void ) {
print("Getting idividual post data, there are \(customValues.count) posts")
for i in 0..<customValues.count {
print("Currently on post: \(i)")
let encodedURL = (customValues[i] + "/postURL")
ref.child(encodedURL).observe(.value, with: { (snapshot) in
if let newURL = snapshot.value as? String{
print("Sending \(newURL) to DemoSource Class")
DemoSource.shared.add(urlString: newURL)
}
})
}
completion()
}
Все же startNext()
Функция (которая переходит к следующему представлению) выполняется до того, как getFeaturedPost
запускается, для l oop, где она печатает, в каком посте она находится c в данный момент. К концу, когда я отправляю данные в класс demosource с DemoSource.shared.add(urlString: newURL)
, значение newURL
равно nil, у меня есть консольный журнал, который показывает вам порядок операторов печати каждой функции:
Getting featured posts...
Done! The posts are: []
Getting idividual post data, there are 0 posts
Finished getting data, going to main screen. // This line should be printed last meaning this function is being executed too early
There's 2 posts avaliable.
Getting reference names for post: 1 // These two lines should be called before the line 'Finished getting data'
Getting reference names for post: 2