# Optimize Data Sending/Querying to Parse Swift iOS

I have a swift method that I'm using to update Parse in the backend when the button is tapped. Votes are being updated: The method acts as a voting system, incrementing a label every time it's tapped. This action is happening in a Custom Cell:

    @IBOutlet weak var votesLabel: UILabel!

var parseObject:PFObject?
@IBAction func happyBtn(sender: AnyObject) {

if(parseObject != nil) {

parseObject!.saveInBackground()

}

}
}


Was wondering how I can optimize this function to send the data to and from Parse in the most efficient manner?

All help is appreciated!

• Well done on the clean-up :-) – Mast Nov 30 '15 at 21:42
• Thanks @Mast! Any clues as to how to improve this would be greatly appreciated as well :) – Lukesivi Dec 1 '15 at 7:08
• Wouldn't recommend putting the update code inside the function that handles the tap event, not really reusable – Daniel Galasko Dec 3 '15 at 5:25
• Furthermore, are you noticing performance issues or you just want to optimize for the sake of it? Could consider using saveEventually() – Daniel Galasko Dec 3 '15 at 5:26
• Currently, this works fine. However, I'm assuming that the sending of data could be done in a more efficient manner than sending to the server every time the user votes. Do you know what I mean? Lastly, I've tried putting this code in the TableViewController in the tap event but it doesn't get executed. @DanielGalasko – Lukesivi Dec 3 '15 at 7:41

I have some feedback concerning the usage of Swift optionals in your code.

The main point is that there is far too much "forced unwrapping". The forced unwrap operator ! can and should be avoided in most cases. Instead of comparing with nil and forced unwrapping

if (parseObject != nil) {
parseObject!.doSomething()
parseObject!.doSomethingElse()
}


use optional binding to test and get the unwrapped value:

if let theParseObject = parseObject {
theParseObject.doSomething()
theParseObject.doSomethingElse()
}


Or, if parseObject is expected to be non-nil when the method is called (e.g. because it is set up in viewDidLoad), declare it as an implicitly unwrapped optional

var parseObject : PFObject!
// ...
parseObject.doSomething()
parseObject.doSomethingElse()


There is no reason to declare var notes as an optional Int?. With

if var votes = parseObject.objectForKey("votes") as? Int { ... }


you get rid of more forced unwrap operators.

Finally, the votesLabel textfield outlet is expected to be non-nil (otherwise you did not connect it correctly in the interface builder). That is the reason why it is declared as an implicitly unwrapped optional UILabel!. There is no need for optional chaining in

 votesLabel?.text = "+\(votes!)"


Putting it all together, the method should look like this:

@IBAction func happyBtn(sender: AnyObject) {

if let theParseObject = self.parseObject {

No ! anymore!
• @lukesIvi: Each x! is a possible candidate for a runtime crash. It relies on you doing the appropriate checks before. Doing "test and unwrap" once saves some instructions later and is safer. See also stackoverflow.com/questions/29717210/…. – Martin R Dec 14 '15 at 19:41