Search Unity

Best way to do error handling? (and others questions)

Discussion in 'Addressables' started by bitinn, Mar 29, 2019.

  1. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Hi all,

    IAsyncOperation has all these properties, which one is actually for checking the results are correct and complete?

    - IsDone?
    - IsValid?
    - Status?
    - Result == null?

    More importantly, say I load multiple assets via LoadAssets<T> API, which one tells me the loading is actually done, and without errors?

    Furthermore, I have used suggestion from this thread to make IAsyncOperation<T> await-able, because I don't know which property actually guarantee result is correct, I make it returns the "IAsyncOperation<T>" instead of "Result", does it sound like a better approach?

    Code (CSharp):
    1.         public readonly struct AsyncOperationAwaiter<T> : INotifyCompletion {
    2.             readonly IAsyncOperation<T> _Operation;
    3.  
    4.             public AsyncOperationAwaiter(IAsyncOperation<T> op) {
    5.                 _Operation = op;
    6.             }
    7.  
    8.             public bool IsCompleted => _Operation.Status != AsyncOperationStatus.None;
    9.  
    10.             public void OnCompleted (Action callback) => _Operation.Completed += _ => callback?.Invoke();
    11.  
    12.             public IAsyncOperation<T> GetResult() {
    13.                 return _Operation;
    14.             }
    15.         }
    Thx in advanced!
     
  2. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    A quick test show:

    - If I do this: LoadAssets<T>("a-non-exist-label", callback)
    - Status = Failed
    - IsDone = true
    - IsValid = true.
    - Callback doesn't fire.
    - And these errors are thrown:

    Screen Shot 2019-03-29 at 18.04.23.png

    So Status is the way to check it, but is it the only way? Why doesn't the doc mention this?
     
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Another question:

    When using label as key with LoadAssets to load many items, how do I figure out the address (or unique name) associated each item?

    My test shows:

    - IAsyncOperation.Key returns the label used, not the address associated with the asset.
    - IAsyncOperation.Context returns the actual location/path of the item, not the address associated with the asset.

    Imagine this use case:

    - We want an unique identifier associates with each prefab
    - And in our case, for memory and performance, we used a uint
    - Because batch LoadAssets doesn't expose the address of each item
    - My only way to store this id, is to store it as a component variable on the prefabs, and read it on load

    Is this an intended design of Addressables?
     
  4. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @bitinn you will have to write that yourself - as it seemed you haved figured out already :)
     
  5. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Haha, I am not sure about the LoadAssets not returning addresses though, I don't think returning the same label is very useful.

    (also the package mixed naming convention, in API it's called Key, but in doc and UI they are called Addresses, in implementation they are actually Locations. not sure which is the right word...)
     
    MNNoxMortem likes this.