简体   繁体   中英

Objective C, one method at a time?

If I make a simple Xcode project in cocos2d I've always wondered what happens in the following situation:

Method A is scheduled every 0.01 seconds

Method B is scheduled every 1 second

Now suppose method B is a large method and takes a moment to compute. Will it EVER be interrupted by method A?

In other words, will a method always complete before another one starts?

I havn't created threads or anything.

In general, when you're scheduling stuff in the UI thread of a UI application, once a specific operation is started, it's not interrupted (except for errors). This holds not only for iOS, but for most UI platforms.

The system may interrupt the UI thread to handle hardware interrupts (or, eg, interrupts due to received cell signals), but those interruptions would be (mostly) "transparent" to the application.

But you'll never be interrupted by your own operations.

This assumes you would be using, say, an NSTimer to schedule your methods AND both methods will be processing on the same runloop (ie not using seperate threads per method which is typically done when you want to schedule two methods to run independantly).

This quote taken directly from the NSTimer class reference overview section on apple's site:

If a timer's firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

That is to say that Method A's polling mechanism ( NSTimer for example) will not fire until Method B has completed assuming they are on the same run loop. Method A would not interrupt Method B, per sé, but is dependent on it completing its task.

If you want information on placing Method A and Method B on separate thread so they work independently, you can start here: Grand Central Dispatch

如果您不使用线程,则保证您的方法按顺序执行,不会中断。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM