๐ท 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 |