Performing an asynchronous function?

in my iOS application, I do the following.

viewDidAppear(){ // Load a spinner in a view on the top [DSBezelActivityView newActivityViewForView:self.view]; // Execute code that require 3 seconds ... // Stop the spinner [DSBezelActivityView removeViewAnimated:YES]; } 

The problem is that the counter does not appear because the processor is working a lot (something similar). Thus, the betweek code to start and stop takes precedence when rendering the view.

I would love to find a way to effectively demonstrate the start of a counter without using a timer to delay code execution.

thanks

+7
source share
3 answers

If you have a method like

 -(void) showSpinner:(UIView*)view { dispatch_async(dispatch_get_main_queue(), ^{ [DSBezelActivityView newActivityViewForView:view]; }); } 

There are several ways to call it from another thread. Choose one of the following options:

 [NSThread detachNewThreadSelector:@selector(showSpinner:) toTarget:self withObject:self.view]; // or [self performSelectorInBackground:@selector(showSpinner:) withObject:self.view]; // or NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSpinner:) object:self.view]; NSOperationQueue *opQueue = [[NSOperationQueue alloc] init]; [opQueue addOperation:invOperation]; // or dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self showSpinner:self.view]; }); 

Alt + click for more details.

+22
source

Move the code between the start and stop activity indicator to a separate thread, since it blocks the main thread. This is why the activity indicator is not displayed.

Edit: Example

+1
source

I agree with the 1st answer with a few modifications. I just experienced this same problem. The problem is that any graphic image automatically switches to updating the background when you have code that takes time to complete. Throwing the counter into the background is what it does in essence. What you want (unfortunately) is for you the main code to run in the background and the counter to run in the foreground. I know this sounds bad, but in some cases, letting your code run a little slower to show that the application is doing something useful, useful to the user.

To make the counter work: 1) Take all the code, which takes 3 seconds to run, and put it in the function, which is the void function 2) Create an instance of your counter, but save it in a variable accessible outside your viewDidAppear procedure. 3) Launch the new NSTimer, which runs continuously in increments of approximately every quarter second or so. I will determine what goes into the procedure, which is called every cycle later. 4) Call the procedure created in step 1 using the performSelectorInBackground function. This basically means that you start your run (for 3 seconds) in the background, which is actually the only way that animated counters can display and really come to life. 5) In the routine that you created in step 1, add a line of code at the top right that updates the object (global object) to true, indicating that we are in the middle of our main 3-second procedure. 6) At the end of the procedure defined in step 1, add a line of code that sets the same global one, defined in step 5, to false, indicating that our 3-second procedure has completed. 7) In the timer procedure, now we want to do something similar to the following:

 // If busy that start the spinner if(YES == busy){ [spinner startAnimating]; }else{ [spinner stopAnimating]; // Here we can also stop and deallocate the timer } 

If you need further help on this, I can really provide the exact code. Take a look at an example application that I developed for the Pepperdine News Group. When you press the button, the spinner appears in the upper right corner of the screen.

http://itunes.apple.com/us/app/pepperdine-graphic-for-iphone/id516343215?mt=8

0
source

All Articles