0

The following is the code I am using to call 5 different services to get response and store it in my json text file.when the background service call is running my app is getting hang, no touch event is working.after completion of background service call its working.i don't want to hang my app when calling background service how can i do this.

[NSTimer scheduledTimerWithTimeInterval:60.0 target:self
                      selector:@selector(checkAlert) userInfo:nil repeats:YES];
Vizllx
  • 9,135
  • 1
  • 41
  • 79
sairam
  • 25
  • 10
  • Use different thread instead of Main to call background services. Main thread is responsible for all UI interface and user interaction. So don't block your Main thread. – Vijay Masiwal May 14 '15 at 10:14

2 Answers2

2

you can use Grand Central Dispatch (GCD) in this you can write the code to do some processing on a background thread and then do something with the results in the main run loop is incredibly easy and compact:

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   // Add code here to do background processing
   //
   //
dispatch_async( dispatch_get_main_queue(), ^{
    // Add code here to update the UI/send notifications based on the
    // results of the background processing
   });
});

If want to add delay then can also use

// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
   // call your method
});
Shruti
  • 1,849
  • 1
  • 13
  • 21
  • ... and the delayed timing? Care to suggest how that is acheived? – trojanfoe May 14 '15 at 10:21
  • 1
    https://developer.apple.com/library/prerelease/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_after.... if you want to add delay... use `dispatch_after` – Shruti May 14 '15 at 10:26
  • I think that would be useful to add to your answer. – trojanfoe May 14 '15 at 10:26
  • // Delay execution of my block for 10 seconds. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // call your method }); its calling but same problem ui is hang – sairam May 14 '15 at 10:32
  • may be try to increase the delay and check – Shruti May 14 '15 at 10:33
  • what doubt? tell me may be i can help – Shruti May 14 '15 at 10:40
  • from my dashboard view controller I'm calling background service for every 5mins.and again I'm going to some other view controller from there also I'm calling some url's.what i want to know is if i run background service from dashboard is it affect to when i go to some other view controller.bcz some other url's I'm calling from this view controller. – sairam May 14 '15 at 10:45
  • yes i think so.. till your dashboard is in navigation stack it will run the services in background. – Shruti May 14 '15 at 10:51
  • is there any possibility to achieve this. – sairam May 14 '15 at 10:53
  • I think you can suspend this operation in `viewDidDiappear` .. check this link for reference.. http://stackoverflow.com/questions/6928752/how-to-stop-the-execution-of-tasks-in-a-dispatch-queue – Shruti May 14 '15 at 10:55
  • @SairamGogikar your welocme... :) – Shruti May 14 '15 at 11:00
0

You are actually not calling it in background that is reason your UI is getting freeze.

dispatch_async( dispatch_get_global_queue(0, 0), ^{
  [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAlert) userInfo:nil repeats:YES];
});

But keep in note , if you need to Update UI then do it in main thread otherwise UI related task will not be updated.

Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Using GCD **and** `NSTimer`? Why? Does this code actually call `checkAlert` in a background thread? – trojanfoe May 14 '15 at 10:19
  • can u send me please GCD Exeample – sairam May 14 '15 at 10:21
  • @Sairam Gogikar if you have any issue with this then post here, otherwise I am not available for any other clarification to other users. – Vizllx May 14 '15 at 10:23
  • @SairamGogikar You can surely check this link for GCD clarification http://stackoverflow.com/questions/13079963/how-to-send-a-message-or-notification-to-main-thread-in-x-code/13080519#13080519 – Vizllx May 14 '15 at 10:26
  • [NSTimer scheduledTimerWithTimeInterva this code in dashboard view controller it will call every one minute but when i will go another view controller in that view controller i am calling another services it will effect on this services – sairam May 14 '15 at 10:26
  • If your dashboard view controller is alive in the app, i mean until and unless it is not popped from navigation stack, it will keep on calling the method in background. – Vizllx May 14 '15 at 10:28
  • but some time not calling (checkAlert) methode – sairam May 14 '15 at 10:35