NSThread causing UIView to freeze.

Discussion in 'iOS Programming' started by TrevorB, Dec 7, 2009.

  1. TrevorB macrumors newbie

    Nov 28, 2009
    Hey there.. I've only been at this for a week, plugging a way so far, but hit a glitch where I thought I should ask for some help.

    I'm trying to animate an object on a UIView. I've decided to do this by subclassing NSThread, and having that pass a message back to my View Contoller after a delayed interval.

    This appears to work OK, the thread is launched, the messages are passed back and forth, except that the UIView doesn't appear to update until the Thread's main method is complete. I can see the methods being called proplerly (NSLog's work as I think they should)... I'd rather have the threads run concurrently so that one can update the other.

    Any ideas what I might be doing wrong?

    Code Snippets:
    - (void) main {
    	NSAutoreleasePool *autorelease = [[NSAutoreleasePool alloc] init];
    	NSLog(@"Thread Started");
    	[NSThread sleepForTimeInterval:2.0];
    	NSLog(@"Thread Still here...");
    	[viewController threadMessage];
    	[NSThread sleepForTimeInterval:10.0];
    	NSLog(@"Thread Still here2...");
    	[autorelease release];
    - (void) setParent:(id)sender {
    	viewController = sender;
    	NSLog(@"View Contoller set");
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        // Override point for customization after app launch    
        [window addSubview:viewController.view];
        [window makeKeyAndVisible];
    	TestThread *myThread = [[TestThread alloc] init];
    	[myThread setParent:viewController];
    	[myThread start];
    - (void)threadMessage {
    	CGPoint touchPoint;
    	touchPoint.x = 20;
    	touchPoint.y = 20;
    	target.center = touchPoint;
    	yLabel.text = @"New Message";
    	NSLog(@"ThreadMessage finished");
    The problem is that I see "ThreadMessage finished" in the console, but the animations produced by "target.center = touchPoint" and "yLabel.text..." don't show up until after the 10 second delay produced by TestThread.m's " [NSThread sleepForTimeInterval:10.0];"

    Any help would be appreciated!
  2. TrevorB thread starter macrumors newbie

    Nov 28, 2009
  3. icewing macrumors 6502

    Jun 19, 2008
    St Louis
    Thanks for doing such a great follow up! That's bound to be very helpful for someone in the future. I wish more people took the time to be that detailed.

Share This Page