简体   繁体   English

目标C,一次一种方法?

[英]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: 如果我在cocos2d中创建一个简单的Xcode项目,我总是想知道在以下情况下会发生什么:

Method A is scheduled every 0.01 seconds 方法A每0.01秒安排一次

Method B is scheduled every 1 second 方法B每1秒安排一次

Now suppose method B is a large method and takes a moment to compute. 现在假设方法B是一个很大的方法,需要花一点时间来计算。 Will it EVER be interrupted by method A? 是否会被方法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). 通常,当您在UI应用程序的UI线程中调度内容时,一旦启动特定操作,它就不会被中断(错误除外)。 This holds not only for iOS, but for most UI platforms. 这不仅适用于iOS,也适用于大多数UI平台。

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. 系统可以中断UI线程以处理硬件中断(或者,例如,由于接收到的单元信号引起的中断),但是这些中断对于应用程序(大部分)是“透明的”。

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). 这假设您将使用NSTimer来安排您的方法,并且两个方法都将在同一个runloop上进行处理(即,每个方法不使用单独的线程,这通常在您希望安排两个独立运行的方法时完成)。

This quote taken directly from the NSTimer class reference overview section on apple's site: 该引用直接来自苹果网站上的NSTimer 类参考概述部分

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. 也就是说,方法A的轮询机制(例如NSTimer )在方法B完成之前不会触发,假设它们处于同一个运行循环中。 Method A would not interrupt Method B, per sé, but is dependent on it completing its task. 方法A不会因为sé而中断方法B,而是依赖于它完成其任务。

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 如果您想要将方法A和方法B放在单独的线程上以便它们独立工作的信息,您可以从这里开始: Grand Central Dispatch

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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