m1234 m1234 - 4 months ago 21x
iOS Question

Cancel a query when segmented control index changes Swift

I am wondering how I can cancel a query from Parse Server when the index value of my segmented control changes.

Lets say I have three classes in Parse Server. Past Posts, Present Posts, and Future Posts. I use a segmented control to populate a table view based on which selection has been made. For example if Past Posts is selected then the table view is loaded with the Past Posts data from Parse Sever and if the Future Posts is selected then the Future Posts data is loaded in the table view from Parse Server.

Now lets say the user selects the Past Posts option and a query starts fetching data from Parse Server. The user then selects Future Posts before the Past Posts query is finished. How can I cancel the Past Posts query so that only the Future Posts query is active. Any help is much appreciated!

@IBAction func didSelectSelegment(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:

case 1:

case 2:

case 3:
displayAlert("Error", message: "Unexpected Index")



Create a variable in your ViewController:

var currentQuery: PFQuery?

Then you can set this variable to whatever query you are executing or set it to nil if you are not running a query. To cancel it, simply run: