Can anyone tell me how to implement background execution in iOS .like i have to call a Webservice in background so it doesn't interrpt the main UI.
Asked
Active
Viewed 1,053 times
-1
-
Can you please tell how are you calling your web service? Are you using maybe AFNetworking? Some code for example. – Flipper May 19 '16 at 10:13
4 Answers
0
use following code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Do background work
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI
});
});

Parth Bhuva
- 857
- 6
- 26
0
I suggest you can write a service class with completion block (closure) that call your webservice. And then use GCD dispatch_async to call your service. So that the function call will be run in a background instead of main thread.
And update the UI in the completion block.
For GCD tutorial, you can refer to https://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

PrimaryChicken
- 963
- 1
- 8
- 29
0
You can use this code:
dispatch_async(backgroundQueue, ^{
int result = <some really long calculation that takes seconds to complete>;
dispatch_async(dispatch_get_main_queue(), ^{
[self updateMyUIWithResult:result];
});
});
}

neha
- 167
- 2
- 11
0
Here is a simple solution for using background thread and main thread.
Hope it helps you.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),^{
//do your web service calls to not to disturb main thread.
dispatch_async(dispatch_get_main_queue(), ^{
//here you can do what you want with main thread
//once you got data you can get it on to main thread and execute the operation
});
});

Santo
- 1,611
- 3
- 17
- 37