Multi Threading issue with ActivityWeel
I got an issue with the MultiThreading thing. I got this piece of code(drawActivityWheel makes an UIActivityIndicatorView):
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector: @selector(drawActivityWheel) toTarget:self withObject:nil];
//Do some actions
[NSThread detachNewThreadSelector: @selector(removeActivityWheel) to开发者_C百科Target:self withObject:nil];
[pool release];
And it works, but i got many messages in console saying:
*** __NSAutoreleaseNoPool(): Object 0x758a210 of class __NSArrayM autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x6e111a0 of class UIView autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x6e183c0 of class UISegmentedControl autoreleased with no pool in place - just leaking
I read something that maybe i should use something like:
[self performSelectorOnMainThread:@selector(drawActivityWheel) withObject:nil waitUntilDone:NO]But nothing happens. The activity weel is not been showing. I believe that Im using threading in a bad way, please let me know how it really works!
Thanks for ur help buddies!
You needed to add the NSAutoreleasePool in the methods your detach.
-(void)drawActivityWheel {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// your threaded code here
[pool drain];
}
You need to implement your thread entry points like this:
- (void) drawActivityWheel {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//do work here
[pool drain];
}
- (void) removeActivityWheel {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//do work here
[pool drain];
}
Note that if your threads are long-lived, a slightly different pattern is recommended.
The answers already given cover the question as put adequately, I'd just add that performSelectorOnMainThread:withObject:waitUntilDone:
isn't just preferred for anything that talks to UIKit, it's mostly required since prior to iOS 4.0 UIKit wasn't threadsafe at all and even in 4.0+ it's only very selectively safe to call from other threads. It's not guaranteed to be safe to create a UIActivityIndicatorView
on a background thread, or to add it to another view. So what you're doing may crash randomly or on future versions of the OS, or result in any other piece of undefined behaviour.
For authority on that, see the introduction to UIKit, specifically:
Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.
If your attempts to call your method on the main thread appeared to be having no effect then it's likely your main thread is blocked. The selector you pass will be scheduled to be performed as soon as it can be, but it's just another thing on the list of things to be done on that thread. So if something else is currently blocking then it won't be done until that thing finishes, at which point you may well end up with draw and remove occurring all but atomically.
精彩评论