Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using AFNetworking in my application and I am attempting to implement a "Tap To Cancel" feature in my progress HUD. I have a singleton class that manages all of the HTTP requests. If the progress HUD is tapped, I call:

[[[HTTPRequestSingleton sharedClient] operationQueue] cancelAllOperations];

But this doesn't "cancel" the operation like I need it to. I read up on the NSOperationQueue docs and came across this:

Canceling an operation object leaves the object in the queue but notifies the object that it should abort its task as quickly as possible. For currently executing operations, this means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.

And regarding the cancelAllOperations method:

This method sends a cancel message to all operations currently in the queue. Queued operations are cancelled before they begin executing. If an operation is already executing, it is up to that operation to recognize the cancellation and stop what it is doing.

My problem seems to specifically involve an operation that is already executing, which I want to immediately cancel. With AFNetworking, how can I alert the operation that it should cancel and discard all information about the request?

Code used for operation

AFJSONRequestOperation *loginOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    //operation was successful

    if (loginOperation.isCancelled)
    {
        //can't do this. variable 'loginOperation' is uninitialized when captured by block      
    }

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

    //operation failed

}];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
626 views
Welcome To Ask or Share your Answers For Others

1 Answer

After a full morning of digging through AFNetworking source code, it turns out that the reason my operations weren't canceling had nothing to do with the operations themselves, but rather because I've been starting the operations incorrectly all this time. I've been using

[NSOperation start];

when I should have been adding it to my HTTPRequestSingleton's operation queue:

[[[HTTPRequestSingleton sharedClient] operationQueue] addOperation:NSOperation];

Adding it to the queue allows it to be canceled properly without having to check the isCancelled property.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...