Timers Timer CPRE 388

Similar documents
User Interfaces. Lecture 15. Application Programming on Mac OS. Hamza Bennani September 4, 2018

Objective-C Runtime. Cocoa s Jewel in the Crown. NSConference Nicolas

Multitasking and Background Execution

ITP 342 Mobile App Dev. Fundamentals

AVAudioPlayer. avtouch Application

ITP 342 Mobile App Dev. Unit Testing

UI Design and Storyboarding

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

Mobile Application Development

Object-Oriented Programming with Objective-C. Lecture 2

About MSDOSX. Lecture 0

Objective-C and COCOA Applications

NSObject. - (NSString *)description Provides us with a string description of the object

Getting Help. iphone Application Programming Lecture 3: Foundation Classes. Data Structures in Objective C. Online Documentation.

C Blocks Block CPRE 388

ios Development Lecture 1 Introduction to Objective-C Ing. Simone Cirani

Using Swift with Cocoa and Objective-C

iphone Programming Touch, Sound, and More! Norman McEntire Founder Servin Flashlight CodeTour TouchCount CodeTour

ITP 342 Mobile App Dev. Fundamentals

Collections & Memory Management. Lecture 2

AdFalcon ios SDK Developer's Guide. AdFalcon Mobile Ad Network Product of Noqoush Mobile Media Group

Concurrency. CS 442: Mobile App Development Michael Saelee

COCOA WORKSHOP PART 1. Andreas Monitzer

Objective-C. Deck.m. Deck.h. Let s look at another class. This one represents a deck of cards. #import <Foundation/Foundation.h> #import "Deck.

Mobile Development - Lab 2

Introducing CloudKit. A how-to guide for icloud for your Apps. Frameworks #WWDC14. Session 208 Olivier Bonnet CloudKit Client Software

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

ios Performance and Concurrency Patrick Thomson

Create an App that will drop PushPins onto a map based on addresses that the user inputs.

Contents. iphone Training. Industry Trainers. Classroom Training Online Training ON-DEMAND Training. Read what you need

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

CS193p Spring 2010 Thursday, April 29, 2010

Chapter 5: CPU Scheduling

An ios Static Library for Service Discovery and Dynamic Procedure Calls

HPX Performance Counters HARTMUT KAISER (LSU), MACIEJ BRODOWICZ (IU)

Programming Language 2 (PL2)

Data Management

Discovering the ios Instruments Server

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Mobile Application Development

CIS233J Java Programming II. Threads

Mobile Application Development L14: Miscellaneous

Acollada ios Charting Components

CPU scheduling. Alternating sequence of CPU and I/O bursts. P a g e 31

ITP 342 Advanced Mobile App Dev. Memory

COM2007 Week 9 Java Concurrency and Alting

ios: Objective-C Primer

Review (Basic Objective-C)

Real Time Kernel v1.1

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

EE Embedded Systems Design. Lessons Exceptions - Resets and Interrupts

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CS 47. Beginning iphone Application Development

Animate Objects. Chapter Overview. Objectives of this Chapter. Animate Objects. Interactive Programming In Java Page 1

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

MANNING IN ACTION SAMPLE CHAPTER. Brendan G. Lim. WITH Jerry Cheung AND Jeremy McAnally

ITP 342 Mobile App Dev. Connections

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

Collections. Fall, Prof. Massimiliano "Max" Pala

The Operating System. Chapter 6

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

real-time kernel documentation

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Cisco StadiumVision Mobile API for Apple ios

Exam Concurrent and Real-Time Programming

Core Animation. Building Animated UI s. Bill Dudney. Gala Factory Software LLC. Bill Dudney Core Animation: Building Animated UI s Slide 1

CSE 230 Intermediate Programming in C and C++ Functions

DL/ID Parsing Component for ios

Fix Bugs Faster Using Activity Tracing

#include <tobii/tobii.h> char const* tobii_error_message( tobii_error_t error );

Extensions to Barrelfish Asynchronous C

12.1. Unit 12. Exceptions & Interrupts

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Module 12: I/O Systems

Assistive Application Programming Guide For macos 2.4

Introducing the New Development Tools

LarvaLight User Manual

AVAudioRecorder & System Sound Services

Threads SPL/2010 SPL/20 1

Monday, 1 November The ios System

Class object initialization block destructor Class object

Xcode 4 Cookbook. Steven F. Daniel. Chapter No. 2 "User Interfaces Creating the UI"

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Spring Stanford CS193p Spring 2011

Working Effectively with Objective-C on iphone OS. Blaine Garst Wizard of Runtimes

2d video game development with MacRuby

CS370 Operating Systems

EE4390 Microprocessors

Allocation Schedule GL-1048

Configuring WO Automation for Radio for TuneGenie

iphone Application Programming Lab 2: MVC and Delegation + A01 discussion

28 Using Logic Functions

ITP 342 Mobile App Development. Data Persistence

Chapter 6. Multiform Projects The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Module 12: I/O Systems

آنستیتیوت تکنالوجی معلوماتی و مخابراتی ICTI

Core Data Potpourri. Paul

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

ITP 342 Mobile App Dev. Connections

Principles of Programming Languages. Objective-C. Joris Kluivers

Transcription:

Timers CPRE 388

Timers A timer provides a way to perform a delayed action or a periodic action Thetimer waits until a certain time interval has elapsed and then fires, sending a specified message to a specified object

Timers and Run Loops NSRunLoop objects control loops that wait for input, and they use timers to help determine the maximum amount of time they should wait. When the timer s time limit has elapsed, the run loop fires the timer (causing its message to be sent), then checks for new input.

Timing Accuracy the effective resolution of the time interval for a timer is limited i to on the order of 50 100 milliseconds A repeating timer reschedules itself based on the scheduled firing time, not the actual firing time

Alternatives to Timers use performselector:withobject:afterdelay: and related methods to invoke a method directly on another object Some variants, such as performselectoronmainthread:withobject:wa ituntildone:, allow you to invoke the method on a different thread.

Using Timers When you create a timer, you must configure it so that it knows what message to send to what object when it fires Three ways to create a timer: scheduling a timer with the current run loop; creating a timer that you later register with a run loop; initializing a timer with a given fire date.

Timer Creation two ways to tell a timer what message it should send and the object to which it should send the message specifying i each independently d using an instance of NSInvocation (void) timerfiremethod:(nstimer*)thetimer

timer controller object that declares methods to start / stop four timers configured in different ways. properties for two of the timers and a timer count, and three timer related methods (timerfiremethod:, invocationmethod:, and countedtimerfiremethod:) also provides a method to supply a user info dictionary

@interface TimerController : NSObject { NSTimer *repeatingtimer; NSTimer *unregisteredtimer; NSUInteger timercount; t @property (assign) )NSTi NSTimer *repeatingtimer; @property (retain) NSTimer *unregisteredtimer; @property NSUIntegertimerCount; (IBAction) startoneofftimer:sender; (IBAction) startrepeatingtimer:sender; (IBAction) stoprepeatingtimer:sender;

(IBAction) createunregisteredtimer:sender; (IBAction) startunregisteredtimer:sender; d (IBAction) stopunregisteredtimer:sender; (IBAction) startfiredatetimer:sender; (void) timerfiremethod:(nstimer*)thetimer; (void) invocationmethod:(nsdate *)date; (void) countedtimerfiremethod:(nstimer*)thetime r; (NSDictionary *)userinfo; @end

(NSDictionary *) userinfo { return [NSDictionary dictionarywithobject:[nsdate date] forkey:@"startdate"]; (void) targetmethod:(nstimer*) thetimer { NSDate *startdate = [[thetimer userinfo] objectforkey:@"startdate"]; NSLog(@"Timer started on %@", startdate); (void) invocationmethod:(nsdate *)date { NSLog(@"Invocation for timer started on %@", date);

Scheduled Timers The following o two class methods automatically at register the new timer with the current NSRunLoop object in the default mode (NSDefaultRunLoopMode): scheduledtimerwithtimeinterval:invocation:repe dti t l i ti ats: scheduledtimerwithtimeinterval:target:selector: userinfo:repeats:

(IBAction)startOneOffTimer:sender { [NSTimer scheduledtimerwithtimeinterval:2.0 target:self selector:@selector(targetmethod:) userinfo:[self userinfo] repeats:no]; The timer is automatically fired by the run loop after 2 seconds, and is then removed from the run loop.

schedule a repeating timer, that again uses a selector: (IBAction) startrepeatingtimer:sender { NSTimer *timer = [NSTimer scheduledtimerwithtimeinterval:0.5 target:self selector:@selector(timerfiremethod:) userinfo:[self userinfo] repeats:yes]; self.repeatingtimer = timer;

Unscheduled Timers create timers that you may schedule at a later timeby sending themessage addtimer:formode: to an NSRunLoop object. timerwithtimeinterval:invocation:repeats: timerwithtimeinterval:target:selector:userinf o:repeats:

(IBAction) createunregisteredtimer:sender { NSMethodSignature *methodsignature = [self methodsignatureforselector:@selector(invocationmethod: )]; NSInvocation *invocation = [NSInvocation invocationwithmethodsignature:methodsignature]; [invocation settarget:self]; [invocation setselector:@selector(invocationmethod:)]; NSDate *startdate = [NSDate date]; [invocation setargument:&startdate atindex:2]; NSTimer *timer = [NSTimer timerwithtimeinterval:0.5 invocation:invocation repeats:yes]; self.unregisteredtimer = timer;

(IBAction)startUnregisteredTimer:sender { if (unregisteredtimer!= nil) { NSRunLoop *runloop = [NSRunLoop currentrunloop]; [runloop addtimer:unregisteredtimer formode:nsdefaultrunloopmode];

create a timer with a given start time (one second in the future), and then start the timer by adding it to a run loop: (IBAction) startfiredatetimer:sender { NSDate *firedate = [NSDate datewithtimeintervalsincenow:1.0]; NSTimer *timer = [[NSTimer alloc] initwithfiredate:firedate interval:0.5 target:self selector:@selector(countedtargetmethod:) userinfo:[self userinfo] repeats:yes];

timercount = 1; NSRunLoop *runloop = [NSRunLoop currentrunloop]; [runloop addtimer:timer formode:nsdefaultrunloopmode]; [timer release]; although the timer is configured to repeat, it will be stopped after it has fired three times by the countedtargetmethod:

Stopping a Timer If you create a repeating timer, however, you stop it by sending it an invalidate message. You can also send a non repeating timer an invalidate ld message bf before it fires to prevent it from firing

(IBAction) stoprepeatingtimer:sender p { [repeatingtimer invalidate]; self.repeatingtimer = nil; (IBAction) stopunregisteredtimer:sender { [unregisteredtimer invalidate]; self.unregisteredtimer = nil;

You can also invalidate a timer from the method it invokes. (void) countedtargetmethod:(nstimer*) thetimer { NSDate *startdate = [[thetimer userinfo] objectforkey:@"startdate"]; NSLog(@"Timer started t on %@; fire count %d", startdate, timercount); timercount++; if (timercount > 3) { [thetimer invalidate];