Does UIActivityIndicator require manual threading on iPhone

前端 未结 5 754
滥情空心
滥情空心 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:29

    Yes, you need to put your operation in a separate thread while the UIActivityIndicatorView calls remain on the main thread.

    Here's why: If you start animating your indicator view then immediately begin your process, your process will block until finished, and the user will never see any "activity."

    Instead, start animating the indicator, then pop a new thread for your process. Use notifications, a delegate pattern, or performSelectorOnMainThread:withObject:waitUntilDone: to let the main thread know that the process is finished. Then stop the animation and continue.

    This will ensure that the user knows something is happening while your process does its thing.

    0 讨论(0)
  • 2020-12-13 03:42

    An UIProgressView (the progress bar) can not run by itself, you need to periodically increment its value.

    An UIActivityIndicatorView (the spinning gear) can run by itself: just call startAnimating when you start your operation and stopAnimating when you're done.

    So an UIActivityIndicatorView may be what you're looking for.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-13 03:53

    Starting a new thread can be overkilling and a source of complexity if you want to do things that are supposed to start on the main thread.

    In my own code, I need to start a MailComposer by pushing a button but it can take some time to appear and I want to make sure the UIActivityIndicator is spinning meanwhile.

    This is what I do :

    -(void)submit_Clicked:(id)event
    {
        [self.spinner startAnimating];
        [self performSelector:@selector(displayComposerSheet) 
                   withObject:nil afterDelay:0];    
    }
    

    It will queue displayComposerSheet instead of executing it straight away. Enough for the spinner to start animating !

    0 讨论(0)
  • 2020-12-13 03:56

    Which is easier:

    1> Have 1 method that starts the spinner (in the background).
    2> Run your long-running code right here.
    3> Have 1 method that ends the spinner.
    

    or....

    A> Have 1 method that starts the spinner.
    B> Have 20 methods (1 for each thing you want to do in the background)
    C> Have 1 method that ends the spinner.
    

    I always see everyone suggest A,B,C... the hard way.

    0 讨论(0)
提交回复
热议问题