Der Klassiker:
Sie können nicht alles von einer Methode zurückgeben, die eine asynchrone Aufgabe
Sie benötigen, einfach
func checkBought(movieName : String, completion:(Bool) -> Void) {
boughtRef.observeEventType(.Value, withBlock: { (snap) in
if snap.value![movieName]! != nil {
if self.timestamp > snap.value![movieName]! as! Double {
//expire
print("expire")
completion(false)
} else {
//not expire
print("not expire")
completion(true)
}
} else {
//not bought yet
print("No movie")
completion(false)
}
})
}
Oder einfacher
func checkBought(movieName : String, completion:(Bool) -> Void) {
boughtRef.observeEventType(.Value, withBlock: { (snap) in
if let movieStamp = snap.value![movieName] as? Double where self.timestamp <= movieStamp {
//not expire
print("not expire")
completion(true)
} else {
// expire or not bought yet
print("expire or no movie")
completion(false)
}
})
}
einen Fertigstellungsblock enthält
A Nennen Sie es mit
checkBought("Foo") { flag in
print(flag)
}
Sie verwenden nicht die Blöcke, wie beabsichtigt, sollte die Return-Anweisung INSIDE Ihr Block sein. Wie es jetzt funktioniert ist, dass es false zurückgibt, bevor der Abschlussblock fertig ist. –