2

I have a tab bar application with three tabs. In my second tab view i have a UIProgressView. I have a NSTimer in my viewDidLoad method which calls a method which updates the progressView

progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];

All is fine so far.

I want to know how do i run the timer in the background throughout the lifecycle of the app, so that progressView is updated no matter which view the user is in. It will be great if you can show me with a code snippet.

Thanks in advance.

Neelesh
  • 3,673
  • 8
  • 47
  • 78

3 Answers3

1

It's not advisable to update UI elements from background threads. Also, it's not advisable to modify UI elements of a view when the user is not in that view. You are using precious system resources.

Better way is to update the ProgressBar as soon as that view becomes active...

UPDATE: You can see here to know how to run a task in background thread. You could set up your NSTimer to start in the selector you specify there. But again, this kind of stuff could lead to some weird bugs. Better to avoid..

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • Thanks. But I am just trying to simulate a concept here. This will be just for a demo and will not submit the app to appstore. – Neelesh Aug 10 '11 at 07:46
  • As an addition to this answer: setup your timer in `viewWillAppear` and stop/release it in `viewWillDisappear` methods of your viewController. `NSTimer` retains its delegate and if you don't do that, your viewController can never be released. – Evgeny Shurakov Aug 10 '11 at 07:47
  • what is the concept you trying to simulate? – Srikar Appalaraju Aug 10 '11 at 07:47
  • @srikar i am trying to show the progress of a system. ahhhh..ok..Now i get it. I need not update the progressView from other view. I can do that in the `viewWillAppear` method also. But how do i keep running the timer in the background so that the `count` will keep on increasing over time – Neelesh Aug 10 '11 at 07:53
  • @srikar : can you please help me ? – Neelesh Aug 10 '11 at 08:04
  • @Neelesh, you need to see my update in the answer. What you essentially need to do is to start a background thread, start your NSTimer there & keep the thread & timer running.This should get what you want... – Srikar Appalaraju Aug 10 '11 at 08:07
  • I followed this http://stackoverflow.com/questions/1421817/iphone-sdkcall-a-function-in-the-background and i guess it works now. thanks – Neelesh Aug 10 '11 at 08:26
  • Cool! if any answer actually helped you solve your problem, go the extra step & upvote this answer & mark it as selected answer. It'll help the community...thanks. – Srikar Appalaraju Aug 10 '11 at 08:28
0

u can add the method in background this way

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
// increment the progress here and keep the property of that count in appdelegate 
//
dispatch_async( dispatch_get_main_queue(), ^{
    // Add code here to update the UI/send notifications based on the
    // results of the background processing
});

});

in diffent view's viewWillAppear u can set this way: [loadingProgressView setProgress: appDelegate.count];

Banker Mittal
  • 1,918
  • 14
  • 26
0

You could keep a property in the ProgressViewController (or even in the app delegate), say a float that tracks the progress. When you need to update the progress, another ViewController can change the value of the property.

Once the view becomes visible, update the UI (UIProgressView) from viewWillAppear:.

Mundi
  • 79,884
  • 17
  • 117
  • 140