Firebase: What's the correct way to update values? - ios

If my database structure is like this:
and I have this:
ref = Database.database().reference().child(passUserID)
ref?.child("wins").updateChildValues("wins": numerator)
where numerator = (games played beforehand) + (games played after logging in), for the second statement it is giving me an error in the expression that I don't know how to fix.
Also if I try to do this instead:
ref?.child("wins").setValue(numerator)
it messes up my data bad and gives a bad instruction error.

The updateChildValues method takes a Dictionary. So you just need to call the method with a dictionary containing the key-value pairs you want to update, i.e.
ref = Database.database().reference().child(passUserID)
ref?.updateChildValues(["wins": numerator])
For more on how to read/write data to firebase realtime database, refer to: https://firebase.google.com/docs/database/ios/read-and-write

If you use .setValue triggers only in the beginning. Instead of .setValue use updateChildValues(.values/.childAdded/.childRemoved etc..) to update the values inside your tree. It will be executed whenever changes occurs to your node.
How to update particular value of child in Firebase DB

Related

How to update a single value of key in every child from firebase?

I am just stucked at this since last few hours and I have tried everything that is possible to update these values in firebase.
I want to update
is_read_p: "0"
to
is_read_p: "1"
for every record in the database.
So far, I have tried this code,
[[[[_mainRef child:#"messages"] child:[NSString stringWithFormat:#"359_361"]]childByAutoId] updateChildValues:#{#"is_read_c":#"0"}];
But, instead of updating, it adds three more child like this:
I know there must be a silly mistake or I might be missing something. Please help me finding that missing part. Thanks. :)
Each time you call childByAutoId, the client creates a reference to a new unique child node. Since you then call updateChildValues on that new location, you're creating a new child node, instead of updating an existing one.
Firebase doesn't support update queries. You'll need to execute the query, process each matching node, and update them individually.
Also see:
Swift Firebase: Update specific objects resulting from Firebase query

Append element to Firebase Array

How I could append an element to an array like that:
Using this code I'm overriding the old data:
let toUpdate = [book.id]
self.refUsers.child(localUser.key!).child("booksPurchased").setValue(toUpdate, withCompletionBlock: { (error, _) in
You could use this method: firebase.firestore.FieldValue.arrayUnion()
Example with angularfire2:
this.afs.collection('collection').doc(id).update( {
array: firebase.firestore.FieldValue.arrayUnion( 'newItem' )
});
For more information: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue#arrayunion
In this case, you will have to read the existing data, then write it back with the new value added. Arrays like this are not always the best way to store lists of data if you want to perform a lot of append operations. For that, you're better off pushing data into a location using childByAutoId.
Reading and writing lists
Append to a list of data
Use the childByAutoId method to append data to a list in multiuser applications. The childByAutoId method generates a unique key every time a new child is added to the specified Firebase reference. By using these auto-generated keys for each new element in the list, several clients can add children to the same location at the same time without write conflicts. The unique key generated by childByAutoId is based on a timestamp, so list items are automatically ordered chronologically.
You can use the reference to the new data returned by the childByAutoId method to get the value of the child's auto-generated key or set data for the child. Calling getKey on a childByAutoId reference returns the auto-generated key.
You can use these auto-generated keys to simplify flattening your data structure. For more information, see the data fan-out example.
-https://firebase.google.com/docs/database/ios/lists-of-data
You could set the values of the keys in the array to true, and then set the value directly in an update.
So if 'newId' is the new item to add, maybe something like:
const update = {
[`/users/${localUser.key}/booksPurchased/${newId}`]: true]
}
firebase.db.ref().update(update);
Firebase docs example of an update:
https://firebase.google.com/docs/database/web/read-and-write

Firebase observeEventType .childAdded doesn't work as promised

When I call this observe function from in my viewcontroller, the .childadded immediately returns a object that was already stored instead of has just bin added like .childadded would suspect.
func observe(callback: RiderVC){
let ref = DBProvider.Instance.dbRef.child("rideRequests")
ref.observe(DataEventType.childAdded) { (snapshot: DataSnapshot) in
if let data = snapshot.value as? NSDictionary {
let drive = cabRide(ritID: ritID, bestemming: bestemming,
vanafLocatie: vanaf, taxiID: taxiID, status: status)
print(drive)
callback.alertForARide(title: "Wilt u deze rit krijgen?", message: "Van: \(vanaf), Naar: \(bestemming)", ritID: ritID)
}
}
}
When I try this function with .childchanged, I only get a alert when it is changed like it suppose to do, but when doing .chiladded, it just gets all the requests out of the database and those requests were already there.
When I add a new request, it also gives an alert. So it works, but how can I get rid of the not added and already there requests?
Does anybody know this flaw?
This is working exactly as promised. From the documentation:
Retrieve lists of items or listen for additions to a list of items.
This event is triggered once for each existing child and then again
every time a new child is added to the specified path. The listener is
passed a snapshot containing the new child's data.
That might seem weird at first, but this is generally what most developers want, as it's basically a way of asking for all data from a particular branch in the database, even if new items get added to it in the future.
If you want it to work the way you're describing, where you're only getting new items in the database after your app has started up, you'll need to do a little bit of work yourself. First, you'll want to add timestamps to the objects you're adding to the database. Then you'll want to do some kind of call where you're asking to query your database by those timestamps. It'll probably look something like this:
myDatabaseRef.queryOrdered(byChild: "myTimestamp").queryStarting(atValue: <currentTimestamp>)
Good luck!

Access Parse Object ID right after instantiation

I'm creating two PFObjects at the same time that should reference each other's object IDs when they're saved. In the example below, the second object is supposed to save the first object's object ID in an array.
let objectForFirstClass = PFObject(className:"ClassOne")
let objectForSecondClass = PFObject(className: "ClassTwo")
objectForSecondClass.setObject([objectForFirstClass.objectId!], forKey: "classOneObjectArray")
The last line is causing the error because objectForFirstClass.objectId is nil. I'd assume this is because the object hasn't been saved yet. How can I fix this?
You want to save after creating the first object, and in the completion handler, create the second one with a reference to the first one.
You can use saveAllInBackground:block: for this.
Correct, the object id is assigned by the server when saved. I'd be tempted to write some cloud code to do what you want so you can send some details and the cloud code will create and connect the objects, then return both of them to you. You can of course do the same thing locally in your app, there's just more network comms.
You should also consider using pointers or relationships. These are better for querying, though the same save requirements apply before you can set the connections.

Parse.com findObjects() get data

I need to run a SYNCHRONOUS call to parse.com. This is what I got:
var query = PFQuery(className:"myClass")
query.whereKey("groupClassId", equalTo:self.currentGroupId)
query.selectKeys(["objectId", "firstName", "lastName"])
self.arrayCurrentData = query.findObjects() as Array<myData>
This return the correct number of rows from parse.com and fills up my local array. But how can I extract the data from the array? If I look at the array at runtime it shows that all the data I need is in 'serverData' in self.arrayCurrentData.
Normally if I loop an async(findObjectsInBackgroundWithBlock) filled array I would ask
self.arrayCurrentData[i].lastName
to get the lastName, but that is not the case in the sync array. There I can't ask directly for values (or so it seems).
Anyone who know what I am talking about and how to get data synchronous from parse.com?
Get the PFObject's attributes with valueForKey(). This is true whether or not the object was fetched synchronously. In other words...
self.arrayCurrentData[i].valueForKey("lastName")
EDIT - This approach generates a compiler message because you've typed the response as Array<myData>. But find returns PFObjects, so ...
self.arrayCurrentData = query.findObjects() as [PFObject]
... is the correct cast. I'm not a swift speaker, but the expression self.arrayCurrentData[i].lastName pleases the compiler because arrayCurrentData[i] is typed as myData. But this fails at run time because the real returned objects are PFObjects.
As an aside, I'd take a hard look at the rationale for fetching synchronously. I can't think of a case where its a good idea on the main thread. (off the main okay, but then you've already opted for asynch vs. the main, and the block-based methods provide a good way to encapsulate the post-fetch logic).

Resources