簡體   English   中英

如何從設置有Firebase的一個IOS App讀取/寫入另一個Firebase項目中包含的另一個Firebase數據庫? 迅捷3

[英]How to read/write from one IOS App set up with firebase, to another firebase database contained in another firebase project? Swift 3

我有一個Firebase數據庫,它通過GoogleService-Info.plist連接到我的IOS應用程序。 在AppDelegate中,我配置了App FIRApp.configure()。 我可以讀/寫數據。

現在,從這個IOS應用程序中,我想訪問另一個FireBase數據庫brevCustomer 出於某種原因, let dbRef viewDidLoad中的let dbRef在Xcode中帶有一個標志,表明此“從未使用過不可變的值dbRef ”,並且應用程序在startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in )的第一行崩潰dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in

誰能顯示如何進行配置,以便可以讀寫brevCustomer數據庫?

編輯

請考慮以下情形:

  • 我有兩個IOS應用程序CustomerWorker,以及兩個名為CustomerFireBaseWorkerFirebase的 Firebase項目,我希望它們以以下方式工作。

  • 客戶使用電子郵件和密碼注冊,登錄,進行預訂,然后將數據保存在CustomerFireBase中。

  • 工作人員使用電子郵件和密碼注冊,日志是,觀察WorkerFirebase的值更改或添加子項
    • 從CustomerFireBase讀取
      • 寫入CustomerFireBase
      • 寫入WorkerFirebase

我該如何實現? 基本上,我需要從使用Firebase 按常規方式配置的一個IOS應用程序獲得對另一個Firebase項目中包含的另一個Firebase數據庫的讀寫訪問權限。

Class Claim {

  var dbRef:FIRDatabaseReference! //create a reference to Firebase database `brevCustomer`, not the one from .plist file

   override func viewDidLoad() {
       super.viewDidLoad()

     let app = FIRApp(named: "brevCustomer")
     let dbRef = FIRDatabase.database(app: app!).reference().child("Users")
     startObservingDB() // observe the database for value changes
    }

 func startObservingDB() {
   //it crashes on the line below
    dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in

        //iterate over each user node child
        for user_child in snapshot.children {
             print(user_child)} 

          }, withCancel: { (Error: Any) in
         print(Error)
       })
   } // end of startObservingDB()
}//end of Claim class



class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// Use Firebase library to configure APIs for the initial project with .plist file saved in Xcode
FIRApp.configure()


    /** 1. create a Firebase options object to hold the configuration data for the second Firebase Project */
    let secondaryOptions = FIROptions(googleAppID: "1:82424687545:ios:71df5d45218ad27",
                                      bundleID: "com.vivvdaplar.Brev",
                                      gcmSenderID: "8201647545",
                                      apiKey: "AIzaSyCNtyUf2T3UunH6-ci_WyvOqCl_RzXI",
                                      clientID: "8200687545-42vklp94reavi6li6bolhcraoofc6.apps.googleusercontent.com",
                                      trackingID: nil,
                                      androidClientID: nil,
                                      databaseURL: "https://brev-72e10.firebaseio.com",
                                      storageBucket: "com.vivvdaplar.Brev",
                                      deepLinkURLScheme: nil)

    // Configure the app
    FIRApp.configure(withName: "brevCustomer", options: secondaryOptions!) 
       return true
  }
} //end of AppDelegate

回答問題和評論。

如您所知,當用戶向Firebase注冊時,會在Firebase服務器上創建一個用戶帳戶,並為該用戶提供用戶ID(uid)。

一種典型的設計模式是在Firebase中擁有一個/ users節點,該節點存儲有關用戶的其他信息,例如昵稱,地址或電話號碼。

我們可以利用那個/ users節點來指出它是什么樣的用戶。 Worker或Client,將它們與應用程序和Firebase的其余部分綁定在一起,以便它們獲取正確的數據。

例如

users
  uid_0
    nickname: "John"
    user_type: "Worker"
  uid_1
    nickname: "Paul"
    user_type: "Client"
  uid_2
    nickname: "George"
    user_type: "Worker"
  uid_3
    nickname: "Ringo"
    user_type: "Worker"

如您所見,John,George和Ringo都是工人,Paul是客戶。

當用戶登錄時,Firebase登錄功能將返回用戶的身份驗證數據,其中包含uid。

    Auth.auth().signIn(withEmail: "paul@harddaysnight.com", password: "dog",
     completion: { (auth, error) in

        if error != nil {
            let err = error?.localizedDescription
            print(err!)
        } else {
            print(auth!.uid)
            //with the uid, we now lookup their user type from the
            //users node, which tells the app if they are a client
            //or worker
        }
    })

如果應用數據是這樣划分的

app
  client_data
     ...
  worker_data
     ...

可以設置一個簡單的規則,以驗證用戶user_type對於worker_data節點是Worker,對於Client_data節點是Client。 這是一個偽示例,該示例將允許Client用戶僅訪問client_data節點中的數據(概念性)

rules 
  client_data
     $user_id
        ".read": "auth != null && root.child(users)
                                      .child($user_id)
                                      .child("user_type") == 'Client'"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM