I am developing a cross platform app for Android and IOS using Flutter.
I am trying to read a certain record in my firebase database. Specifically, I am trying to get a record in my "Managed orders" table that contains a specific orderID. This is achieved by specifying the orderID in the path of the .get() Firebase command. I then store this specific record as a map.
This works perfectly on Android. A single record is read correctly, and is stored as a map. However, when I run the EXACT same code on IOS, it simply fetches the ENTIRE "Managed orders" table, instead of just one record that matches a specific orderID.
Any help would me much appreciated!
var details = await _dbRef.child('ManagedOrders/$orderID').get();
if (details.exists) {
var ordersMap = details.value as Map;
}
Related
I'm not sure if the method I'm currently using when I just want a Firebase auto-generated ID in my Swift app is correct. It feels a bit clunky, and I was hoping there was a better method.
These IDs are just for UUID consistency in my application and a few other use cases, so I don't want to perform a read/write operation in Firestore.
Please note, I'm not looking at any UUID generation method (e.g. UUID().uuidString) nor do I want to use a 3rd party lib which claims to use the same mechanism to generate UUIDs. I'm trying to specifically and explicitly generate the same IDs as Firebase, using Firebase, without incurring a read/write cost.
func generateId() -> String {
return Firestore.firestore().collection("unused").document().documentID
}
Ignoring the namespacing of calling Firestore.firestore() (only doing that because of where the code is called from), this feels very strange, to create a document ref to a collection that doesn't exist (and will never exist), just to extract the document ID from the document it creates.
I would have thought/hoped there was some sort of Firestore.generateId() static method or utility somewhere, and maybe there is which just isn't showing up on my auto-complete...
What you're doing now is your easiest option. There is no public API for generating those random document ID strings.
If you want to make your own function, you can simply copy what Firestore does, since the client libraries are open source.
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 ?
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:
The Firebase documentation on using multiple projects in your application.
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.
When working with a database on Firebase, is it possible from within an iOS app to check the date(& time) a document was last updated using some standard API? I mean without implementing my own system to know when it was last time touched.
It would be convenient if there was a field "lastUpdate" time-stamp for instance.
Neither the Firebase Realtime Database nor Cloud Firestore automatically adds a timestamp field to the data for writes.
If you want such a field, you will have to add it yourself, either from the client, or from Cloud Functions.
For a simple example of the latter, which tracks when a node in the database was last modified, see this folder in the functions-samples repo. The main code:
exports.touch = functions.database.ref('/chat/{message}').onWrite(
(change, context) => admin.database().ref('/lastmodified').set(context.timestamp));
I have an app with tutorials. I also have a tab where users can suggest tutorials. Right now, the app loads a table view with an JSON array taken from my website. Every tutorial object in the array looks like this:
{"string" : "Tutorial Name", "value":1, "devices":["deviceid1","deviceid2"]}
The string is the name of the tutorial they want to see. The value is how many people have voted for it. The devices is an array of strings where I would save the device ID of each user who votes for that tutorial. This is to make sure they do not vote more than once.
Now, what I want the app to do is to change the value key to add one and to add its deviceID to the array.
How do I do that in xcode? I am assuming I might have problems with 2 users trying to change the values at the same time. Should I maybe create a small API for this? Possible create a file that would get the values for me and change them as well?
If so, how do I go about doing this?
Thanks!
If you are making changes to the JSON from your app, you are only changing the values locally. This means that the changes won't be updated on the devices of other users who are accessing the app. I think the best way to do this would be to create a small API.
1) You will need to setup a MySQL database that has keys for "string" and "value".
2) Create a PHP script that takes in a string and then adds one to the value field of the database row. This script will be called when a user "votes" for a tutorial.
3) Create a PHP script that queries the data from the database and converts it to JSON format. When loading the app, this script would be called and your app would parse the JSON data and display it into a tableview with the updated values.
Hope this sets you off in the right direction!
Am new to ios application development, but not to application development. Previously i created an android application, a part of it contains an array of elements(say list heading) and its corresponding description (in another array) i didn't have any trouble in retrieving those array elements and using them.
Requirements:
my project is not very complex to use SQLite datastores or Core Data.
i could have the data inside the application variable itself and use them as they are only used at the particular page.
i require a very simple solution to convert my existing data as said arrays.
Problem:
Now i would like to perform the same for ios application too. so i dont want to create the same array like structures redundantly.
Assumptions:
Am assuming tat i could write a script to generate the arrays into an XML File and parse them and use them in my application.
So i need some suggestions and directions on my assumptions.