Search Unity

Save/Load Async using Task

Discussion in 'Scripting' started by jister, Nov 14, 2019.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey, Trying to make async save and load functions.
    How can i test these to see if they actually ran async?
    Save:
    Code (CSharp):
    1. private static async void SerializeData(BinaryFormatter bf)
    2.     {
    3.         isDoneAsync = false;
    4.         await Task.Run(() =>
    5.         {
    6.             using (FileStream stream = File.Open(FILE_PATH, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    7.             {
    8.                 m_Data.isSaved = true;
    9.                 bf.Serialize(stream, m_Data);
    10.             }
    11.         });
    12.         Debugger.Log("Saved Async");
    13.         isDoneAsync = true;
    14.     }
    Load:
    Code (CSharp):
    1. private static async void DeserializeData(BinaryFormatter bf)
    2.     {
    3.         isDoneAsync = false;
    4.         await Task.Run(() =>
    5.         {
    6.             using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
    7.             {
    8.                 m_Data = bf.Deserialize(stream) as T;
    9.             }
    10.         });
    11.         Debugger.Log("Loaded Async");
    12.         isDoneAsync = true;
    13.     }
     
    elguncumayev likes this.
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Hmm apparently they don't work, visual studio just skips the calls...
    calling them like this:
    Code (CSharp):
    1. private static bool DeserializeData(BinaryFormatter bf, bool async = false)
    2.     {
    3.         if (!File.Exists(FILE_PATH))
    4.             return false;
    5.  
    6.         if(async)
    7.         {
    8.             DeserializeData(bf).Wait();
    9.             return isDoneAsync;
    10.         }
    11.         else
    12.         {
    13.             using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
    14.             {
    15.                 m_Data = bf.Deserialize(stream) as T;
    16.             }
    17.             return true;
    18.         }
    19.     }
     
    elguncumayev likes this.
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ok so just for others, i got it to work like this:
    To Call it:
    Code (CSharp):
    1. public static async Task<bool> SaveAsync()
    2.     {
    3.         if (saveData == null)
    4.             saveData = await SerializationManager<SaveData>.LoadAsync();
    5.         saveData.Data[DataIndex] = resortData;
    6.         return await SerializationManager<SaveData>.SaveAsync(saveData);
    7.     }
    in my SerializationManager:
    Code (CSharp):
    1. public static async Task<bool> SaveAsync(T data)
    2.     {
    3.         BinaryFormatter bf = new BinaryFormatter();
    4.         Debugger.Log("File saved at: " + FILE_PATH);
    5.         m_Data = data;
    6.         return await SerializeDataAsync(bf);
    7.     }
    8. private static async Task<bool> SerializeDataAsync(BinaryFormatter bf)
    9.     {
    10.         await Task.Run(() =>
    11.         {
    12.             using (FileStream stream = File.Open(FILE_PATH, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    13.             {
    14.                 m_Data.isSaved = true;
    15.                 bf.Serialize(stream, m_Data);
    16.             }
    17.         });
    18.         Debugger.Log("Saved Async");
    19.         return true;
    20.     }
    so any method that wants to call async saving or loading needs to have the async keyword, and to get the return type use the await keyword.
     
  4. rgalindox

    rgalindox

    Joined:
    Jan 30, 2014
    Posts:
    2
     
  5. rgalindox

    rgalindox

    Joined:
    Jan 30, 2014
    Posts:
    2
    Wow, it helped me a lot! Thanks man!
     
  6. Leasel

    Leasel

    Joined:
    Jun 21, 2018
    Posts:
    3
    Thank you