ios - Cannot signal semaphore with Firebase observeEventType - what gives? -
i using firebase read/write data remote server.
my objective write routine takes callback function. heres routine should accomplish:
- write data firebase (this works fine)
- observe changes on above written data
- if change occurs call callback success
- if timeout triggered, call callback failure
im unable figure out why steps 2-4 failing me. below snippet of code
here's whats going on in code:
- it sets gcd semaphore
- it writes data root firebase url - adds user called joe. works fine. can see data written vulcan
- it sets reference joe's node , watches events on childadded joe. here api reference
- upon getting event signals semaphore, should call callback message "success"
- instead see "timeout" message. strangely though observer print out newly added key:value pair, doubly confusing!
whats going on here? using semaphores incorrectly? or using firebase's api incorrectly?
typealias completionfoo = (gid:string) -> void
func setstateready(somestr: string, callfoo: completionfoo) { let semaphore = dispatch_semaphore_create(0) var myrootref = firebase(url:"https://xxxxxx.firebaseio.com/") var joe = ["state" : "ready", "msg" : somestr] var usersref = myrootref.childbyappendingpath("users") var users = ["joe": joe] usersref.setvalue(users) var joeref = firebase(url: "https://xxxxxx.firebaseio.com/users/joe/") // read data , react changes joeref.observeeventtype(.childadded, withblock: { snapshot in print(snapshot.key) print(snapshot.value) dispatch_semaphore_signal(semaphore) }) let timeout = dispatch_time(dispatch_time_now, int64(30 * double(nsec_per_sec))) if dispatch_semaphore_wait(semaphore, timeout) != 0 { joeref.removeallobservers() callfoo(gid: "timeout") } else { joeref.removeallobservers() callfoo(gid: "success") } } override func viewdidload() { super.viewdidload() setstateready("hi joe") { (gid) -> void in print(gid) }
Comments
Post a Comment