views:

53

answers:

1

Hi there.

Does someone know a way to remove requests from an ASINetworkQueue in a persistent way? The reset function doesn't seem to do the job. What I'm trying to do is the following

- (void)fillAndRunQueue:(ASINetworkQueue*)queue requests:(NSArray*)requests {  
    for (ASIHTTPRequest* request in requests) {  
        if ([request check]) { // Valid request => add it to the queue    
            [queue addOperation:request];  
        } else { // Invalid request => cancel immediatelly  
            // HOW TO REMOVE ALL PREVIOUS REQUESTS FROM THE QUEUE??  
            return;  
        }  
    }  

    [queue go];  
}
+1  A: 

The two obvious options I can see are:

[queue cancelAllOperations];

or if you want more control:

[request cancel];

for each request you want to "remove" (this isn't technically removing them, but may have a close enough effect for your purposes).

Although, I think [queue reset]; should also work - maybe you can explain exactly what happens when you try to use it?

If all else fails, releasing and then recreating the queue should remove everything.

Update

To further explain, I don't think it's possible to actually remove items from an NSOperationQueue, only to cancel them. (ASINetworkQueue is a subclass of NSOperationQueue.)

The apple docs are here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html

JosephH
First of all, thanks for replying. The problem with [queue cancelAllOperations], [queue reset] and [request cancel] (for each request) is that they all leave the number of requests in the queue unchanged. I did monitor the latter with [queue requestsCount] and it always stays the same.
hennes
To be more clear, what I want is to completely emtpy the queue. Of course releasing and recreating the queue would be an option for this, but I hoped there would be a more elegant way.
hennes
Another workaround would be to first verify all requests and then add them to the queue at once. However, the original question (how to generally remove requests from a queue) still remains.
hennes
I think the answer to that is that it is not actually possible to remove items from an NSOperationQueue - I've updated my answer to explain and link to the docs.
JosephH
Yeah, seems you're right on that. I guess I'll go with the approach of validating all requests before they're actually added to the queue. Thanks a lot!
hennes