Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
//My questions are based on the following code that come from the AFNetworking code that is freely available online. I want to understand what this code is saying. Please see the first point below, named step 1.

Code:
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
              placeholderImage:(UIImage *)placeholderImage 
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
    if (![urlRequest URL] || (![self.af_imageRequestOperation isCancelled] && [[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]])) {
        return;
    } else {
        [self cancelImageRequestOperation];
    }
    
    UIImage *cachedImage = [[AFImageCache sharedImageCache] cachedImageForURL:[urlRequest URL] cacheName:nil];
    if (cachedImage) {
        self.image = cachedImage;
        self.af_imageRequestOperation = nil;
        
        if (success) {
            success(nil, nil, cachedImage);
        }
    } else {
        self.image = placeholderImage;
        
        AFImageRequestOperation *requestOperation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
        [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(operation.request, operation.response, responseObject);
            }
            
            if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
                self.image = responseObject;
            } else {
                self.image = placeholderImage;
            }
            
            [[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[urlRequest URL] cacheName:nil];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failure) {
                failure(operation.request, operation.response, error);
            }
        }];
        
        self.af_imageRequestOperation = requestOperation;
        
        [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];
    }
}

Step 1:
Code:
if (![urlRequest URL] || (![self.af_imageRequestOperation isCancelled] && [[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]])) {
        return;
    } else {
        [self cancelImageRequestOperation];
    }

This bit says: If a URL is not present within urlRequest or (the imageRequestOperation is not cancelled and the URL within urlRequest is equal to the URL in the request in self.af_imageReuqestOperation) then return and do nothing.

otherwise cancel the request and move forward.

Step 2:
Code:
UIImage *cachedImage = [[AFImageCache sharedImageCache] cachedImageForURL:[urlRequest URL] cacheName:nil];
    if (cachedImage) {
        self.image = cachedImage;
        self.af_imageRequestOperation = nil;
        
        if (success) {
            success(nil, nil, cachedImage);
        }
    } else {
        self.image = placeholderImage;

This bit says: Check cachedImage archive for an downloaded image that matched a url. If you find it, assign it to the self.image object and delete the self.af_imageRequestOperation. Oh and if the success object exists (not sure what created it) then assign it's request and response parameters as nil respectively and the image parameter as the chatted image we found.

If no success then set up a placeholder image, because we didn't find **** in the cache archive.

Then move forward

Step 3:
Code:
AFImageRequestOperation *requestOperation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
        [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(operation.request, operation.response, responseObject);
            }
            
            if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
                self.image = responseObject;
            } else {
                self.image = placeholderImage;
            }
            
            [[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[urlRequest URL] cacheName:nil];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failure) {
                failure(operation.request, operation.response, error);
            }
        }];

This bit says: Set up a request operation because we didn't find **** early in the cache archive. Initialise it with the request that was passed in. If we receive a successful chunk of info from the server, and the success object exists, then assign the request, response and response objects accordingly. Otherwise, don't assign ****.

If the url in the urlReqest is the same as the url in the request in the self.af_imageRequestOperation, then we got a match and assign the self-image object with the downloaded image. If we don't have a match, then assign a placeholder image. In any case, cash the response data with the key URL in urlRequest.

If we didn't download information from the server for whatever reason, then assign the properties in the failure block accordingly.

Add the requestOperation to the queue to manage threading of all this.

Have I got this right?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.