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

2024-04-06

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

现在,在这个 IOS 应用程序中,我想访问另一个 FireBase 数据库brevCustomer。因为某些原因let dbRef from viewDidLoadXcode 中有一个标志说这个“不可变值”dbRef从未使用过”并且应用程序在 fun startObserving() 的第一行崩溃dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in.

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

EDIT

请考虑以下场景:

  • 我有两个 IOS 应用程序Customer and Worker和两个名为的 Firebase 项目客户火力基地 and 工人Firebase我希望他们按照以下方式工作。

  • 客户使用电子邮件和密码注册、登录、预订,数据保存在 CustomerFireBase 中。

  • Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added
    • read from CustomerFireBase
      • 写入客户 FireBase
      • 写入 WorkerFirebase

我怎样才能实现这个目标?基本上,我需要从配置的一个 IOS 应用程序获得读/写访问权限以通常的方式 https://firebase.google.com/docs/ios/setup使用 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 都是工人,Paul 是客户。

当用户登录时,Firebase 登录函数将返回用户身份验证数据,其中包含 uid。

    Auth.auth().signIn(withEmail: "[email protected] /cdn-cgi/l/email-protection", 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数据是这样划分的

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'"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从使用 Firebase 设置的一个 IOS 应用程序读取/写入另一个 Firebase 项目中包含的另一个 Firebase 数据库?雨燕3 的相关文章

  • 带有 Core Data 对象的动态 UITableView 高度

    过去几天我一直在试图解决一个谜团 即为什么我的批处理大小为 20 的 NSFetchedResultsController 总是在获取完成后立即错误 即加载到内存中 我的所有对象 从而导致请求需要约 20 秒 事实证明 这是因为在我的 he
  • 是什么导致了这个 iPhone 崩溃日志?

    我有点卡住了 需要解决这个问题 因为我的一个应用程序出现了随机崩溃 而这些崩溃并不总是能够重现 这是崩溃日志之一 Incident Identifier 59865612 9F00 44EA 9474 2BF607AD662E CrashR
  • iOS 10 的错误? NSDate 日本地区时间描述和 24 小时休息

    这似乎是 iOS 10 的一个错误 在 iOS 8 和 9 中都可以 NSDate date description 的小时描述是错误的 它附加了 24 小时描述和 12 小时描述 我没有使用 NSDateFormatter 只是默认设置
  • 如何在 Apple Watch Extension/App 和 iOS App 之间建立通信通道

    我正在探索 WatchKit SDK 当我有 WatchKit 应用程序时 是否可以在 WatchKit 应用程序上从 iPhone 应用程序设置值 例如文本 设置 我可以从 iPhone 应用程序调用 WatchKit 应用程序扩展中的函
  • 如何在 Swift 3 中解析 JSON 数组 [重复]

    这个问题在这里已经有答案了 我从 Socket 获取了一些我想访问的数据 但收到错误消息 指出每次都无法将 NSArray 转换为 NSDictionary struct SocketEventHandler let event Strin
  • 这个错误是无效上下文0x0吗?

    我在ViewDidLoad中编写了以下代码 Implement viewDidLoad to do additional setup after loading the view typically from a nib void view
  • TS2345:“字符串 | 类型的参数” null' 不可分配给'string | 类型的参数网址树'

    这个问题的标题是 Angular CLI 编译器抛出的消息 我的 app component ts 中有这个构造函数 export class AppComponent constructor private userService Use
  • 如何使用 Firebase 托管来托管映像?

    Issue 如何使用 Firebase 托管来托管图像文件 我目前正在 Firebase 中执行这些步骤应用程序内消息传递设置要在应用程序内向用户显示的消息 当要求提供图片网址对于 UI 建议使用的消息Firebase 托管 https f
  • UICollectionView 未出现

    我正在尝试设置UICollectionView 以编程方式在我的视图控制器中扩展UIViewController 由于某种原因 我的收藏视图根本没有显示 以下是我所拥有的 为什么没有出现 我将它连接到委托和数据源并将其添加为子视图self
  • 如何请求用户开启定位服务

    我需要我的应用程序来访问用户的当前位置 它在应用程序开始时检查用户是否已设置 如果没有 我需要应用程序显示提示以使其使用位置服务 就像警报视图一样 点击按钮 它应该会带您进入 iPhone 上的位置服务屏幕 您可以通过以下代码检查 loca
  • 在后台任务中安排通知

    我正在为 iOS 开发一个日历 闹钟应用程序 它与网络服务器同步 当在服务器上添加活动时 会发出推送通知 以便 iOS 客户端可以获取新数据 并根据需要更新和安排下一次警报的时间 本地通知 但这仅在应用程序在客户端打开时才有效 我希望客户端
  • iOS UIButton 带有圆角和背景 bug

    我发现圆形 UIButton 存在一个奇怪的问题 这是我创建此按钮的代码块 let roundedButton UIButton type System roundedButton frame CGRectMake 100 100 100
  • 使用 Google place API 从 lat long 获取附近的地点

    我正在使用 google place API 即 https maps googleapis com maps api place search json location 33 7167 73 0667 radius 500 type f
  • 线程 1:信号 SIGABRT - AppDelegate.h

    main m Journey Created by Julian Buscema on 2014 07 13 Copyright c 2014 Julian Buscema All rights reserved import
  • FireMonkey iOS RAD Studio XE2 - 在从 URL 加载的表单上显示图像

    是否可以将 TImage 放置在 iOS 的 FMX 表单上 并将图像 jpg 从 URL 加载到此 TImage 中以在 iOS 应用程序中显示 我尝试过但没有成功 任何正确方向的提示或指出都会受到赞赏 将 TButton TImageC
  • 应用程序传输安全已禁用,但仍然出现 SSL 握手错误

    我在通过 HTTPS SSL 连接到 API 时遇到问题 我已经使用下面的字典完全禁用了应用程序传输安全性 ATS 尽管 SSL 证书通过了 NSCURL 的所有测试
  • 具有 Firebase (FCM) 推送通知的 Node js

    我正在使用 Node js 开发 REST api 并且有一个休息端点来发送 firebase 推送通知 我的代码如下 const bodyParser require body parser var cors require cors v
  • 如何更改已上传的 Firebase 存储图像文件名?

    我需要更改已上传到 firebase 存储中的文件名 因为 在 firebase 存储中上传图像后 我将 url 保存在 firebase 数据库中的特定子 文件夹 下 但是 当我将图像移动到另一个子 文件夹 时 我需要根据新名称更改存储中
  • 自定义 MKAnnotationView - 如何捕获触摸而不忽略标注?

    我有一个自定义 MKAnnotationView 子类 它完全按照我想要的方式显示视图 在那个视图中 我有一个按钮 我想捕获按钮上的事件来执行操作 这很好用 但是 我不希望标注被忽略或消失 基本上 触摸标注中的按钮将开始播放声音 但我想保留
  • FCM onMessageReceived 应用程序运行时返回空白消息和标题

    正如您在标题中所写 当应用程序关闭时 它运行良好 并且onMessageReceived获取消息正文和标题 但如果应用程序处于前台模式 运行模式 则可以发送通知 但没有消息和标题 请问该怎么办 代码 Override public void

随机推荐