DatabaseViewController.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseDatabase
  15. import UIKit
  16. /// A class to demonstrate the Firebase Realtime Database API. This will show a number read
  17. /// from the Database and increase or decrease it based on the buttons pressed.
  18. class DatabaseViewController: UIViewController {
  19. private enum Counter: Int {
  20. case increment = 1
  21. case decrement = -1
  22. var intValue: Int {
  23. return rawValue
  24. }
  25. }
  26. // MARK: - Interface
  27. /// Label to display the current value.
  28. @IBOutlet var currentValue: UILabel!
  29. // MARK: - User Actions
  30. /// The increment button was hit.
  31. @IBAction func incrementButtonHit(_ sender: UIButton) { changeServerValue(with: .increment) }
  32. /// the decrement button was hit.
  33. @IBAction func decrementButton(_ sender: UIButton) { changeServerValue(with: .decrement) }
  34. // MARK: - Internal Helpers
  35. /// Update the number on the server by a particular value. Note: the number passed in should only
  36. /// be one above or below the current number.
  37. private func changeServerValue(with type: Counter) {
  38. let ref = Database.database().reference(withPath: Constants.databasePath)
  39. // Update the current value of the number.
  40. ref.runTransactionBlock { currentData -> TransactionResult in
  41. guard let value = currentData.value as? Int else {
  42. return TransactionResult.abort()
  43. }
  44. currentData.value = value + type.intValue
  45. return TransactionResult.success(withValue: currentData)
  46. }
  47. }
  48. // MARK: - View Controller Lifecycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Observe the current value, and update the UI every time it changes.
  52. let ref = Database.database().reference(withPath: Constants.databasePath)
  53. ref.observe(.value) { [weak self] snapshot in
  54. guard let value = snapshot.value as? Int else {
  55. print("Error grabbing value from Snapshot!")
  56. return
  57. }
  58. self?.currentValue.text = "\(value)"
  59. }
  60. }
  61. // MARK: - Constants
  62. private enum Constants {
  63. static let databasePath = "magicSyncingCounter"
  64. }
  65. }