Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:41:32+00:00 2026-06-13T12:41:32+00:00

I wrote a function that downloads image from web. It takes block as an

  • 0

I wrote a function that downloads image from web. It takes block as an argument. Everything worked fine until i added some memory caching. If a image is alredy in cache, the function doesnt return (but block is not nil).

- (void) downloadImageFromURL:(NSURL *) url completionBlock:(void (^)(UIImage *image, NSError *error)) block {

dataHandler = [DataHandler sharedInstance];

UIImage *img=[dataHandler.avatarImages objectForKey:[url absoluteString]];
//image is in cache
if (img) {
    block(img, nil);
}
//not in cache, download it (works ok)
else {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        UIImage *picture=[UIImage imageWithData:imageData];
        if(picture) {
            //save to cache
            [dataHandler.avatarImages setObject:picture forKey:[url absoluteString]];
            block(picture, nil);
        }
        else {
            NSError *error = [NSError errorWithDomain:@"image_download_error" code:1
                                             userInfo:[NSDictionary dictionaryWithObject:@"Can't fetch data" forKey:NSLocalizedDescriptionKey]];
            block(nil, error);
        }

    });
}

The block is called like this:

        ImageDownloader *idl=[[ImageDownloader alloc] init];

        NSURL *imageUrl=[NSURL URLWithString:ta.avatarUrl];
        [idl downloadImageFromURL:imageUrl completionBlock:^(UIImage *image, NSError *error)
         {
             if(!error) {
                 dispatch_async(dispatch_get_main_queue(), ^(void) {
                     logo.image=image;
                 });
             } else {
                 NSLog(@"error %@", error);
             }

         }];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:41:33+00:00Added an answer on June 13, 2026 at 12:41 pm

    change your call to the block when image in cache to look like this:

    if (img) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            block(img, nil);
        });
    }
    

    user shouldn’t care if the image in cache or not, and in any case your block should be called asynchronous.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a function I wrote quite some time ago that works fine, but
I'm trying to build up an algorithm that downloads a JPEG image from an
I am working on a function to download an image from a web server,
I wrote a function that scans a tab delimited file of baseball stats. public
So I wrote this function that is given possible numbers, and it has to
i wrote a custom jquery function that accept a callback but not being able
I wrote a simple function, that makes emacs add matching quotes (so when I
I have a C++ DLL that I wrote that has a single exposed function,
I want to write a function that takes a list of numbers and returns
So I have a function which gets an image from a URL which looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.