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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:16:35+00:00 2026-05-30T14:16:35+00:00

I’m having problems adding subviews to a scrollview sequentially. I’ve got a JSON response

  • 0

I’m having problems adding subviews to a scrollview sequentially.

I’ve got a JSON response coming back from the server which I parse into an array of Business objects, and I send off to the function updateCarousel, which looks like this:

-(void) updateCarousel: (NSArray *)response{
    if(response && response.count>0){
        int i=0;
        self.scrollView.hidden=NO;
        [self.scrollView setNeedsDisplay];
        self.pageControl.hidden=NO;

        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];

        for (Business *business in response){ 
            if (i >= MAX_INITAL_SEARCH_RESULTS)
                 break;

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;

        frame.size = scrollView.frame.size;

        CardView *cardView = [[CardView alloc] initWithBusinessData:business andFrame:frame];


        //I've tried the following code with and without wrapping it in a GCD queue
        dispatch_queue_t addingQueue = dispatch_queue_create("adding subview queue", NULL);
        dispatch_async(addingQueue, ^{
            [self.scrollView addSubview:cardView];
        });
        dispatch_release(addingQueue);

        cardView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];            
        i++;

        self.scrollView.contentSize = CGSizeMake(i*(self.scrollView.frame.size.width), self.scrollView.frame.size.height);
        self.pageControl.numberOfPages=i;

    }
}else{
    self.scrollView.hidden=YES;
    self.pageControl.hidden=YES;
    NSLog(@"call to api returned a result set of size 0");
}

The result – despite the many things I’ve tried – is always the same: the scrollView adds the subviews all at once, not as they are processed through the loop. I don’t understand how this is possible. If I add a sleep() at the end of the loop, it somehow waits for the whole loop to be over before it shows the subviews as added. How does it even know how long the results array is? I’m at my wits’ end, please help.

  • 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-30T14:16:36+00:00Added an answer on May 30, 2026 at 2:16 pm

    I assume you are not using any extra threads to process the data.
    What you experience is the application is stuck executing your method. Even though you add your subviews one by one (with a sleep between them), no other piece of code is executed to handle your adding.

    1. You can use another thread to load the data and add subviews but this needs to be synchronized to the main thread (more complicated).

    2 You can break your method in multiple calls. Between 2 calls of your load method, other pieces of code are allowed to execute and this means the scrollview will be able to process/display your subviews one by one.

    You will need to change your loading method to something like this:

    
    - (void)updateCarouselStep:(NSNumber*)loadIndex
    {
        if (response && response.count > 0)
        {
            // Here add only a subview corresponding to loadIndex
    
    
            // Here we schedule another call of this function if there is anything 
            if (loadIndex < response.count - 1)
            {
                [self performSelector:@selector(updateCarouselStep:) withObject:[NSNumber numberWithInt:(loadIndex+1) afterDelay:0.5f];
            }
        }
    
    }
    
    
    

    This is just one basic solution to the problem. For example, you need to take into consideration what happens if you update response data before you finish loading the previous one.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.