Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:19Length:2.3 hours
  • Overview
  • Transcript

4.3 Getting Events From a Backing Timer

While WKInterfaceTimer is very useful for displaying a functional timer to the end user, it is a little limited from a technical perspective. An important feature that is missing is the ability to trigger an event when the timer runs out. A common approach to get around this is to use a backing timer to actually fire the events.

4.3 Getting Events From a Backing Timer

As I mentioned in the end of the last lesson, the Interface Timer within WatchKit has some of its drawbacks in the fact that there really are no events that you can subscribe to. So once the timer gets all the way down to zero, there's really no events fired so that you know when that that has happened. So typically the work around for something like that is to incorporate another timer to kind of handle the events that need to get fired at certain intervals. Now while this may not be an ideal situation, it's unfortunately the situation that we have to deal with. So, what we're gonna do is we're gonna come to the top of our interface controller definition, and we're gonna add another property here. This is going to be a private variable. And we'll call this timer, and this is going to be of type timer, and we're going to make this also be optional. So let's go ahead and save this. So now what we need to do is we need to kind of integrate this into our work flow. And make it be the same kind of intervals as the interface timer that we have on the interface so we know when things are actually happening. So the first most logical place to put that would be in our start function. So let's come down here and right after we call our interface timer reset function. Let's go ahead and get our timer up and running. So we're going to use a function on timer called schedule timer and that's going to allow us to set a couple of things. Including the time interval, a target, a selector and some more pieces of information. So I'll show you what all of these are for, these are the ones that we're going to use for the time being. So we need to specify the time interval, well, we actually already have that. So we can use the interval. So that's pretty nice. Now we need a target. Now what the target means is what function or what object are we going to use to let know when something has actually happened or when our timer has actually fired. So the target is going to be our cell, so I want to be the class, I want to be the object that knows when something has actually happened. Now we need a selector, and a selector is just a fancy way to say what method or what function do I need to call on the target to let it know that the timer has actually fired. So we're going to use a selector here and we're going to use the pound sign and then within here, we're going to say selector. Then we need to specify the actual function, so we are in the reminder interface controller. And the function that we're going to use, or that we're ultimately going to create is going to be timerEnded and that function is going to take in an instance of a timer. So now we have that, now the user info is really nothing we need to worry about at this point in time. And repeats is going to be false, so repeats is asking whether or not this scheduled timer should continue to run even after it has fired and in this case, we don't want it to. Once it stops, we want to know when it has stopped and then we're gonna stay there. And I believe this is going to say it once to unwrap that interval again, which is fine. Okay, so now we're getting a little closer. Now we have this time and it's running but now we need to handle this timer and its situation. So we need to add that function in. So let's come down here and once again, we are going to create a function. This is going to be timerEnded and it's going to take in a timer that's going to pass in that instance of timer itself. And now what do we wanna do at this point? Well, we want to stop the interface timer that is on the user interface. I think that's a good first points, so let's go ahead and stop that interface timer cuz we don't wanna do it anything else. Then we also want to use this timer and we want to invalidate it, so basically it stops any other event from firing on this guy. Removes it from its rumble bush, which basically it means it's gonna seat there and do nothing because at this point we don't want it to do anything. Then we are also going to change the state to be equal to StageDone, cuz this at this point, the stage is completed, the timer has fired. And then we can change the Action button Using the set title function to the next stage. Because at this point we're assuming that there's going to be another stage, but we'll put a check in there for that. So now what we've done is we have added in a new timer that's kind of a backing timer for our interface timer. And it's going to be running at the same interval, and then when it's done, it's actually going to call a selector function to let us know when things have finished. We're gonna know that it's actually finished because we should see the change In the action button. So let's go ahead and save all of this. Let's re-run our application. Yes, we do want to stop this debugging session. And we are also going to at some point want to add in a little bit more of a notification to the end user. And by that, I mean we want the end user to know when the stage is completed but we don't want them to have to be constantly looking at their watch. So once I pull up the morning wake up, I can hit this button, it's going to start counting down. And if everything is wired up correctly once it gets down to zero, we should see this button text change To next stage. And as I mentioned before, we're gonna wanna be able to give them some sort of notification. And we'll talk about that in an upcoming lesson, but it's kind of interesting how you can handle that with the Apple Watch. So now as you can see as I got down to zero, we switched to next stage, which means we were able to fire this timer ended function to let us know when we were done with that particular stage. So now we have been able to successfully start our timer, have it start counting down all via the start function. When the timer has ended we have transitioned into the stage done state and changed our action button title to Next Stage. Now in the next lesson we need to transition from here. Into the next stage if there is one to transition into and continue along the different states of our application.

Back to the top