Firebase simultaneous realtime database update (Android)

137 views
Skip to first unread message

Uğur B

unread,
Jun 8, 2016, 8:53:21 PM6/8/16
to Firebase Google Group
I am developing an android app kinda random chat.

i have users structure in realtime database which is created whenever a new users login with username.

Database structure is as below.

users: {
  user1Id : {
    status: searching,
    username: guest
  }
 .
 .
 .


}

When user click search button the status changes to searching and i am getting all the users whose status are searching at the same time and randomly match 2 of them. Finally change the status of both users to matched and create a room for those users.

But I stucked at this point. For an instance, when 3 users click search their status turns to searching and i randomly match them. But when i read the status as searching, the other user may be matched already.  I can not assure multiple writes to database. 

I also used Google App Engine Servlet but no luck. 


I wonder is there any method to handle multiple updates of the same field in a queue or lock the field for a user who first accessed? 

Jacob Wenger

unread,
Jun 9, 2016, 1:47:23 PM6/9/16
to fireba...@googlegroups.com
I think you are looking for transactions. You can find docs for them here.

Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/626a75d7-d919-4851-9db0-805d16051427%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Uğur B

unread,
Jun 9, 2016, 2:50:10 PM6/9/16
to Firebase Google Group
thanks men, it seems what i m looking for. i will try it asap. i also wonder if it is possible with write permissions. i.e. auth users can only write if the status field is 'searching'. 
i m trying but i couldnt manage yet. but it must be possible i guess.

9 Haziran 2016 Perşembe 20:47:23 UTC+3 tarihinde Jacob Wenger yazdı:
Reply all
Reply to author
Forward
0 new messages