4
\$\begingroup\$

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) {

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

            votes!++
            parseObject!.setObject(votes!, forKey: "votes")
            parseObject!.saveInBackground()

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

    }
}

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ Well done on the clean-up :-) \$\endgroup\$ – Mast Nov 30 '15 at 21:42
  • \$\begingroup\$ Thanks @Mast! Any clues as to how to improve this would be greatly appreciated as well :) \$\endgroup\$ – Lukesivi Dec 1 '15 at 7:08
  • \$\begingroup\$ Wouldn't recommend putting the update code inside the function that handles the tap event, not really reusable \$\endgroup\$ – Daniel Galasko Dec 3 '15 at 5:25
  • \$\begingroup\$ Furthermore, are you noticing performance issues or you just want to optimize for the sake of it? Could consider using saveEventually() \$\endgroup\$ – Daniel Galasko Dec 3 '15 at 5:26
  • \$\begingroup\$ 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 \$\endgroup\$ – Lukesivi Dec 3 '15 at 7:41
1
\$\begingroup\$

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 {
        if var votes = theParseObject.objectForKey("votes") as? Int {
            votes++
            theParseObject.setObject(votes, forKey: "votes")
            theParseObject.saveInBackground()
            votesLabel.text = "+\(votes)"
        }
    }
}

No ! anymore!

\$\endgroup\$
  • \$\begingroup\$ hey this works, thanks a lot! Why is forced unwrapping bad practice in this case? Why is it bad practice in general? Does this increase speed, execution? Overall question: what does this change do compiler wise? Thank you once again. \$\endgroup\$ – Lukesivi Dec 14 '15 at 19:33
  • 1
    \$\begingroup\$ @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/…. \$\endgroup\$ – Martin R Dec 14 '15 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.