Calling API from Event Functions

There are several event functions in Unity that will be called multiple times by the game engine in a short period of time.

  • The Update and LateUpdate functions are called per frame.
  • The FixedUpdate function is called at regular time intervals (0.01 sec by default).
  • The OnGUI function could be called multiple times per frame for rendering and handling GUI events.

You need to be careful when calling Kii Cloud API from these event functions. For example, the next sample code will create a few dozen of KiiObject per second.

void Update () {
  KiiObject obj = Kii.Bucket("high_score").NewKiiObject();
  obj ["score"] = 0;
  obj.Save((KiiObject savedObj, Exception e) => {
    if (e != null)
    {
      // Handle the error.
    }
  });
}

Please implement the logic safely, like the following sample code. In the sample code below, we are using a flag to prevent calling the same API repeatedly.

private bool isInitializedHighScore = false;
void Update () {
  if (!isInitializedHighScore) {
    isInitializedHighScore = true;
    KiiObject obj = Kii.Bucket("high_score").NewKiiObject();
    obj ["score"] = 0;
    obj.Save((KiiObject savedObj, Exception e) => {
      if (e != null)
      {
        // Handle the error.
      }
    });
  }
}