It seems that in my iOS app (which I am writing in Xcode with Swift), functions that run after a button is pressed all run at once without regard to any sleep functions in between them.
Here is some background:
In order to create a line of communication between my iPhone app and my Raspberry Pi, I am using json storage bins (https://jsonstorage.net/). The way the app works is it updates a json bin created at the site above and sends an email as a text message to the Raspberry Pi (the Raspberry Pi uses an Adafruit FONA to handle text messages), and this triggers the Pi to gather data from the json bin. The json bin will have been updated with a string, which tells the Pi what to do. Upon receiving this string, the Pi does something and adds information to a different json bin. Then, I access this bin in my app and use it to update certain variables.
Here is the code where the functions run (a button is pressed, which pushes to a new view controller; here is the viewDidLoad()
function for that new VC):
override func viewDidLoad() {
super.viewDidLoad()
uploadJsonData(dataString:"varsData")
sendEmail(body:"app varsData")
sleep(15)
collectData()
}
Conceptually, this code should work. If I have a button that, when pressed by the user, runs the uploadJsonData()
and sendEmail()
functions, then the user waits 15 seconds (note that if I have a sleep(15)
as part of the first button, this won't work), then the user presses another button that runs the collectData()
function, then the code will work as expected, and the variables will be updated with new data immediately after that second button is pressed. However, this is not the case with the code above: instead, the variables are updated with the data from before the second json bin is updated, and the second json bin itself doesn't update until at least 15 seconds after all of the functions run, regardless of the length of the sleep function (implying that the sleep doesn't do anything, and everything runs at once, or at least the interactions with the internet all occur at once). Does anyone have ideas regarding why this is/how to fix it?