0

I have a UITextfield and a UIButton. The user can enter, for example, search word such as "dog" or "cat" and it will trigger a method in another class that runs on a custom dispatch GCD queue to fetch the images (around 100 or so).

Everything works fine, except if the user in the midst of fetching, decides to change and enter another search word such as "cat" and then press the fetch button, I would like to be able to stop that thread / method while it is fetching the images from the previous search term.

I have thought about NSThread (something I never used before) or blocks (to get notified once the method has finished running), but the problem with blocks is, I will get notified once the method had finished doing its thing, but what I need here is to tell it to stop fetching (because the user has decided on another search and entered another search term).

Can someone please cite me with some samples, as to how we can be able to stop a loop / method while it is running on a custom GCD thread before it is finished? Thanks in advance.

Unheilig
  • 16,196
  • 193
  • 68
  • 98

2 Answers2

1

I'm using NSOperationand NSOperationQueue to cluster markers on a map in the background and to cancel the operation if necessary. The function to cluster the markers is implemented in a subclass of NSOperation:

ClusterMarker.h:

@class ClusterMarker;

@protocol ClusterMarkerDelegate <NSObject>

- (void)clusterMarkerDidFinish:(ClusterMarker *)clusterMarker;

@end

@interface ClusterMarker : NSOperation

-(id)initWithMarkers:(NSSet *)markerSet delegate:(id<ClusterMarkerDelegate>)delegate;
// the "return value"
@property (nonatomic, strong) NSSet *markerSet;
// use the delegate pattern to inform someone that the operation has finished
@property (nonatomic, weak) id<ClusterMarkerDelegate> delegate;

@end

and ClusterMarker.m:

@implementation ClusterMarker

-(id)initWithMarkers:(NSSet *)markerSet delegate:(id<ClusterMarkerDelegate>)delegate
{
    if (self = [super init]) {
        self.markerSet = markerSet;
        self.delegate = delegate;
    }
    return self;    
}

- (void)main {
    @autoreleasepool {

        if (self.isCancelled) {
            return;
        }

        // perform some Überalgorithmus that fills self.markerSet (the "return value")

        // inform the delegate that you have finished
        [(NSObject *)self.delegate performSelectorOnMainThread:@selector(clusterMarkerDidFinish:) withObject:self waitUntilDone:NO];
    }
}

@end

You could use your controller to manage the queue,

self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.name = @"Überalgorithmus.TheKillerApp.makemyday.com";
// make sure to have only one algorithm running
self.operationQueue.maxConcurrentOperationCount = 1;

to enqueue operations, kill previous operations and the like,

ClusterMarker *clusterMarkerOperation = [[ClusterMarker alloc] initWithMarkers:self.xmlMarkerSet delegate:self];
// this sets isCancelled in ClusterMarker to true. you might want to check that variable frequently in the algorithm
[self.operationQueue cancelAllOperations];
[self.operationQueue addOperation:clusterMarkerOperation];

and to respond to the callbacks when the operation has finished:

- (void)clusterMarkerDidFinish:(ClusterMarker *)clusterMarker
{
    self.clusterMarkerSet = clusterMarker.markerSet;

    GMSProjection *projection = [self.mapView projection];
    for (MapMarker *m in self.clusterMarkerSet) {
        m.coordinate = [projection coordinateForPoint:m.point];
    }

//    DebugLog(@"now clear map and refreshData: self.clusterMarkerSet.count=%d", self.clusterMarkerSet.count);
    [self.mapView clear];
    [self refreshDataInGMSMapView:self.mapView];
}

If I remember correctly I used this tutorial on raywenderlich.com as a starter.

user511
  • 154
  • 1
  • 7
0

I would recommend using NSOperation as it has cancel method which will cancel the current running operation.

ldindu
  • 4,270
  • 2
  • 20
  • 24
  • 1
    If you use `NSOperation` you need to be sure to check `isCancelled` within your code in order to detect when to abort. – mttrb Jun 29 '13 at 14:49