I’m pretty sure that most beginning programmers are intimidated by the term “loop”. After all, a loop in real life could mean a number of different things that bear little similarity to the concept of loop in computer programming. I was recently talking with an aspiring iOS developer that was struggling with how a button is linked to Swift code in iOS.
As we continued the discussion, I realized that what was clear to me from experience writing code wasn’t clear to the person I was talking to. What is happening in the background when you load an app? Well, quite a bit, actually. And it isn’t really stated clearly off that bat when you jump into Xcode and iOS development. Sure, it’s probably in the Apple documentation somewhere, but it’s not spelled out for you.
The main event loop
You’ve probably been told that computer code is just a bunch of instructions that tells the computer what to do. The computer executes your commands to a tee. By that logic, the computer should run your commands until it has no more commands to execute, and stop. Right?
Well, yes. But remember Apple did some stuff in the background that you can’t see. So if you just write a couple of print statements in viewDidLoad (the first function to run in your app) in your app’s only UIViewController (the thing that shows the app’s view on the phone’s screen), what exactly is your app doing when you run it? How is it still running and showing something on the screen after the code you wrote has been executed?
The answer is that your app, at it’s most basic function, is a loop. By loop I mean it goes in a circle, doing the same things over and over. Specifically, it does two things in this loop. It checks for an “event” – a good example is whether or not the user tapped a button. The second thing is it reacts to an event if one happened. A good example would be to play a sound in reaction to a button being tapped. If no event happened, it checks again. And again and again, in a loop. That’s what your app is. In my head, it looks a little something like this:
Apple does in fact have official documentation on this subject, it’s really old and in their archive but doesn’t seem to have been replaced by anything new. So as of this writing at the end of 2021, Apple’s explanation of the iOS app main event loop is here, I thought it would be worth mentioning:
Your app’s job: react to events
Back to computers executing commands one by one – yes that is how they work. A computer program that works in this fashion is loosely called a “script”, but there’s no hard definition of what that actually means. The important thing to remember about the difference between your app and other types of computer programs is that it’s waiting for input from the user, who is (hopefully) the owner of the phone your app is running on. Your app’s job is to react to input from your user.
So how does the iPhone know what code to run when an event happens? It knows because you tell it. You attach a function to a button, which is just a bundle of code. That function will run when the button is pressed. The cool part is iOS does all the heavy lifting for your once you make the connection. You don’t need to know about how messages are carried in the event loop or what’s going on behind the curtains, just connect your button and code and away you go! It almost feels like you’re cheating somehow.