Does UIActivityIndicator require manual threading on iPhone

前端 未结 5 756
滥情空心
滥情空心 2020-12-13 02:56

I am running creating an iPhone application which performs a costly operation and I wanted to create an activityIndicator to let the user know the application has not frozen

5条回答
  •  春和景丽
    2020-12-13 03:48

    I just found someone asking a very similar question with a good answer on the apple iPhone forums. https://devforums.apple.com/message/24220#24220 #Note: you have to have an appleID to view it.

    Essentially, yes, you do need to start your process running in a different thread, but its quite easy to do (as paraphrased from user 'eskimo1')

    - (IBAction)syncOnThreadAction:(id)sender
    {
        [self willStartJob];
    
        id myObject = [MyObjectClass createNewObject];
        [self performSelectorInBackground:
            @selector(inThreadStartDoJob:)
            withObject:myObject
        ];
    }
    
    - (void)inThreadStartDoJob:(id)theJobToDo
    {
        NSAutoreleasePool * pool;
        NSString *          status;
    
        pool = [[NSAutoreleasePool alloc] init];
        assert(pool != nil);
    
        status = [theJobToDo countGrainsOfSandOnBeach];
    
        [self performSelectorOnMainThread:
            @selector(didStopJobWithStatus:)
            withObject:status
            waitUntilDone:NO
        ];
    
        [pool drain];
    }
    

    where -willStartJob and -didStopJobWithStatus are my own methods that:

    • disable the UI, to prevent the user from kicking off two jobs at once
    • set up the activity indicator

提交回复
热议问题