GCD goes back to the main topic

In my application, I load information from several (6-10) websites using NSXMLParser and then load information into views.

Currently, my apps are set up to navigate sites in viewDidLoad

my main view controller and load them each in a background thread that I created. It does this in a background thread, so the user does not have to wait for all sites to load before loading the view.

for (NSMutableDictionary *dict in self.sitesArray) {
    SiteData *data = [[SiteData alloc] init];
    [data setDelegate:self];
    dispatch_async(backgroundQueue, ^(void) {
        [data loadSite:[dict objectForKey:@"SiteName"]];
    });

}

      

In SiteData, I load the site using NSXMLParser (all delegate methods are also implemented correctly)

-(void)loadSite:(NSString *)site{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[self fullURLForSiteName:site]];
    [parser setDelegate:self];
    [parser parse];
    return;
}

      

When the NSXMLParser has completed the document and the SiteData instance is full of site data, it navigates to a method in my main view controller on the main thread.

- (void)parserDidEndDocument:(NSXMLParser *)parser{    
    dispatch_async(dispatch_get_main_queue(), ^(void) {
         [delegate successfullyLoadedSite:self];
    });
}

      

successfullyLoadedSite:

loads the submitted site data into a view and displays it to the user. Note that multiple site data is displayed on the same screen.

What I want to do: I want each site to appear on the screen as it loads, without waiting for all of them to finish loading to refresh the view.

What's going on: One of the sites is loading and displaying, then I have to wait for all the others to load and then all the others will be displayed right away.

From the logging console, it seems that as soon as it calls successfullyLoadedSite:

on the main queue for the first time, everything starts up in the main queue. Once the first one successfullyLoadedSite:

in the main queue is called, it loads all sites into objects and then loads them into views.

Any idea what's going on? If you can't tell I'm new to multithreading. Thank:)


Edit: I am creating backgroundQueue

like this:

dispatch_queue_t backgroundQueue;

      

and in init

backgroundQueue = dispatch_queue_create("uni.que.identifier.bgqueue", NULL);

      

and release it in dealloc

with

dispatch_release(backgroundQueue);

      

+3


source to share


1 answer


The queue using the dispatch_queue_create function to create is another queue, then the dispatch_queue_create block being dispatched will be executed as sequential order.

using:



dispatch_queue_t backgroundQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

      

+2


source







All Articles