Unity/Database

05. Firebase Realtime Database - Delete

๐Ÿ”ท Delete Data

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

// Firebase ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ
using Firebase;
using Firebase.Database;

// Firebase์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
public class User
{
    public string userName;
    public int gold;

    // ์ƒ์„ฑ์ž
    public User(string _userName, int _gold)
    {
        this.userName = _userName;
        this.gold = _gold;
    }

}

public class FirebaseManager : MonoBehaviour
{
    // ์ €์žฅํ•  ๋ฐ์ดํ„ฐ UI
    public InputField userName;
    public InputField gold;

    // ๋กœ๋“œํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text allDataField;
    private bool isLoad = false;
    private string allDataStr = "";

    // ์„ ํƒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text selectDataField;
    private Query userNameQuery;

    // ์‚ญ์ œํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text deleteDataField;
    private bool isDelete = false;
    private bool IsCompleted = false;

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    private DatabaseReference reference;
    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ณ ์œ ์ฃผ์†Œ(URI)
    private readonly string uri = "https://fir-demo-3c2bb-default-rtdb.firebaseio.com/";

    void Awake()
    {
        // ์•ฑ ์†์„ฑ ์ƒ์„ฑ
        AppOptions options = new AppOptions();
        options.DatabaseUrl = new System.Uri(uri);
        // ์•ฑ์„ ์ƒ์„ฑ
        FirebaseApp app = FirebaseApp.Create(options);
    }

    void Start()
    {
        reference = FirebaseDatabase.DefaultInstance.RootReference; // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฃจํŠธ๋ฅผ ์ฐธ์กฐ

        allDataField.text = "Start";
    }

    // ๋ฐ์ดํ„ฐ ๋“ฑ๋ก
    public void InsertData()
    {
        // UI ์ž…๋ ฅ๊ฐ’
        string _userName = userName.text;
        int _gold = int.Parse(gold.text);

        // ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ
        User user = new User(_userName, _gold);

        // Json ํฌ๋งท์œผ๋กœ ํฌ๋งทํŒ…
        string json = JsonUtility.ToJson(user);
        // UserData ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•˜์œ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€
        reference.Child("UserData").Child(_userName).SetRawJsonValueAsync(json);
    }

    /*
        ๋ฐ์ดํ„ฐ ์กฐํšŒ
        - GetValueAsync ์‚ฌ์šฉ
        - ๋น„๋™๊ธฐ ๋ฐฉ์‹, ํ˜ธ์ถœ์™„๋ฃŒ๋œ ํ›„ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต
    */
    public void LoadAllData()
    {
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์‹คํŒจ
            {
                Debug.LogError("Failed load data!!!");
            }
            else if (task.IsCompleted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์„ฑ๊ณต
            {
                // ์Šค๋ƒ…์ƒท ์ƒ์„ฑ : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋‹จ์œ„
                DataSnapshot snapshot = task.Result;

                // ๋ฐ์ดํ„ฐ ๊ฑด์ˆ˜ ์ถœ๋ ฅ
                Debug.Log($"๋ฐ์ดํ„ฐ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜ : {snapshot.ChildrenCount}");
                // ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
                allDataStr = "";
                foreach (DataSnapshot data in snapshot.Children)
                {
                    // DataSnapshot.Value ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผ
                    // {ํ‚ค}:{๊ฐ’} IDictionary ์ž๋ฃŒํ˜• ์‚ฌ์šฉ (Key:Value)
                    IDictionary _data = (IDictionary)data.Value;
                    allDataStr = allDataStr + $"Name : {_data["userName"]} , Gold : {_data["gold"]}\n";
                }
                isLoad = true;
            }
        });
        StartCoroutine(UpdateAllData());
    }

    IEnumerator UpdateAllData()
    {
        yield return new WaitUntil(() => isLoad);
        isLoad = false;
        allDataField.text = allDataStr;
    }


    // ํ‚ค ๊ฐ’์œผ๋กœ ๊ฒ€์ƒ‰
    public void SelectData()
    {
        // ๊ฒ€์ƒ‰ํ•  UI ์ž…๋ ฅํ•ญ๋ชฉ
        string _userName = userName.text;
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ์ •๋ ฌ, ์ถ”์ถœ Query
        userNameQuery = reference.OrderByChild("userName").EqualTo(_userName);

        // ๊ฒ€์ƒ‰
        // ๊ฒ€์ƒ‰์ด ์™„๋ฃŒ๋˜๋ฉด or ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด
        // ValueChanged : ์ด๋ฒคํŠธ ํ•จ์ˆ˜
        userNameQuery.ValueChanged += OnDataLoaded;
    }

    // Query ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
    void OnDataLoaded(object sender, ValueChangedEventArgs args)
    {
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ ์Šค๋ƒ…์ƒท ์ •์˜
        DataSnapshot snapshot = args.Snapshot;

        foreach (var data in snapshot.Children)
        {
            IDictionary _data = (IDictionary)data.Value;
            selectDataField.text = $"Name : {_data["userName"]}, Gold : {_data["gold"]}";
        }
        userNameQuery.ValueChanged -= OnDataLoaded;
    }

    // ๋ฐ์ดํ„ฐ ์‚ญ์ œ
    public void DeleteData()
    {
        // ๊ฒ€์ƒ‰ํ•  UI ์ž…๋ ฅํ•ญ๋ชฉ
        string _userName = userName.text;
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData").Child(_userName);

        // ์‚ญ์ œ
        reference.RemoveValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์‹คํŒจ
            {
                IsCompleted = false;
                isLoad = true;
            }
            else if (task.IsCompleted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์„ฑ๊ณต
            {
                IsCompleted = true;
                isLoad = true;
            }
        });
        StartCoroutine(PrintDeleteData());
    }

    IEnumerator PrintDeleteData()
    {
        yield return new WaitUntil(() => isLoad);
        isLoad = false;

        if (IsCompleted)
        {
            deleteDataField.text = "deleted successfully.";
        }
        else
        {
            deleteDataField.text = "Error occurred while deleting.";
        }
    }


}

 

 

 

- ๋ฒ„ํŠผ ์ƒ์„ฑ : Button - Delete

- ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ : DeleteData()

 

 

 

- TMP_text ์ƒ์„ฑ : Text (TMP) - Delete Data

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

- Load > Ojui_2 ์ž…๋ ฅ ํ›„ Delete

- Data๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ง€์› ์กŒ๋‹ค๋Š” ๋ฌธ๊ตฌ

 

 

 

- ๋‹ค์‹œ Loadํ•ด๋ณด๋ฉด Ojui_2๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ์„ ํ™•์ธ

 

 

 

- Firebase ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€๋ด๋„ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

'Unity > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

00. Azure ์ฒดํ—˜๊ณ„์ • ํ™œ์„ฑํ™”  (0) 2021.08.20
06. Firebase Realtime Database - ์‹ฌํ™”  (1) 2021.08.15
04. Firebase Realtime Database - Select  (0) 2021.08.13
03. Firebase Realtime Database - Load  (0) 2021.08.12
02. Firebase Realtime Database - Insert  (0) 2021.08.11