Async Operations
Most functions in the API will return a VyTask object that may or may not contain data of a specific type (VyTask
& VyTask<T>
). These are built on top of the .NET Tasks but offer you a promise-like way of handling the async operations. VyTasks can be awaited, or you can set callbacks that fire on various events.
VyTask
A VyTask will notify you the moment an async procedure is completed. It contains some basic properties that give you more information (VyTaskResult
):
Value | Type | Description |
---|---|---|
Success | Boolean | Whether the task was completed successfully or not |
Cancelled | Boolean | Whether the task was canceled |
Exception | Expection | In case of unsuccessful or canceled, this field will give you more info about the reason |
Data | (T) | The data that should be returned once the task is finished |
Future releases of the SDK will further extend the VyTaskResult
. (This could include additional API response information like Pagination)
VyTask Usage
The following snippets will show you 4 ways of retrieving all the wallets.
VyTaskResult<VyWalletDto[]> walletResult = await VenlyAPI.Wallet.GetWallets();
if (walletResult.Success) Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)");
else Debug.LogException(walletResult.Exception);
VyWalletDto[] wallets = await VenlyAPI.Wallet.GetWallets().AwaitResult();
Debug.Log($"YAY - retrieved {wallets.Length} wallet(s)");
//OnComplete
VenlyAPI.Wallet.GetWallets()
.OnComplete(result =>
{
if(result.Success) Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)");
else Debug.LogException(result.Exception);
});
//OnSuccess/OnFail
VenlyAPI.Wallet.GetWallets()
.OnSuccess(wallets => Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)"))
.OnFail(Debug.LogException);
VyTask Creation
Next to usage in the SDK itself, you can of course also create and return your own VyTasks. The moment you define a new VyTask
, a VyTaskNotifier
object is created. This notifier object is used to signal the state of the task.
The following snippet shows you how to create a VyTask
, and use its corresponding VyTaskNotifier
to signal for completion.
public void ExecuteTask()
{
CancelIfNoWallets() //CancelIfNoWallets code below
.OnSuccess(() => Debug.Log("Wallets Available"))
.OnCancel(() => Debug.Log("No Wallets, abort!"))
.OnFail(Debug.LogException)
.Finally(() => Debug.Log("Whatever happens, execute this after completion..."));
}
//Simple Async Function that Cancels if there are no wallets present
public VyTask CancelIfNoWallets()
{
//Non Generic VyTask
VyTaskNotifier taskNotifier = VyTask.Create(); //or VyTask<T>.Create()
//Async Task
VenlyAPI.Wallet.GetWallets()
.OnSuccess(wallets =>
{
if(wallets.Length > 0) taskNotifier.NotifySuccess(); //Wallets = SUCCESS
else taskNotifier.NotifyCancel(); //No Wallets = CANCEL
})
.OnFail(ex =>
{
taskNotifier.NotifyFail(ex); //Something went wrong... = EXCEPTION
});
return taskNotifier.Task;
}
//using ASYNC/AWAIT
public async void ExecuteTaskAsync()
{
var result = await CancelIfNoWallets(); //CancelIfNoWallets code below
if (result.Success) Debug.Log("Wallets Available");
else if (result.Cancelled) Debug.Log("No Wallets, abort!");
else Debug.LogException(result.Exception);
}
//Simple Async Function that Cancels if there are no wallets present
public VyTask CancelIfNoWallets()
{
//Non Generic VyTask
VyTaskNotifier taskNotifier = VyTask.Create(); //or VyTask<T>.Create()
//Async Task
VenlyAPI.Wallet.GetWallets()
.OnSuccess(wallets =>
{
if(wallets.Length > 0) taskNotifier.NotifySuccess(); //Wallets = SUCCESS
else taskNotifier.NotifyCancel(); //No Wallets = CANCEL
})
.OnFail(ex =>
{
taskNotifier.NotifyFail(ex); //Something went wrong... = EXCEPTION
});
return taskNotifier.Task;
}
Updated 9 months ago