1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Special Features
  6. Repeater

Repeater

An asynchronous method works on its own. Once called, it “isolates” itself and carries out its activities, while other asynchronous methods are also running and the whole Unity project is running and doing other things.

If the Unity project just needs to run these processes in parallel without any checks, there is no problem. If, on the other hand, it is required to check what the asynchronous methods have done, it is necessary to “hook” a callback function that is executed upon completion of the operations and which, optionally, provides data.

However, the solution offered by a callback doesn’t cover the case where it is necessary to execute an asynchronous method multiple times, collecting the data generated by them and, in the end, having it all together when needed.

With synchronous methods, this type of problem doesn’t exist. It is enough to use the synchronous method in a for loop and collect the data. At the end of the for loop, all data will have been collected. This is possible because a synchronous method is executed only if the previous synchronous method was executed. On the other hand, with asynchronous methods this is not possible because an asynchronous method runs regardless of what the previous method is doing or did.

Loop exampleSync methods resultAsync methods result
for ( … ) {
…..method 1;
…..method 2;
…..method 3;
…..method 4;
}
result 1
result 2
result 3
result 4
result 3;
result 1;
result 4;

(result 2 is failed and there isn’t a result)

To resolve a situation like this, you can use the UniRESTClient.Async.Repeater functionality.


What it is

The Repeater is a functionality that works similarly to a for loop, but it has been created specifically for managing async methods.


How it works

A repeater takes an asynchronous method into processing and executes it several times. The number of executions depends on a range between two numbers exactly as it happens in a classic for loop.

At each execution, the progressive number between the two numbers of the range (the index) is made available to the asynchronous method which, if necessary, can use it.

At the end of the loop, a callback function will be called, confirming that all asynchronous methods have been executed.


Repeater C# development

To use this feature you have to use the UniRESTClient.Async.Repeater class.


1. Declare an instance

The first thing to do is to declare a new instance of the UniRESTClient.Async.Repeater class. This will initialize the Repeater engine.

var myLoop = new UniRESTClient.Async.Repeater();

2. Configure and start the loop

To configure and start a loop involving an async method, you have to use the Run() method. This method both configures and runs the loop.

myLoop.Run(from, to, callBack, onComplete);
fromintThe starting index.
tointThe ending index. This number is excluded. For example, if the range is 0 to 3, the indexes will be 0, 1, 2 and not 3.
Basically, it’s the equivalent of a for loop like this one:
for (var i = from; i < to; i++)
callBackAction<int>The function to call repeatedly. It must be defined as an async function and contains an int parameter for the generated indexes.
This value can be used if necessary or ignored.
onCompleteActionThe function to call when all the repeated methods have been executed.

Examples

The following examples cover some typical scenarios.


Simple example

In this example, I just need to delete the first four records from the test table. I know their ids are 0, 1, 2 and 3. No particular checks are required.

// Instance declaration.
var myLoop = new UniRESTClient.Async.Repeater();

// Configuration and running.
myLoop.Run(0, 4, async (int index) =>
    {
        await UniRESTClient.Async.Delete(
           API.test_test,
           new DB.Test { id = index },
           (bool ok) => { }
        );
    },
    () =>
    {
        Debug.Log("DONE!");
    }
);

Complex example

In this example, I have a list of IDs of users and I want to show a list of their usernames. I have an API that gives me a user’s details by his ID, so I use a Repeater to call the Read() method more times and I use the data variable to collect the results.

// Instance declaration.
var myLoop = new UniRESTClient.Async.Repeater();

// Configuration and running.
var ids = new int[] { 7, 9, 10, 11 };
var data = new List<DB.Users>();

myLoop.Run(0, ids.length, async (int index) =>
    {
        await UniRESTClient.Async.ReadOne(
           API.test_users,
           new DB.Users { id = ids[index] },
           (DB.Users result, bool hasData) => {
               data.Add(result);
           }
        );
    },
    () =>
    {
        foreach (var user in data) Debug.Log("User: " + d.username);
    }
);