0

I'm using performSelectorInBackground in download and parse some xml. Once the xml is parsed I call performSelectorOnMainThread to build my UI based on values in the xml. The build UI function does get called but nothing gets put onto the screen. It should be o.k to call the main thread from a background thread should it not?

If I don't perform the xml parsing on a background thread the UI add just fine.

-(id) myCustomInit:(Data *) data;

{
    if ( self= [super init]) {
            baseURL=data.url;
    [self performSelectorInBackground:@selector(getXML) withObject:nil];

    }


    return self;

}


-(void) getXML
{
    // Set up a pool for the background task.

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // XML parsing code removed for brevity



    [self performSelectorOnMainThread:@selector(handleUIOnXMLComplete) withObject:nil waitUntilDone:YES];

    [pool release];


}

-(void)handleUIOnXMLComplete
{

// add buttons etc to view

    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:button];

}

Edit

This is the code in the parent UIViewController Thats actually adds the view

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NewViewData *rld=nil;

    rld=[dataArray objectAtIndex:indexPath.row];
    NewView *controller = [[NewView alloc] myCustomInit:rld ];


    controller.title = rld.Title;
    controller.view.backgroundColor = [UIColor blackColor];

    [table.navigationController pushViewController:controller animated:YES];
    //[table.tableView reloadData];

}
18
  • What kind of updates do you do in handleUIOnXMLComplete? You might need to call setNeedsDisplay or setNeedsLayout in that method to redraw the view.
    – Seyther
    Mar 7, 2011 at 10:45
  • I pretty much just create a bunch of buttons and sliders and add them to the view. I tried adding setNeedsDisplay & setNeedsLayout at the end of my updateUI function but I have the same problem
    – dubbeat
    Mar 7, 2011 at 10:49
  • Your two lines in init before 'return self' need to go inside your if block btw.
    – occulus
    Mar 7, 2011 at 10:54
  • handleUIOnXMLComplete deffinately gets called. It's just that the UI won't update
    – dubbeat
    Mar 7, 2011 at 10:56
  • If you haven't already, I suggest either debugging (or lots of NSLogging) inside your handleUIOnXMLComplete method to ensure it is going through the exact code path you think it is.
    – occulus
    Mar 7, 2011 at 10:58

1 Answer 1

0

Triggering a call to a method on the main thread, from a background thread, is allowed and fine.

setNeedsLayout and setNeedsDisplay shouldn't need calling -- when you add a subView to a view, the display of the newly added item is triggered anyway.

What happens if you replace your entire handleUIOnXMLComplete method with the following?

-(void)handleUIOnXMLComplete
{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    label.text = @"XXXXXXX YYYYYYYY";
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
}

I did a quick reproduction, as far as I could, of your situation and the above code does work -- I get to see the "XXXXXXX YYYYYYYY" label.

I'd avoid setting a whole new self.view by the way -- it's preferable to modify the existing self.view.

By the way, in the code you posted, you're not setting a frame on your custom UIButton! So do something like button.frame = CGRectMake(0.0, 0.0, 160.0, 160.0); after you make your button and before you add it to the view.

8
  • well thats weird. I don't see a label
    – dubbeat
    Mar 7, 2011 at 11:46
  • And the view you're adding stuff to is definitely the currently showing view in the nav controller? And there's no weirdness like some layer on top of the view obscuring anything? addSubview will put your new subview on top of any other subviews, so normally you'll see the thing you've just added on top of the other stuff.
    – occulus
    Mar 7, 2011 at 11:50
  • I edited my question to explain with example how I am trying to add the view.
    – dubbeat
    Mar 7, 2011 at 12:01
  • If you're still completely perplexed by the behaviour, it might be time for a "make it and break it" approach. Start a new app from one of the samples (e.g. the nav bar sample might be good). Make it do the thing which you're having trouble with and not much else (so do the background threading bit, which adds ui items to a uiview on the main thread). Then start bolting on bits of the real code, bit by bit, to see at which point you break it.
    – occulus
    Mar 7, 2011 at 15:06
  • @occulus I found out what the problem was. If I put the following line in the custom init function and not the update UI function it works. self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; For some reason it seems that the view needs to be created right off the bat.
    – dubbeat
    Mar 7, 2011 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.