1

I am implementing long running task for location update send to server. I am using NSTimer to call method after every 60 seconds. I read many posts they are using following code .

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            self.timerLocationUpdateToServer = [NSTimer timerWithTimeInterval:interval
                                                                       target:self
                                                                     selector:@selector(retryLocationUpdateToServer)
                                                                     userInfo:nil
                                                                      repeats:YES];

            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSRunLoop mainRunLoop] addTimer:self.timerLocationUpdateToServer forMode:NSDefaultRunLoopMode];

            });
        });

Do we really need to use dispatch_async ? if yes then Why ?

Some one used two identifier one for timer and other for task.

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;

    }];

    bgTask1 = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask1];
        bgTask1 = UIBackgroundTaskInvalid;

    }];

Do we need to initialize two UIBackgroundTaksIndentifier or one will solve the purpose?

Thanks

user2019279
  • 163
  • 2
  • 7

1 Answers1

1

Your dispatch_async() to the background queue is wholly pointless. Not only is it pointless, it's possibly unsafe (depending on what other code accesses the timerLocationUpdateToServer property and whether it's atomic). You should replace that entire first code block with

self.timerLocationUpdateToServer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                                    target:self
                                                                  selector:selector(retryLocationUpdateToServer)
                                                                  userInfo:nil
                                                                   repeats:YES];

Similarly in the second code block, you only need one background task identifier. Multiple will work, but it's actually more efficient to use only one if you can manage it (since beginning a background task isn't free).

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Kevin, Thanks for your help, Then can you please tell me why they use dispatch_async for long running background task ? see link http://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application . Please reply – user2019279 Jan 31 '13 at 08:18
  • Also if I am using above code, my retryLocationUpdateToServer method is not calling . If i am writing [[NSRunLoop mainRunLoop] addTimer:self.timerLocationUpdateToServer forMode:NSDefaultRunLoopMode]; then its calling. – user2019279 Jan 31 '13 at 16:55
  • @user2019279: Did you use `+scheduledTimerWith...` or just `+timerWith...`? The former schedules it on the current runloop for you (and I'm assuming you're already on the main thread). The latter doesn't, and requires you to schedule it yourself. – Lily Ballard Jan 31 '13 at 18:33