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:

  1. write data firebase (this works fine)
  2. observe changes on above written data
  3. if change occurs call callback success
  4. 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:

  1. it sets gcd semaphore
  2. it writes data root firebase url - adds user called joe. works fine. can see data written vulcan
  3. it sets reference joe's node , watches events on childadded joe. here api reference
  4. upon getting event signals semaphore, should call callback message "success"
  5. 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

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -