3

I am currently working with Firebase Firestore in an iOS application. There is a legal use case where users have to first accept a consent before I am allowed to upload their data to Firebase but I have the need to let users can already save data even if they have not yet agreed to the consent.

My question would be if it is possible to have two different instances of a Firestore database inside one iOS app. One instance that stores data only offline and does not sync and one which is syncing. Then I can decide at runtime where to store the data.

Is there any documentation or experience for this ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
grahan
  • 2,148
  • 5
  • 29
  • 43

2 Answers2

3

It is definitely possible to access multiple Firestore databases (or other Firebase resources) in a single application. But only one of them can be initialized from the GoogleService-Info.plist. The other one(s) you will have to initialize from within your code, based on the information in the secondary GoogleService-Info.plist.

The basic approach for this is to first create a FirebaseOptions object with the configuration data of the second project:

// Configure with manual options.
let secondaryOptions = FirebaseOptions(googleAppID: "1:27992087142:ios:2a4732a34787067a", gcmSenderID: "27992087142")
secondaryOptions.bundleID = "com.google.firebase.devrel.FiroptionConfiguration"
secondaryOptions.apiKey = "AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk"
secondaryOptions.clientID = "27992087142-ola6qe637ulk8780vl8mo5vogegkm23n.apps.googleusercontent.com"
secondaryOptions.databaseURL = "https://myproject.firebaseio.com"
secondaryOptions.storageBucket = "myproject.appspot.com"

And then use that to initialize a secondary App object to get the Firebase service(s) you need:

// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)

// Retrieve a previous created named app.
guard let secondary = FirebaseApp.app(name: "secondary")
  else { assert(false, "Could not retrieve secondary app") }

let secondaryDb = Firestore.firestore(app: secondary)

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your answer. But would it be possible to have two firebase app instances with the same configuration ? Because what I need is basically the primary firebase Firestore instance copied bust just locally not syncing – grahan Feb 11 '20 at 15:13
  • It's worth a shot with the approach above. – Frank van Puffelen Feb 11 '20 at 15:14
  • It seems that in general it would work, but it also seems that write transaction are queued when I disable the network for the second instance. Therefore I don't get any callbacks for my write transaction. If the write transaction wouldn't get queued it would work – grahan Feb 11 '20 at 15:28
  • Transactions require a connection to the server, otherwise they can't check aainst conflicting writes and would be pretty meaningless. Did you consider using a regular local database for storing the data before the user consent? – Frank van Puffelen Feb 11 '20 at 15:51
  • Yes makes totally sense that transactions won't work then. Using a local database is the approach I am implementing right now. More work but seems to be correct solution here. Not sure what do you with your answer. Basically our conversion lead to the fact that my approach will not work with two firebase instances but it basically answers my question. – grahan Feb 11 '20 at 16:28
0

We had a similar Situation with our app - A legal use case has come up that requires users to opt-in to storing data in the cloud. To that end, we need a storage solution that allows for some data to be stored locally only and some data that is synced to the cloud when the user opts-in. We thought of turning off Firestore network activity for offline data: Firestore.firestore().disableNetwork(completion: )

However, we contacted Google and their PM suggested that

Firestore is not a great offline-only database. It's designed for data that will eventually sync to the server. The offline features are meant to paper over temporary connectivity losses. Performance degrades as the number of pending write operations (aka offline writes) increases, and query performance on large offline datasets is pretty bad when compared to normal on-device mobile databases.

While they may be able to use the two-database workaround, I would generally recommend they just choose a totally different data store for their offline-only data. SQLite is the standard option, but there are many others.

Jijo John
  • 1,368
  • 2
  • 17
  • 31