Unity/Database

06. Firebase Realtime Database - ์‹ฌํ™”

๐Ÿ”ท Firebase ์‹ฌํ™”

๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ์•ž์„œ ํฌ์ŠคํŒ…ํ•œ ์˜ˆ์ œ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ,

์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ์กฐ

 

 

 

๐Ÿ”ถ ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ

fir-demo.export.json
0.00MB

- ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ

 

 

 

๐Ÿ”ท Load data

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

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

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;

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    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;
    }

    public void LoadAllData()
    {
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("student");
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.LogError("Failed load data!!!");
            }
            else if (task.IsCompleted)
            {
                // ์Šค๋ƒ…์ƒท ์ƒ์„ฑ : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋‹จ์œ„
                DataSnapshot snapshot = task.Result;

                // ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
                allDataStr = "";
                // code๊นŒ์ง€ ์ ‘๊ทผ (0000,0001 ...)
                foreach (DataSnapshot data in snapshot.Children)
                {
                    IDictionary CodeData = (IDictionary)data.Value;
                    allDataStr += $"code : {CodeData["code"]}\n";

                    // info์— ์ ‘๊ทผ
                    DataSnapshot info = data.Child("info");
                    IDictionary infoData = (IDictionary)info.Value;
                    allDataStr += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";

                    // grade์— ์ ‘๊ทผ
                    DataSnapshot grade = data.Child("grade");
                    IDictionary gradeData = (IDictionary)grade.Value;
                    allDataStr += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";

                    allDataStr += "--------------------------------------\n\n";

                }
                isLoad = true;
            }
        });
        StartCoroutine(UpdateAllData());
    }

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

 

 

 

- Load ๋ฒ„ํŠผ ํด๋ฆญ

- ๋ฐ์ดํ„ฐ ์ž˜ ๋ถˆ๋Ÿฌ์˜ค๋Š”์ง€ ํ™•์ธ

 

 

 

๐Ÿ”ท Select data

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

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

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;

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    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;
    }

    public void LoadAllData()
    {
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("student");
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.LogError("Failed load data!!!");
            }
            else if (task.IsCompleted)
            {
                // ์Šค๋ƒ…์ƒท ์ƒ์„ฑ : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋‹จ์œ„
                DataSnapshot snapshot = task.Result;

                // ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
                allDataStr = "";
                // code๊นŒ์ง€ ์ ‘๊ทผ (0000,0001 ...)
                foreach (DataSnapshot data in snapshot.Children)
                {
                    IDictionary CodeData = (IDictionary)data.Value;
                    allDataStr += $"code : {CodeData["code"]}\n";

                    // info์— ์ ‘๊ทผ
                    DataSnapshot info = data.Child("info");
                    IDictionary infoData = (IDictionary)info.Value;
                    allDataStr += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";

                    // grade์— ์ ‘๊ทผ
                    DataSnapshot grade = data.Child("grade");
                    IDictionary gradeData = (IDictionary)grade.Value;
                    allDataStr += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";

                    allDataStr += "--------------------------------------\n\n";

                }
                isLoad = true;
            }
        });
        StartCoroutine(UpdateAllData());
    }

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


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

        // ๊ฒ€์ƒ‰
        userNameQuery.ValueChanged += OnDataLoaded;
    }

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

        // ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง„ ํ•™์ƒ์ด ์—†์Œ
        if (snapshot.ChildrenCount == 0)
        {
            selectDataField.text = "No students have that code.";
        }
        else
        {
            // code๊นŒ์ง€ ์ ‘๊ทผ  (0000, 0001 ...)
            foreach (var data in snapshot.Children)
            {
                IDictionary CodeData = (IDictionary)data.Value;
                selectDataField.text = $"code : {CodeData["code"]}\n";

                // info์— ์ ‘๊ทผ
                DataSnapshot info = data.Child("info");
                IDictionary infoData = (IDictionary)info.Value;
                selectDataField.text += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";

                // grade์— ์ ‘๊ทผ
                DataSnapshot grade = data.Child("grade");
                IDictionary gradeData = (IDictionary)grade.Value;
                selectDataField.text += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";
            }

        }
        userNameQuery.ValueChanged -= OnDataLoaded;
    }

}

 

 

 

- 0001 ์ž…๋ ฅ > Select ๋ฒ„ํŠผ ํด๋ฆญ

- ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง„ ํ•™์ƒ์„ ์ฐพ์•„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ

 

 

 

- ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ์œ„์™€ ๊ฐ™์ด ์ถœ๋ ฅ

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

01. Azure CosmosDB Settings  (0) 2021.08.21
00. Azure ์ฒดํ—˜๊ณ„์ • ํ™œ์„ฑํ™”  (0) 2021.08.20
05. Firebase Realtime Database - Delete  (0) 2021.08.14
04. Firebase Realtime Database - Select  (0) 2021.08.13
03. Firebase Realtime Database - Load  (0) 2021.08.12