I’m very new to Parse and trying to set up what is basically a voting app where I can collect data on how many times all users have pressed a button. I found some information on Atomic Increment Operations from Parse but I’m trying to have this feature be implemented as something like a singleton.
So if I wanted to do this for one user:
gameScore.incrementKey("score" byAmount:NSNumber(numberWithInt:10))
gameScore.saveInBackground()
How would I get the same implementation where all users can update the same counter?
1
I was actually able to solve my problem by using Firebase instead of Parse. I wanted real-time updating and Firebase provided that with ease. Here’s my code for the View Controller:
//
// ViewController.swift
// Firebase Test
//
// Created by Cole Smith on 7/24/15.
// Copyright (c) 2015 Cole Smith. All rights reserved.
//
import UIKit
import Firebase
class ViewController: UIViewController {
var firebaseRef = Firebase(url: "[LINK TO FIREBASE]")
var valueOfCounter: Int = 0
@IBOutlet weak var counterLabel: UILabel!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func counterPressed(sender: AnyObject) {
valueOfCounter++
incrementCounterToFirebase(valueOfCounter)
}
func incrementCounterToFirebase(intForSubmission: Int) {
firebaseRef.setValue(intForSubmission)
// Read data and react to changes
firebaseRef.observeEventType(.Value, withBlock: {
snapshot in
println("(snapshot.key) -> (snapshot.value)")
self.counterLabel.text = "(snapshot.value)"
})
}
}