AlamoFire and its asynchronies communications

I understand that AlamoFire and URL communications with a cloud database is async. My question is how do most people handle the couple second delay of communications. My app stores the users email address and a bool value of true if they are logged in. If true it jumps to the second viewcontroller with detailed info. Because of the delay there is no info. I put a 2 second delay but it looks kludgy on the transitions from view one to two. Attached is the code. I'm also thinking a wait screen but not sure. Any help would be great. Thanks.

A good way to think about networking code is to assume that every network request takes 30 seconds to complete. This isn’t just a theoretical idea. Requests really can take that long in the real world.

When you start thinking this way, you uncover all the places in your app where the UI isn’t aligned with the reality of networking. For example, in your situation, you just can’t assume that you’ll be able to verify the user’s credentials in 2 seconds, so your UI will need to accommodate that. For example:

  • You might keep the user on the first view controller with a UI that says ‘logging in’ until your check is complete.

  • You might transition to the second view controller but have it display a placeholder until the check is complete.

Either way, think about the UI first, and then write the code to match.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

3 Likes

That's a good way to think about it. I would suggest also that you break up that code chunk into a couple functions.

You have a login chunk of code, a get league chunk of code, a navigate to screen chunk of code.

You should probably break those chunks out into distinct functions with completion handlers. You could also probably simplify this code by using Codable to get the data instead of using strings to get the data out of the json responses.

1 Like

Thanks I was working off of my laptop. I am going to func each for ease and call different reads instead of them all. I figured a separate window as a progression would be the fix. First time calling the cloud and was not to sure. Thanks again.

I thought the case, thanks. First time talking to the cloud just checking on how others handle it. I'll put a progression bar on the screen until I have info in selected var's. Thanks again.

I use SVProgressHUD to let the user know that stuff is loading. You could also use UIApplication.isNetworkActivityIndicatorVisible (although Mr google says it's deprecated as of iOS 13 although Apple's prodigious docs don't say what replaces it) to show the gear in the status bar.

Likely nothing, as it doesn't exist on iPhone X-class devices or the latest iPad Pros. Plus, with the amount of network traffic modern apps may have, showing it has become less and less useful.