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 7403073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:57:38+00:00 2026-05-29T04:57:38+00:00

In my app I have a tableView filled with contents from a server. To

  • 0

In my app I have a tableView filled with contents from a server. To download these contents I use NSURLConnection and I create a NSMutableArray (tableItems) to hold and to manage the addresses to the images I want to use.

In connectionDidFinishLoading, after populating tableItems, there is this for-loop:

for (int i = 0; i < [tableItems count]; i++) {

        // HERE I CHECK IF THE IMAGE I'M LOOKING FOR IS ALREADY ON DISK
        NSString *pathToCheckImage = [NSString stringWithFormat:@"%@/%@.png", pathToPreviews, [tableItems objectAtIndex:i]];
        NSData *dataOfCheckImage = [NSData dataWithContentsOfFile:pathToCheckImage];
        UIImage *checkImage = [UIImage imageWithData:dataOfCheckImage];

        NSString *pathToImage;
        UIImage *image;
        if (checkImage != nil) {
            // THE IMAGE IS ALREADY ON DISK SO I GET IT FROM TEMP FOLDER
            image = checkImage;
        } else {

            // THE IMAGE IS NEW SO I HAVE TO GET THE IMAGE FROM THE SERVER
            pathToImage = [NSString stringWithFormat:@"http://www.SERVER.com/SERVER_PATH/%@.png", [tableItems objectAtIndex:i]];

            NSURL *url = [NSURL URLWithString:pathToImage];
            NSData *data = [NSData dataWithContentsOfURL:url];
            image = [UIImage imageWithData:data];

            // AND SAVE IT ON DISK
            NSString *path = [NSString stringWithFormat:@"%@/%@.png", pathToPreviews, [tableItems objectAtIndex:i]];
            [self cacheImageOnDisk:image withPath:path];
        }

        if (image != nil) {
            [arrayOfImages addObject:image];
        }

}

This code is working, even if, depending on the number and size of the images I have to download from the server, it can take 1 or 2 minutes to perform its task.

The problem is that if the user quits (home button pushed) while this for-loop is running it keeps on performing its task until the end, even if it needs 1 minute to finish it.

During this period, launching the app again inevitably ends up crashing on startup.

I’ve tried to stop this for-loop on exit but applicationDidEnterBackground, applicationWillResignActive and applicationWillTerminate are not called until for-loop ends its task.

I’ve tried to set “application does not run in background”, but nothing changed.

Any suggestion will be really appreciated.

  • 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-05-29T04:57:40+00:00Added an answer on May 29, 2026 at 4:57 am

    You should not be downloading images on the main thread. The app won’t be responsive. Grand Central Dispatch is an easy method to accomplish these tasks.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSUInteger count = [tableItems count];
        for (int i = 0; i < count; i++) {
            // TODO: check if app didResignActive and break
    
            UIImage *image = ... // get image from disk or network
    
            // Add the image to the array on the main thread to avoid any threading issues
            dispatch_sync(dispatch_get_main_queue(), ^{
                [arrayOfImages addObject:image];
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app I have a tableview being filled by values from an NSMutableArray.
I'm trying to create a create a simple app where I have a TableView
I have a tableView storing a list of notifications (retrieved from server). How can
I have a tableview based app that downloads rss feeds from the web. So
I have a tableView which has cells with phone numbers. The app is not
I have an app that has a TableView, NavigationView and TabBar running together. There
I have an app that uses a tableview, along with a UIButton that I
I have an app which contains a scrollview with several tableviews. Each tableview is
I have a tableview that my app shows when it loads, I would like
In my iPhone app I have two viewControllers: viewController1 has a tableView that shows

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.