0

I've connected my app to Firebase Realtime database and have set up a data class but haven't found a way to save an int to the database. I would like to store the number of times a user presses a certain button. I have seen a few yt tutorials but haven't gotten past this step since I couldn't find one that's specifically for Int and not String.

Data Class: data class Total(val amount: Int? = null)

Activity:

class SecondActivity : AppCompatActivity() {

    private lateinit var Total: Total
    private lateinit var amount : Total
    var database : DatabaseReference = FirebaseDatabase.getInstance().getReference("Total")

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

var incrementNumber = 0
button_increment.setOnClickListener {

     database.child("value").setValue(incrementNumber);
     "Button has been pressed $incrementNumber times!".also {increment_textview.text = it }

        }
    }
}
John Doe
  • 33
  • 5
  • Yes, `int` values can be stored in Firebase. They'd fall under the `NSNumber` mentioned in the docs [here](https://firebase.google.com/docs/database/ios/read-and-write#basic_write) and work no differently than storing a string value. If you have any problems with this, edit your code to show how you are trying to store the integer value into the database. – Frank van Puffelen Aug 04 '21 at 03:13
  • Hello, I am having trouble editing my code because I am unaware of a how to store the integer value into the database. In ```button_increment.setOnClickListener``` is where I am trying to figure out a way to increment the number of button presses to be stored by the database. For now ```incrementNumber++``` works for locally counting the number of button presses. – John Doe Aug 04 '21 at 03:26
  • 1
    Trying to **increment** a value is significantly different than trying to read a value. I'll write up an answer based on https://firebase.google.com/docs/database/ios/read-and-write#atomic_server-side_increments – Frank van Puffelen Aug 04 '21 at 03:30

2 Answers2

2

To atomically increment a value in the database, you can do:

Database.database().reference().setValue(["counter": ServerValue.increment(1)]);

If the value already exists, this increments it by 1. If the value doesn't exist yet, this sets it to 1.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for your response and I apologize for misunderstanding the incrementing and reading a value. Would replacing my ```Increment++``` with this Line of code work without any other steps? I'm worried that I've might not have properly set up the data class or referenced it correctly in the activity because as of now, the line of code provided gives me an error that the first "Database" in ```Database.database().reference().setValue(["counter": ServerValue.increment(1)]);``` is an unresolved reference. I tried replacing it with the name of my data class but to no avail. – John Doe Aug 04 '21 at 03:44
  • In your case, you'll first need to initialize `database` and then you can do `database().reference().setValue...`. – Frank van Puffelen Aug 04 '21 at 03:50
  • Is ```private lateinit var database : FirebaseDatabase``` not initializing the database? I've also tried using ```database = FirebaseDatabase.getInstance().getReference("Total")``` "Total" being the data class, but neither attempt has worked – John Doe Aug 04 '21 at 04:09
  • See https://github.com/firebase/quickstart-ios/blob/master/database/DatabaseExampleSwift/PostListViewController.swift#L35 for an example – Frank van Puffelen Aug 04 '21 at 14:22
1

Try below. Basically we are getting the Total node and updating its value.

Step 1:

var database : FirebaseDatabase = FirebaseDatabase.getInstance().getReference("Total")

Step 2:

database.child("value").setValue(incrementNumber);
HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • Thank you for your response. The good news is that the "Total" value can be seen in the console. However the value is separate from the "Increment" Data Class. Does that mean the Data Class is not needed? I've updated code in the question to include your contribution. The app runs correctly and does not crash when I press the increment button. However the value remains at 0. – John Doe Aug 04 '21 at 04:53
  • change "value" for "amount" in Step 2. – HaroldSer Aug 04 '21 at 04:56
  • I made the change and the console does show "Total" holding "amount" correctly but unfortunately the value for "amount" is still 0 despite the number of button presses – John Doe Aug 04 '21 at 05:05
  • change incrementNumber to ++incrementNumber. This will add 1 to incrementNumber before sending it to the database through setValue. – HaroldSer Aug 04 '21 at 05:16
  • Thank you, this is working almost perfectly! I have the console open as I run the app and I can see it update live with each press of the button. The only issue is that when I close and reopen the app, the number is reset to 0. I think it might have something to do with how I set up the code because the number also resets when I return to the previous activity, I fear that it may reset whenever I press the button after every new "onCreate" when the activity is opened up. – John Doe Aug 04 '21 at 05:22