How to cancel a completion handler in iOS

jjpp

I want to get an array of nearby locations using mapkit framework. So when the user types in a textfield I call the following function.

- (void)searchForLocations:(NSString *)string
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(search:) object:nil];
    [self performSelectorInBackground:@selector(search:) withObject:string];
}

- (void)search :(NSString *)string
{
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = string;
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;

    region.span = span;
    region.center = newLocation.coordinate;

    request.region = region;

    MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];

    [search startWithCompletionHandler:^(MKLocalSearchResponse
                                         *response, NSError *error) {
        if (response.mapItems.count == 0)
        {
            NSLog(@"No Matches");
        }
        else
        {

            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
        }
    }];
}

As you can see I want to cancel the previous search if a new input text is coming. But the previous search is not cancelled. How can i cancel the previous search?

Thanks in advance.

Yaser

There is a cancel method on MKLocalSearch. Have you tried that one?

Edit: Ah, sorry, I was being stupid. You need to keep a reference to your old search in some way in order to cancel it. Put it in a property which you can clear (i.e. set to nil) when the search is finished. Whenever you call the search function, cancel the previous search function (no "if" needed, nil swallows all), then create a new one

@property (nonatiomic, strong) MKLocalSearch *previousSearch;

- (void)search :(NSString *)string
{
    [self.previousSearch cancel];
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = string;
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;

    region.span = span;
    region.center = newLocation.coordinate;

    request.region = region;

    MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];

    [search startWithCompletionHandler:^(MKLocalSearchResponse
                                     *response, NSError *error) {
        self.previousSearch = nil;
        if (response.mapItems.count == 0)
        {
            NSLog(@"No Matches");
        }
        else
        {

            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
        }
    }];

    self.previousSearch = search;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Completion handler not called - iOS

From Dev

Completion handler not called - iOS

From Dev

How can I properly document a method with a completion handler in iOS swift

From Dev

How to call this completion handler?

From Dev

How to cancel a postDelay Handler

From Dev

UIActivityViewController completion handler still calls action if user presses cancel

From Dev

How to reset a Completion Handler in Swift

From Dev

Store Completion Handler in Variable iOS (Objective c)

From Dev

Google SDK iOS - sign() method completion handler

From Dev

How are each of the iOS background fetch UIBackgroundFetchResult types handled after the completion handler is called?

From Dev

How to store a Closure completion handler to call later?

From Dev

How to write completion handler in Swift with animation.

From Dev

How to pass background task identifier to completion handler?

From Dev

How to add completion handler/closure for 'animated: true'

From Dev

How to properly dismiss modal viewcontroller with completion handler

From Dev

How can a completion handler return to the wrong ViewController?

From Dev

How can I use the MDCSnackbarMessage completion handler?

From Dev

Is it possible to call a block completion handler from another function in iOS?

From Dev

Cancel a completion block

From Dev

Cancel zsh tab completion

From Dev

Cancel zsh tab completion

From Java

How could I create a function with a completion handler in Swift?

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

From Dev

How to create custom NSAlert Sheet Method with the Completion Handler paradigm

From Dev

How to use lambda to for boost asio async completion handler

From Dev

How to pass (optional) completion handler closure to transitionFromViewController in Swift?

From Dev

How to use lambda to for boost asio async completion handler

From Dev

How can I assign result from completion handler to a variable?

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

Related Related

  1. 1

    Completion handler not called - iOS

  2. 2

    Completion handler not called - iOS

  3. 3

    How can I properly document a method with a completion handler in iOS swift

  4. 4

    How to call this completion handler?

  5. 5

    How to cancel a postDelay Handler

  6. 6

    UIActivityViewController completion handler still calls action if user presses cancel

  7. 7

    How to reset a Completion Handler in Swift

  8. 8

    Store Completion Handler in Variable iOS (Objective c)

  9. 9

    Google SDK iOS - sign() method completion handler

  10. 10

    How are each of the iOS background fetch UIBackgroundFetchResult types handled after the completion handler is called?

  11. 11

    How to store a Closure completion handler to call later?

  12. 12

    How to write completion handler in Swift with animation.

  13. 13

    How to pass background task identifier to completion handler?

  14. 14

    How to add completion handler/closure for 'animated: true'

  15. 15

    How to properly dismiss modal viewcontroller with completion handler

  16. 16

    How can a completion handler return to the wrong ViewController?

  17. 17

    How can I use the MDCSnackbarMessage completion handler?

  18. 18

    Is it possible to call a block completion handler from another function in iOS?

  19. 19

    Cancel a completion block

  20. 20

    Cancel zsh tab completion

  21. 21

    Cancel zsh tab completion

  22. 22

    How could I create a function with a completion handler in Swift?

  23. 23

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

  24. 24

    How to create custom NSAlert Sheet Method with the Completion Handler paradigm

  25. 25

    How to use lambda to for boost asio async completion handler

  26. 26

    How to pass (optional) completion handler closure to transitionFromViewController in Swift?

  27. 27

    How to use lambda to for boost asio async completion handler

  28. 28

    How can I assign result from completion handler to a variable?

  29. 29

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

HotTag

Archive