If you use dataTaskWithURL , your intention might be getting a data from server and display the data to your UI. But, sometimes it makes a problem that the resulted data is nil.
To solve this issue, first of all you must recognize that
dataTaskWithURL runs asynchronously. Conversely UI stuffs runs on the main thread on iOS. So the time that you get a data from the server might not be synced with the time you display the data to your UI. So in this case you might see a strange nil value on it.
You can simply think that I can run it sychronously. But, it would cause a bad user experience. So here is a solution which is creating another completionHandler to run it asynchronously. And then if you want to update your UI , remember that the task should be taken on main queue.