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!
-
1\$\begingroup\$ Well done on the clean-up :-) \$\endgroup\$Mast– Mast ♦2015年11月30日 21:42:21 +00:00Commented Nov 30, 2015 at 21:42
-
\$\begingroup\$ Thanks @Mast! Any clues as to how to improve this would be greatly appreciated as well :) \$\endgroup\$Lukesivi– Lukesivi2015年12月01日 07:08:53 +00:00Commented Dec 1, 2015 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– Daniel Galasko2015年12月03日 05:25:18 +00:00Commented Dec 3, 2015 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– Daniel Galasko2015年12月03日 05:26:20 +00:00Commented Dec 3, 2015 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– Lukesivi2015年12月03日 07:41:25 +00:00Commented Dec 3, 2015 at 7:41
1 Answer 1
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!
-
\$\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– Lukesivi2015年12月14日 19:33:23 +00:00Commented Dec 14, 2015 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– Martin R2015年12月14日 19:41:44 +00:00Commented Dec 14, 2015 at 19:41
Explore related questions
See similar questions with these tags.