ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Firebase]파이어베이스 Database 2. 데이터 저장
    개발/유니티 2018. 3. 24. 19:15

    메소드 사용 예제

    RunTransaction()은 공유자원을 관리하는 것과 유사한데, 복잡하므로 나중에...

    using System.Collections;
    using System.Collections.Generic;
    using Firebase;
    using Firebase.Database;
    using Firebase.Unity.Editor;
    using UnityEngine;

    public class DatabaseManager : MonoBehaviour {
    private static DatabaseManager Instance;
    public DatabaseReference reference;

    void Start() {
    // Set this before calling into Database
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://프로젝트 이름.firebaseio.com/");

    // Get the root reference location fo Database
    reference = FirebaseDatabase.DefaultInstance.RootReference;

    User user1 = new User("htw", "naver");
    User user2 = new User("ksj", "daum");
    string json = JsonUtility.ToJson(user1);
    string json2 = JsonUtility.ToJson(user2);

    // Using Json
    reference.Child("users").Child("user1").SetRawJsonValueAsync(json);
    // Using Path
    reference.Child("users").Child("user2").Child("name").SetValueAsync(user2.name);
    reference.Child("users").Child("user2").Child("email").SetValueAsync(user2.email);
    // Using Push
    string key = reference.Child("users").Push().Key;
    reference.Child("users").Child(key).Child("email").SetValueAsync("This is Push()");
    // Using Update
    Dictionary<string, object> update = user2.ToDictionary();
    reference.Child("users").Child("user3").SetRawJsonValueAsync(json);
    reference.Child("users").Child("user3").UpdateChildrenAsync(update);
    }
    }

    class User {
    public string name;
    public string email;

    public User(string name, string email) {
    this.name = name;
    this.email = email;
    }

    public Dictionary<string, object> ToDictionary() {
    Dictionary<string, object> result = new Dictionary<string, object>();
    result["name"] = name;
    result["email"] = email;

    return result;
    }
    }


    결과


    댓글

Designed by Tistory.