如何从一个设置了 firebase 的 IOS 应用程序读取/写入另一个 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

查看:12
本文介绍了如何从一个设置了 firebase 的 IOS 应用程序读取/写入另一个 firebase 项目中包含的另一个 firebase 数据库?斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 GoogleService-Info.plist 连接到我的 IOS 应用程序的 Firebase 数据库.在 AppDelegate 中,我配置了 App FIRApp.configure().我可以读/写数据.

I have a Firebase database connected to my IOS app with the GoogleService-Info.plist. In AppDelegate I configured the App FIRApp.configure(). I could read/write data.

现在,从这个 IOS 应用程序中,我想访问另一个 FireBase 数据库 brevCustomer.出于某种原因,来自 viewDidLoadlet dbRef 在 Xcode 中有一个标志,说这个不可变值 dbRef 从未使用过"并且应用程序在第一次崩溃在 fun startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in.

Now, from within this IOS app, I would like to access another FireBase Database brevCustomer. For some reason let dbRef from viewDidLoad has a flag in Xcode saying this 'immutable value dbRef was never used' and the app crashes on the first line in fun startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in.

谁能展示如何进行配置以便我可以读/写 brevCustomer 数据库?

Could anyone show how to do the configuration so that I can read/write to brevCustomer database?

编辑

请考虑以下场景:

  • 我有两个 IOS 应用 CustomerWorker 以及两个名为 CustomerFireBaseWorkerFirebase 的 Firebase 项目我希望他们按以下方式工作.

  • I have two IOS apps Customer and Worker and two Firebase Projects named CustomerFireBase and WorkerFirebase and I would like them to work in the following way.

客户使用电子邮件和密码进行注册、登录、预订,并将数据保存在 CustomerFireBase 中.

Customer registers with email and password, logs in, makes a booking, and data is saved in CustomerFireBase.

  • 从 CustomerFireBase 读取
    • 写入 CustomerFireBase
    • 写入 WorkerFirebase

    我怎样才能做到这一点?基本上,我需要从一个以通常方式配置的 使用 Firebase,到另一个 Firebase 项目中包含的另一个 Firebase 数据库.

    How can I achieve this? Basically, I need to get read/write access from one IOS app configured in the usual way with Firebase, to another Firebase Database contained in another Firebase project.

    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).

    As you know, when a user registers with Firebase, a user account is created on the Firebase server and the user is provided a user id (uid).

    典型的设计模式是在 Firebase 中有一个/users 节点,用于存储有关用户的其他信息,例如昵称、地址或电话号码.

    A typical design pattern is to have a /users node in Firebase that stores other information about the user, such as a nickname, address or phone number.

    我们还可以利用/users 节点来指示它是哪种用户;Worker 或 Client,它们将与应用的其余部分和 Firebase 相关联,以便他们获得正确的数据.

    We can leverage that /users node to also indicate what kind of user it is; Worker or Client, which would tie into the rest of the app and Firebase so they get to the correct data.

    例如

    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 是客户.

    As you can see, John, George and Ringo are all workers and Paul is a client.

    当用户登录时,Firebase signIn 函数将返回包含 uid 的用户身份验证数据.

    When the user logs in, the Firebase signIn function will return the users auth data, which contains the 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数据是这样划分的

    If the app data is divided like this

    app
      client_data
         ...
      worker_data
         ...
    

    可以设置一个简单的规则来验证用户 user_type 是 worker_data 节点的 Worker 和 client_data 节点的 Client.这是一个伪示例,它将允许客户端用户仅访问 client_data 节点中的数据(概念)

    A simple rule could be set up that verifies the users user_type is Worker for the worker_data node and Client for the client_data node. Here's a pseudo example that will allow a Client user to only access the data in the client_data node (conceptual)

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

    这篇关于如何从一个设置了 firebase 的 IOS 应用程序读取/写入另一个 firebase 项目中包含的另一个 firebase 数据库?斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆