I am working on iOS application in swift. I need to parse two web api(XML) at application launch time and during this period, I need to show launch screen. So I sent a synchronous request to parse the data from server. If net connection is good then application working fine but due to slow net connection or it takes more then 20 seconds to load data from server it can quit automatic. How to fix this issue. Please suggest it.
Asked
Active
Viewed 121 times
-2
-
3Use an *asynchronous* request. // Your app quits because a synchronous request blocks the main thread, and if it goes for too long then iOS kills the app. – Eric Aya Dec 02 '15 at 13:44
-
Just call your api in application's `didFinishLaunchingWithOptions` in appdelegate. – Saqib Omer Dec 02 '15 at 13:46
-
Any other option to show launch screen with asynchronous request. – Vipulk617 Dec 02 '15 at 13:46
-
I have already parsed data in didFinishLaunchingWithOptions – Vipulk617 Dec 02 '15 at 13:47
-
You can add delay functions like sleep(20.0) in didFinishLaunchingWithOptions. But this is not a good way to do this. If you are calling a asynchronous method you should do it properly by try-catch and reload view once api call returns data. – Saqib Omer Dec 02 '15 at 13:52
-
@vipulk617 No, `sleep` will only block the main thread for a given time, thus *also* blocking your connection and your app. **Never** use this to attempt to delay a result. – Eric Aya Dec 02 '15 at 14:14
-
downvoted - 1000th times discussed on SO & really bad idea – Daij-Djan Dec 02 '15 at 14:15
-
1Use some proper asyncrhonous request. The cocoa framework is quite helpful here. Do NOT use any sleep() calls. – Hermann Klecker Dec 02 '15 at 14:19
-
@Eric D for helping me. – Vipulk617 Dec 02 '15 at 14:21
-
See this answer: http://stackoverflow.com/questions/24176362/nsurlconnection-using-ios-swift – Hermann Klecker Dec 02 '15 at 14:22
-
@HermannKlecker thanks for giving valuable time for me. – Vipulk617 Dec 02 '15 at 14:23
-
@SaqibOmer NO - NEVER sleep on the main thread. – rmaddy Dec 02 '15 at 15:16
1 Answers
5
You should never send synchronous requests in main thread!
Add new VC on load. There you can load your data async. When data is ready, pass it the next VC.

Timur Bernikovich
- 5,660
- 4
- 45
- 58