Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

cocoa touch - Does UIActivityIndicator require manual threading on iPhone

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.

The operation is performed entirely in one event call... so there is no chance for the UI framework to receive control to actually display and animate this indicator.

The sample apps which use the UIActivityIndicator (or any other similar animation) start and stop the animation in different events, triggered separately at different stages of the program.

Do I need to manually create a separate thread to run my operation in, or is there already default support for this kind of behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...