Adding subview, gets delayed?

2019-09-06 06:44发布

问题:

i didn't really know how to title this question, but here's a thing that really kills me: In my app i have a UITableView, UISegmentedControl and UINavigationBar. Once UISegmentedControl gets one of its segments selected i want to show a UIActivityIndicatorView on the UINavigationBar and then parse an xml file and present the results in a table. Everything works almost as i want it to, except one thing, the activity indicator view gets added to the uinavigationbar after the parser finishes, even though the method showLoading that adds UIIndicatorView to UINavigationBar gets before parser is initialised. Can anyone explain it? is there something i might be missing? maybe the ui needs to get redrawn? thanks peter

回答1:

It looks that you parse your xml in main thread and so it becomes blocked for UI changes. Try to move xml parsing to separate thread (e.g. by calling your parsing method via -performSelectorInBackground:)

Edit: Actually you're (almost certainly) using autorelease implicitly in your application - as many standard functions return autoreleased objects. When you're running your functions on separate thread you need to create NSAutoreleasePool object there to handle autoreleased objects and avoid memory leaks (see Autorelease Pools in docs). So your parseXML function must look like:

- (void)parseXML{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   ... //xml parsing routines etc
   [pool release];
}