Skip to content

Instantly share code, notes, and snippets.

@vinamelody
Created July 7, 2016 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinamelody/6fa518ad9345f9bb790390a28251bbf3 to your computer and use it in GitHub Desktop.
Save vinamelody/6fa518ad9345f9bb790390a28251bbf3 to your computer and use it in GitHub Desktop.
Swift: Query one last entry from Firebase, with completion handler
func getTheLastGuestKey(completionHandler: (Bool)-> ()) {
self.guestRef.queryOrderedByKey().queryLimitedToLast(1).observeSingleEventOfType(.Value, withBlock: {(snapshot) in
for guest in snapshot.children {
let guestSnapshot = snapshot.childSnapshotForPath(guest.key)
var lastGuestCheckintime: String! = ""
if let result_checkin = guestSnapshot.value?["checkintime"] as? NSNumber {
lastGuestCheckintime = "\(result_checkin)"
}
//let lastGuestCheckintime = guestSnapshot.value?["checkintime"] as! String
if (lastGuestCheckintime == "") {
self.shouldIncrementSequence = false
self.currentGuestKey = guest.key
print("last guest checkintime is blank")
}
else {
self.shouldIncrementSequence = true
print("last guest checkintime is NOT blank")
}
}
completionHandler(true)
}) { (error) in
print(error.localizedDescription)
completionHandler(false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment