1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Database Operations
  6. Read / ReadOne

Read / ReadOne

Read()

The Read() method calls an API asking to perform a reading operation on the Database table the API works with.
Result
The UniREST Server application will reply with the reading result (it depends on the configuration of the API read operation), so this method will return an array of records. Each record is an instance of DB.table class.

_ = #UniRESTClient.Async#.$Read$<£TABLE£>(%api, tableData, callBack%);

TABLE|DB.table class
The table class, under the DB class, containing the data structure.
__
api|string
The API address, under the API class.
__
tableData|object
Optional values to send to the server. It must be an instance of the table class in use.
__
callBack|Action<TABLE[]>
The function to call on operation completed.

TABLE[]
The array of found records. A 0 length array is returned if nothing is found or a system error occurred.

Example

“Weapon” Database table
This table contains all the weapons of a player. The player is identified by its id (player_id).

“weapons/list” API
This API returns the list of all the weapons of a specific player. The player_id is set as “Key”; it will be used as a value to find all the table records with that value. Moreover, the list will be ordered by name.

DB class (UniRESTClientConfig.cs)
Under the DB class, the Weapon class replicates the “Weapon” Database table structure.

[System.Serializable]
public class Weapon
{
     public int id = 0;
     public int player_id = 0;
     public string name = "";
     public int attack = 0;
}

API class (UniRESTClientConfig.cs)
Under the API class, the weapons_list property contains the URL to the weapons/list API.

public static class API
{
     public static string weapons_list = "weapons/list";
}

Read() method
In this example, the Read() method calls the API.weapons_list sending the user ID. The API will use that ID to find the records where player_id is equal to ID. If nothing is found, the result will have 0 length, otherwise, it will be an array of DB.Weapon records.

var result = UniRESTClient.Async.Read<DB.Weapon>(
     API.weapons_list, 
     new DB.Weapon { player_id = UniRESTClient.UserID },
     (DB.Weapon[] results) =>
     {
          if (results.length > 0)
          {
                var r = results[0];
                Debug.Log("First weapon name:" + r.name);
          }
          else
          {
                Debug.LogWarning("No results!");
          }
     }
);

ReadOne()

The ReadOne() method calls an API asking to perform a reading operation on the Database table the API works with. The API must have the “One record only” option checked.
Result
The UniREST Server application will reply with the reading result that will be one record only. The returned data is an instance of DB.table class.

_ = #UniRESTClient.Async#.$ReadOne$<£TABLE£>(%api, tableData, callBack%);

TABLE|DB.table class
The table class, under the DB class, containing the data structure.
__
api|string
The API address, under the API class.
__
tableData|object
Optional values to send to the server. It must be an instance of the table class in use.
__
callBack|Action<TABLE[], bool>
The function to call on operation completed.

TABLE
The found, single record.
__
bool
true if the reading operation returned some value, false if nothing is found or a system error occurred.

Example

“Players” Database table
In this table, each record is a single player with its characteristics.

“player/data” API
This API returns the player’s data from the “Player” table. Since this data is in just one record, the “One record only” option is checked and the record ID is used to find that record.

ReadOne() method
In this example, the ReadOne() method calls the API.player_data sending the ID of the user. The Server will use the ID to find the player’s record and will return the found record (or null if nothing is found).

var result = UniRESTClient.Async.ReadOne<DB.Players>(
     API.player_data, 
     new DB.Players { id = UniRESTClient.UserID },
     (DB.Players result, bool ok) =>
     {
          if (ok)
          {
                Debug.Log("This player name:" + result.name);
          }
          else
          {
                Debug.LogWarning("No result!");
          }
     }
);

If the debugMode is active (see the Debug Mode chapter for info), both methods will show useful log messages if something goes wrong.