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.
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
.
Could anyone show how to do the configuration so that I can read/write to brevCustomer database?
EDIT
Please consider the following scenario:
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.
Customer registers with email and password, logs in, makes a booking, and data is saved in CustomerFireBase.
- Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added
- read from CustomerFireBase
- write to CustomerFireBase
- write to WorkerFirebase
- read from CustomerFireBase
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