close
Async mechanism uses firebase in Unity. Dropdown control will appear an error when I add an option and call dropDownListCreatedStore.RefreshShownValue(); as below:
And then I modify the positions of await and async. The problem is solved. The Code as below:
public class SellerSetting : MonoBehaviour { public Dropdown dropDownListCreatedStore; public InputField InputFieldStoreName; public Dropdown dropDownStoreType; private string storeName; static DatabaseReference reference2; private async Task Start() { reference2 = FirebaseDatabase.GetInstance(Constant.FirebaseDatabaseURL).RootReference.Child("users").Child("stores"); reference2.ChildAdded += HandleChildAdded; reference2.ChildChanged += HandleChildChanged; reference2.ChildRemoved += HandleChildRemoved; reference2.ChildMoved += HandleChildMoved; storeName = "A Store"; var task = await Getdata(); DataSnapshot snapshot = task; Debug.Log($"count = {snapshot.ChildrenCount}"); foreach (DataSnapshot child in snapshot.Children) { Debug.Log("Received value for leader: " + child.Key); await GetType(child); } dropDownListCreatedStore.RefreshShownValue(); } static async Task Getdata() { DataSnapshot Task1 = null; await reference2.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { Debug.Log(task.Exception); } else if (task.IsCompleted) { } Task1 = task.Result; }); return Task1; } private async Task GetType(DataSnapshot child) { DataSnapshot Task1 = null; await child.Reference.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { Debug.Log(task.Exception); } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; Debug.Log($"count2 = {snapshot.ChildrenCount}"); foreach (DataSnapshot child in snapshot.Children) { //Debug.Log("Received value for store type: " + child.Key); //Debug.Log("Received value for store type: " + child.GetRawJsonValue()); StoreItem item = JsonUtility.FromJson(child.GetRawJsonValue()); //Debug.Log($" F Store Name = {item.StoreName}"); //Debug.Log($" F Store Type = {item.StoreType}"); //Debug.Log($" F Store UserId = {item.UserId}"); if (child.Key == storeName) { if (dropDownListCreatedStore.options.Count == 0) { dropDownListCreatedStore.options.Add(new Dropdown.OptionData(item.StoreType)); //dropDownListCreatedStore.RefreshShownValue(); } else { int diffCount = 0; for (int index = 0; index < dropDownListCreatedStore.options.Count; index++) { if (dropDownListCreatedStore.options[index].text != item.StoreType) { diffCount++; } } if (diffCount == dropDownListCreatedStore.options.Count) { dropDownListCreatedStore.options.Add(new Dropdown.OptionData(item.StoreType)); //dropDownListCreatedStore.RefreshShownValue(); } } } } } Task1 = task.Result; }); return Task1; }
全站熱搜