I am an iOS app developer. Right now, I need my app to update his/her scores in the database (Firestore) when a user tabs a button.
I only know that it is possible to add new documents to the database according to the official doc: https://firebase.google.com/docs/firestore/quickstart#swift. But can I simply update one key-value pair within a document at a time?
For example, I have to add 10 points to the user with ID: 1234 if he/she clicks the correct button. So first of all, I need to read the data according to the unique ID, and catch his/her current points. Then put back the points plus 10.
It looks like Firestorage only allows users to upload an entire document once a time.
Swift code snippet is appreciated!
You can update a single or multiple fields in a document with the updateData call.
To atomically increment a numeric value can you can use the FieldValue.increment operation. From that last link comes this example:
washingtonRef.updateData([
"population": FieldValue.increment(Int64(50))
])
This code increments the population field in the document by 50, and leave other fields in the document unmodified.
Related
Suppose I have a collection called 'books' and a page called "all books" in my app. Now let's say I want a little message to pop up in the top right that says "This list is outdated" every time there is an addition, deletion, or change in my 'books' collection. Is there any way to achieve this without having the listener send back all the documents in the 'books' collection each time there is a change? I only want to be notified when the data is outdated and nothing else.
If you know how to achieve this please let me know. I'm good with any language but a solution written in Swift will be preferred.
Thanks in advance!
Just add a field updatedon to your books document and set it to the current time when doing any write (on delete also then, you will have to find a way to hide deleted books).
And set a listener like this:
colref.where("updatedon", ">", new Date())
.orderBy("updatedon","desc")
.limit(1)
This way you are only billed 1 read every time.
It's not possible. A listener on a document or collection always receives the entire document that changed.
If you want a "lite" notification, you could try to use FCM to send a message to the client when a document of interest has changed, but then you'll have to keep a record on the backend of every interesting document for every user, check that list with every change using a Cloud Functions trigger, then notify each client of each interesting change. This is not at all the same as a listener, and would be a lot of work.
Right now, I'm generating unique IDs with childByAutoId(), but was wondering if there was a way to do this only generating numbers, no letters or other characters?
The reason is I need to be able to automatically send this key through imessage (part of how I send invites) and when it contains letters you're not able to automatically select and copy the key without copying the entire text message. With numbers only the key will be underlined and selectable.
Is there a way to either generate an ID with numbers only, or to selectively underline part of an iMessage with MFMessage in Swift?
Thanks!
I've need a similar option. When i create a new user; it will have a numberID which will be unique. I've tried .push() method which is for android, creates a uniqueID but with characters(letters) included. What i've done was something like this;
When i add a new user, i increment a value from different branch which is User2Key in this situation. And gave the value as key(parent) of newly added user.
When i delete or update a user, User2Key will be the same. If i add a new user then it will be incremented so every user will have uniqueID.
You can use a similar approach.
Hope this helps! Cheers!
I recently am given a task by upper management to implement a mass upload coupon task. The goal is to allow users to upload an list of coupon codes (excel or csv), and subsequently giving these coupons codes attributes such as start date, expiry, quantity and so on.
I have already implemented a form to input these attributes. The current implementation method I am doing is as follows:
1.I upload a list of coupon codes (eg: 333245454dfee) and I do not write them directly to the database, instead I converted them to string and display on a page for the user to view. (done)
2.from that view page, there is a form with all the attributes. The user can then input these attributes. (done)
3.The user can create all of these coupons codes with the attributes set.
However, I am stuck now because I am unsure of how to mass create multiple codes and attach all the attributes to them. Right now, I can only create one coupon at a time.
So, to summarize, I would like to ask is it possible to
have a field that contains all the codes I have uploaded
I have others fields for different attributes
how to create all the codes I have uploaded as separate models.
I do not need the codes, I would like to hear what approach there are. I am thinking of creating a variable to store these coupons codes first, then loop them. But I have no idea how to do all of that by pressing one single button.
Thanks in advance.
Hmm, how I would approach this problem would be to have the user upload an csv file with 1 column and multiple rows of coupon. Using carrierwave to upload the file and parse the csv file. After parsing it, you should have an array of coupon and you can just input them into the database.
A good reference for your problem would be http://railscasts.com/episodes/396-importing-csv-and-excel
In my app I sometimes need to import some CSV data and I wanna get a view that is shown before save imported data to provide user possibility to check that data is correct. How to achieve it in Rails?
in the first form field, have a hidden field name "confirm" with value 0.
In the rails controller, if the confirm is 0 you just populate with the csv data, show the form again with confirm = 1
if the confirm is 1, then it means the csv was seen by the user, so you can save it.
bonus points-
Instead of showing the csv, make a textfield out of each csv cell- the user will be able to update the fields, not just check if the data is correct.
I'm looking to create a report with similar that has a checkbox next to each row. Each row has some fields that are editable (such as comments). What I'd like is to give the user the ability to check off which rows he/she would like to update by selecting the checkbox next to that row. Then I'd have a save button at the bottom of the form that only updates the rows that have a check box active next to it.
I'm pretty new to rails and web programming in general so any advice/direction you might be able to give me should prove helpful.
A popular way to achieve this is to :
1- All your checkboxes should share the same name.
2- All your checkboxes' values should be the ID of the row/object
3- When you POST the form, only the checked checkboxes are in the POST data. Retrieve those IDS and only update these objects.
For example, your checkbox should be something like :
<%= check_box_tag "row_ids[]", row.id, false, :id => "row_#{row.id}" %>
Then, in your controller :
Row.find(params[:row_ids]).each do |row|
# do whatever you want
end
Well you can do this, but it adds more work for the user: they have to check off multiple checkboxes before hitting Update. It be nicer if they just hit Update and it worked.
The basic idea is you want the user to just click Update and your code only updates records that changed.
What you can do is store (in a hidden field tag) the ID of each row's record. Then when you update, you loop through all rows and you grab (based on the ID stored in the hidden field) the record from the database. Let's say only Comments were editable. Then you can check to see if the comments have actually been changed (like with a simple string comparison) and if they have, update it. If more things are editable, then you can check them too before deciding if you need to update or not.
That is a high level description, but let me know if you want some more implementation details.