Bo Mortensen Bo Mortensen - 1 year ago 86
C# Question

Unity Firebase SDK SetRawJsonValue performance

I'm trying to get my head around an issue with my Unity 2D game where I'm using Firebase Realtime Database to store data like profiles, hits, misses, level progression etc.

For now, I'm working on the scene where users can create a new profile/new character. I've got the following code working:

var db = FirebaseDatabase.DefaultInstance.RootReference;

Profile profile = new Profile {
userId = GameManager.Instance.User.UserId,
name = "Bo Mortensen",
age = 34,
gender = "M"

string json = JsonUtility.ToJson (profile);

// The Push() is almost instant
var profileKey = db.Child ("profiles").Push ().Key;

// However, this takes around 3 seconds to return, even when specifying 1 as it's priority..
db.Child("profiles").Child(profileKey).SetRawJsonValueAsync (json, 1).ContinueWith (t => {
if(t.IsFaulted) {

if(t.IsCanceled) {

if(t.IsCompleted) {
SceneManager.LoadScene ("Main game scene");

So, creating the actual profile reference for the key is almost instant, where the
is really slow. Had a test where it took around 5 seconds before it loaded the next scene.

Is this normal for Firebase? I've got a feeling, that I shouldn't wait for the task to return anything and just let it sit in the background working while I load the next scene. This will be really fast, but what if it's canceled or faulted? I'd have no control of that.

Should say that I run my Unity game in debug mode and Firebase is currently the free tier for developing. Not sure if performance would increase with a paid tier of Firebase.

Any help/hint is greatly appreciated!

Thanks in advance.

Answer Source

firebaser here

There won't be a performance difference from upgrading your Firebase project to a paid plan. All projects run on the same infrastructure.

If this is the first time you interact with the database, you will lose most time establishing the (web socket) connection between client and server. After that performance is mainly dependent on the amount of data you send vs. the bandwidth you have available. These are the two most common causes and completely out of the control of your application code.

A quick way to check if you're losing time setting up the initial connection is to run the code that writes a few times after each other. If the next calls are significantly faster, you're paying the cost of building the initial connection. There is nothing you can do to prevent that cost, but you may be able to hide it by interacting with the database earlier in your app lifecycle, e.g. by reading/writing a dummy value on startup.

The main other place I could imagine time going would be in the parsing of the JSON. But your object structure seems quite small, so that seems unlikely here.

It's typically safe to transition before the task completes. The isCancelled() will only be true if the data is rejected by your server-side security rules. Since you control both the code and the security rules, that should normally not happen in production. But I'm not really certain when the client would return true for IsFaulted(). That call is not present in the underlying iOS and Android SDK's, so I don't know when the Unity client raises this condition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download