snippetswiftMinor
Optimize Data Sending/Querying to Parse Swift iOS
Viewed 0 times
swiftiossendingparsequeryingoptimizedata
Problem
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:
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!
@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!
Solution
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
in most cases. Instead of comparing with
use optional binding to test and get the unwrapped value:
Or, if
is called (e.g. because it is set up in
as an implicitly unwrapped optional
There is no reason to declare
With
you get rid of more forced unwrap operators.
Finally, the
(otherwise you did not connect it correctly in the interface builder).
That is the reason why it is declared as an implicitly unwrapped optional
Putting it all together, the method should look like this:
No
your code.
The main point is that there is far too much "forced unwrapping". The forced unwrap operator
! can and should be avoidedin most cases. Instead of comparing with
nil and forced unwrappingif (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 methodis called (e.g. because it is set up in
viewDidLoad), declare itas 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 invotesLabel?.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!Code Snippets
if (parseObject != nil) {
parseObject!.doSomething()
parseObject!.doSomethingElse()
}if let theParseObject = parseObject {
theParseObject.doSomething()
theParseObject.doSomethingElse()
}var parseObject : PFObject!
// ...
parseObject.doSomething()
parseObject.doSomethingElse()if var votes = parseObject.objectForKey("votes") as? Int { ... }votesLabel?.text = "+\(votes!)"Context
StackExchange Code Review Q#112353, answer score: 2
Revisions (0)
No revisions yet.