0

I create a UIView contentView and display it. I then fetch data from server and create a bunch of subviews displaying the data. I am using MBProgressHUD to display while waiting on data.

if (datasetSubBar.panels == nil) {
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:datasetSubBar.filterListView animated:YES];

    HUD.labelText = @"Creating Panels";

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [datasetSubBar createPanels];

        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:datasetSubBar.filterListView animated:YES];
        });
    });
}

in createPanels method, i fetch the data then create the panels. The panel is created, added to filterListView (the content view) and then add the constraints:

for (int i = 0; i < panels.count; i++) {
    NSLog(@"thread: %@", [NSThread currentThread]);

    NSDate *startDate = [NSDate date];

    DatasetFilterListPanelView *panel = [panels objectAtIndex:i];

    [contentView addSubview:panel];

    // add constraints to position each panel
}

these are run on a separate thread which is what I believe the issue is. The UI can only be updated on the main thread.

I tried adding:

dispatch_async(dispatch_get_main_queue(), ^{
    [contentView addSubview:panel];
});

But that raises errors for the constraints (the constraints don't have reference to it since its in a different thread).

If I run createPanels on the main thread, the panels will display but it also locks up the UI until its complete.

Any ideas?

Padin215
  • 7,444
  • 13
  • 65
  • 103
  • How many panels are you adding that it's overwhelming the main thread? Maybe a UITableView and cell would be better than "panel" views because they are lazy loaded? – Jason Harwig Feb 28 '13 at 20:11
  • table view would be nice, but they are 2 columns in portrait and 3 columns in landscape. We want to be able to use as much as the screen as we can – Padin215 Feb 28 '13 at 20:40
  • Have you tried adding the constraints inside the same block as `[contentView addSubView:panel]`? – Mike D Feb 28 '13 at 20:43
  • The constraints are not the issue, they are not being displayed/showed. When i look at the list of panels, their frame show correct points and sizes. They are where they should be in the contentView. – Padin215 Feb 28 '13 at 20:54
  • 1
    "they are 2 columns in portrait and 3 columns in landscape" - what about using UICollectionView? It's more flexible than table view but still gives you all the lazy loading/reuse benefits. – jsd Feb 28 '13 at 22:31
  • @jsd: Thanks for the suggestion, I'm testing it out. Running into performance issues from loading the panel on demand with it though, seems my panel isn't very optimal: http://stackoverflow.com/questions/15163693/creating-a-uicollectionviewcell-with-uiimage-and-uilabels-creates-slow-scrolling – Padin215 Mar 01 '13 at 17:55
  • I did end up converting it to UICollectionView instead of my own creation. – Padin215 Mar 05 '13 at 19:58

1 Answers1

0

I'm not sure which constraints are violated, but you could also try:

[contentView performSelctorOnMainThread:@selector(addSubview) withObject:panel waitUntilDone:YES];

instead of

dispatch_async(dispatch_get_main_queue(), ^{
    [contentView addSubview:panel];
});

Not sure whether this will help.

Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40