【发布时间】:2017-12-05 13:55:30
【问题描述】:
我有一个 Firebase 数据库,它使用 GoogleService-Info.plist 连接到我的 IOS 应用。在 AppDelegate 中,我配置了 App FIRApp.configure()。我可以读/写数据。
现在,我想通过这个 IOS 应用程序访问另一个 FireBase 数据库 brevCustomer。出于某种原因,来自viewDidLoad 的let dbRef 在Xcode 中有一个标志,表示“从未使用过不可变值dbRef”,并且应用程序在有趣的startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in 的第一行崩溃。
谁能展示如何进行配置,以便我可以读取/写入 brevCustomer 数据库?
编辑
请考虑以下场景:
我有两个 IOS 应用 Customer 和 Worker 以及两个名为 CustomerFireBase 和 WorkerFirebase 的 Firebase 项目我希望他们按照以下方式工作。
客户使用电子邮件和密码注册、登录、预订,并将数据保存在 CustomerFireBase 中。
- Worker 使用电子邮件和密码注册,日志是,观察 WorkerFirebase 的值更改或添加的子项
- 从 CustomerFireBase 读取
- 写信给 CustomerFireBase
- 写入 WorkerFirebase
- 从 CustomerFireBase 读取
我怎样才能做到这一点?基本上,我需要从一个使用 Firebase 配置为 in the usual way 的 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
【问题讨论】:
-
您“不能”这样做,而且可能不应该这样做。 .plist 文件将特定的 Firebase 数据库链接到应用程序。此外,当您使用 let dbRef = FIR... 时,它将 deRef var 定义为包含它的函数的本地变量,并且 Xcode 指示它未使用,因为它未在该函数中使用。
-
@Jay。感谢您的回答。什么是替代方案?如何从我已经使用默认 Firebase SDK 的 IOS 应用程序中读取/写入另一个数据库?我看到了这个答案,这就是为什么我认为它应该可以工作stackoverflow.com/questions/37634767
-
我将“不能”放在引号中,因为这是可能的,但是,在这样做之前需要考虑很多事情。如果您可以详细说明您的用例,我们或许可以提供答案或最佳实践。请参阅此处的说明Configure Multiple Projects。
-
@Jay 我现在已经编辑了我的问题,请查看
-
员工和客户之间的关系似乎非常紧密,并且对于两个应用程序,您有大量冗余代码和重叠数据。为什么不把它放在一个应用程序中?当用户登录时,如果他们是客户,则访问 Firebase 客户相关数据,如果他们是工作人员,则访问 Firebase 工作人员数据。如果您想要视觉分离,请将它们保留在 /app/worker_data 和 /app/customer_data 等两个节点中。如果您这样做了,那么设置 Firebase 规则以确保用户无法访问其他节点中的数据将是一件轻而易举的事。
标签: ios firebase swift3 firebase-realtime-database firebase-authentication