Anders
Anders

Reputation: 2941

AFNetworking, callback when finished request?

I wonder if it's possible to get a callback when AFNetworkingfinished a request? I'm making an app in which I have a edge case, all downloaded items is checked is_deleted (and should not be displayed in the UI). If this is case I would like to automatically try to download older data (create a new AFNetworking request).

I have tried to call my loadData method again, from itself. But it creates an infinitive loop. Like this:

- (void)loadDataIsOlder:(NSNumber *)older
{    
    NSURLRequest *request = [NSURLRequest requestWithURL:self.streamUrl];
    AFJSONRequestOperation *operation;
    operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonObject) {
        NSDictionary *data = [jsonObject objectForKey:@"data"];
        NSDictionary *meta = [jsonObject objectForKey:@"meta"];
       [...]
        for (NSNumber *deleted in (NSArray *)[data valueForKey:@"is_deleted"]) {
            if ([deleted boolValue] == YES) {
                self.shouldTryToLoadMore = YES;
            } else {
                self.shouldTryToLoadMore = NO;
            }
        }
        if (self.shouldTryToLoadMore == YES) {
            [self loadDataIsOlder:[NSNumber numberWithBool:YES]];
        }
    } [...]
}

So, I wonder if it's some other / better way to do what I want.

Subquestion The array (NSArray *)[data valueForKey:@"is_deleted"] consists of BOOL int values. I wonder if its possible to easily calculate the values of an array containing ints, e.g. [1, 1, 1, 0] = 3? I want to check if all values in the array is 1:s.

Upvotes: 0

Views: 1339

Answers (2)

JeffN
JeffN

Reputation: 1605

You can set a retry counter and put it in the failure block, so it would look something like this:

        - (void)loadDataIsOlder:(NSNumber *)older
    {   
 if (shouldTrytoReloadMore){
        NSURLRequest *request = [NSURLRequest requestWithURL:self.streamUrl];
        AFJSONRequestOperation *operation;
        operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonObject) {
            NSDictionary *data = [jsonObject objectForKey:@"data"];
            NSDictionary *meta = [jsonObject objectForKey:@"meta"];
           [...]
            for (NSNumber *deleted in (NSArray *)[data valueForKey:@"is_deleted"]) {
                if ([deleted boolValue] == YES) {
                    self.shouldTryToLoadMore = YES;
                    [self loadDataIsOlder:[NSNumber numberWithBool:YES]];

                } else {
                    self.shouldTryToLoadMore = NO;
                }
            }
            }
        } [...]
    }
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}

WHen you call the method the first time, set n as the number of times you need to retry.

Upvotes: 1

Dan Shelly
Dan Shelly

Reputation: 6011

I am not familiar with AFNetworking

I wonder if it's possible to get a callback when AFNetworking finished a request?

You could always set the completion block of the operation (will be called even on failure, on the thread the operation run in so be careful with UI updates).
like so: operation.completionBlock = ^{/*what ever you like to accomplish*/};

all downloaded items is checked is_deleted

I'm spotting a little problem in your algorithm.
You are not accumulating the deletion keys properly (you only respond to the last item status).
try: self.shouldTryToLoadMore &= [deleted boolValue]; instead of your if statement.

If you like something shorted (to avoid the "visible" loop) try:

NSMutableSet* set = [NSMutableSet setWithArray:[data valueForKey:@"is_deleted"]];
[set addObject:@YES];
if ([set count] == 1) {
//You have only deleted items in your request
}

note
You are capturing self in the success block, which might be problematic (retain cycle if you own the operation in self).

Upvotes: 1

Related Questions