Firebase asynchronous calls from Unity acting as if they are synchronous, stalling the application.

276 views
Skip to first unread message

bry...@moback.com

unread,
Feb 6, 2018, 8:49:31 PM2/6/18
to Firebase Google Group
Hello!

As y'all might remember, I'm using Unity 2017.3 with .NET 4.6 and Firebase Real-Time Database :)

However, I'm having an issue where calls to Firebase's asychronous API end up stalling both Unity's updates and rendering. This seems like surprising behavior to me since I wrote this program in an fully asynchronous way as to avoid such stalls :)

Specifically, the stalls seem to happen when I call SetRawJsonValueAsync. These are called from the Unity UI event handlers (onClick, for example). These stalls also happen when I use PutBytesAsync with Firebase Storage.

So far, I have not done anything in particular with Unity's synchronization context API, so unless your SynchronizationContextBehavior type is doing something when spanwed on the Firebase Service gameobject at run-time, it looks like we should be using Unity's default synchronization behavior. Another fact that might be germane is that I'm making these calls via the async keyword via UniRx - but that's just a relatively light-weight abstraction over Tasks, AFAIK.

Might I have forgotten to set something up, such as override the way Unity's synchronization context, in order to use Firebase asynchronously?

bry...@moback.com

unread,
Feb 7, 2018, 8:28:54 PM2/7/18
to Firebase Google Group
As more info, I'm looking at the Firebase Services object that is created by Firebase at runtime in the Unity editor, and where it has a readonly field for the SynchronizationContextBehavoir script, there's nothing in that field (None (MonoScript).

With ILSpy, I was able to peek inside the  Firebase.Database.DatabaseReference.SetRawJsonValueAsync function, and I saw that it schedules the Firebase call to its own internal Google.Sharpener scheduler. I'm presuming that that scheduler is being resolved from the main thread, and thus is why all my calls to Firebase are blocking.

Any ideas?

bry...@moback.com

unread,
Feb 9, 2018, 12:19:31 AM2/9/18
to Firebase Google Group
Is this something that isn't currently possible? If not, would we be able to leverage the new 'Job System' multi-threading feature in Unity 2018 in order to get rid of the stalls?

Thanks!

Stewart Miles

unread,
Feb 9, 2018, 12:46:11 PM2/9/18
to Firebase Google Group
Hi,

We end up creating separate threads for streaming and always call the application back on the main thread.
SetRawJsonValueAsync() can be pretty expensive as JSON parsing happens on the main thread and then we execute the set operation on a background thread.  I'm very surprised at the stalls in PutBytesAsync(), under the covers it's using a thread pool to schedule transfer operations which shouldn't be stalling the main thread.  Any chance you have a sample that could demonstrate the behavior you're seeing? e.g A hacked up variant of the samples on https://github.com/firebase/quickstart-unity ?  Also, do you only see this in .NET 4.x builds or also .NET 3.x builds?

Cheers,
Stewart



--
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-talk+unsubscribe@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/97332aff-4cc0-48f3-86e7-baa12b0072ca%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

bry...@moback.com

unread,
Feb 9, 2018, 2:11:11 PM2/9/18
to Firebase Google Group
Thank for getting back to me :)

I'm going to go ahead and build a reproduction case after I finish my current task, and will get it to you ASAP!

Thank you again, Stewart!
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.

bry...@moback.com

unread,
Feb 9, 2018, 5:16:52 PM2/9/18
to Firebase Google Group
Quick follow-up question to help me create the reproduction case -

Should I still expect for Firebase to utilize the non-main thread while in the Unity Editor? I know there are some potential issues with threading in the editor with certain tools.

Just wanted to see if that was applicable here.

Stewart Miles

unread,
Feb 9, 2018, 5:19:48 PM2/9/18
to Firebase Google Group
Yeah Firebase's behavior should be pretty much the same in the Unity Editor in the case of the RT DB and Storage plugins.

Cheers,
Stewart

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

bry...@moback.com

unread,
Feb 21, 2018, 10:45:34 AM2/21/18
to Firebase Google Group
Hi Sterwart!

As I experimented further, it was revealed that the performance issue was due to me observing far more changes on the data structure than I had intended. Once I fixed this, the stalls went away.

So everything seems to be working well on .NET 4.6!

Thank you again for your support even though I was making bad assumptions!

- Bryan
Reply all
Reply to author
Forward
0 new messages