gpt4 book ai didi

ios - 如何从一个使用firebase设置的IOS应用程序读取/写入另一个firebase项目中包含的另一个firebase数据库? swift 3

转载 作者:行者123 更新时间:2023-12-01 19:52:48 24 4
gpt4 key购买 nike

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

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

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

编辑

请考虑以下场景:

  • 我有两个 IOS 应用程序 Customer 和 Worker 以及两个名为 CustomerFireBase 和 WorkerFirebase 的 Firebase 项目,我希望它们以下列方式工作。
  • 客户使用电子邮件和密码注册、登录、预订,并将数据保存在 CustomerFireBase 中。
  • Worker 使用电子邮件和密码注册,日志是,观察 WorkerFirebase 的值更改或添加的子项
  • 从 CustomerFireBase 读取
  • 写信给 CustomerFireBase
  • 写信给 WorkerFirebase

  • 我怎样才能做到这一点?基本上,我需要从配置的一个 IOS 应用程序获得读/写访问权限 in the usual way使用 Firebase 到另一个 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 都是 worker ,而 Paul 是客户。

    当用户登录时,Firebase signIn 函数将返回用户的身份验证数据,其中包含 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_data 节点中的数据(概念)
    rules 
    client_data
    $user_id
    ".read": "auth != null && root.child(users)
    .child($user_id)
    .child("user_type") == 'Client'"

    关于ios - 如何从一个使用firebase设置的IOS应用程序读取/写入另一个firebase项目中包含的另一个firebase数据库? swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44866220/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com