如何在iPhone的for循环中使用NSTimer?
我对 iPhone 应用程序开发非常陌生。我正在将事件从我的 iPhone 应用程序添加到 iCal。用户可以从应用程序一次向 iCal 添加多个事件。如果用户向 iCal 添加超过 50 个事件,则该事件不会添加到 iCal 中,并且应用程序会崩溃。所以,我想避免应用程序崩溃。因此,我计划在 for 循环内延迟向 iCal 添加事件(当循环处于运行状态时)。如何暂停 For 循环运行并在 10 秒延迟后继续 for 循环。同时,for 循环应该在暂停 10 秒后继续。我该怎么做?有什么想法吗?请帮忙解决我的问题吗?提前致谢。
Am very new to iPhone application development. Am adding the events to iCal from my iphone app. The user can add multiple events at a time to iCal from the app. If the user add above 50 events to iCal, the event does not adding in iCal and the app getting crash. So, i want to avoid the app crash. So, i planned to delay the adding event to iCal inside the for loop(While the loop is in running state). How can i pause the For loop running and continue the for loop after 10 sec's delay. Meanwhile the for loop should continue after 10 sec's from where it was paused. How can i do this? Any ideas? Please help to solve my problem? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成您所追求的事情的最佳方法是 NSOperationQueue ,但它有点技术性。像这样的东西可能更容易理解:
标题:
当您指定事件列表时:
nextOp
方法:The best way to do what you're after is an
NSOperationQueue
but it's a bit technical. Something like this might be easier to understand:Header:
When you specify your list of events:
The
nextOp
method:您不应该不要在 iOS 上暂停循环。如果无法及时、同步地完成处理,则应退出循环并退出包含循环的方法。
要完成循环的处理,您的应用程序应稍后启动循环的另一次迭代,例如在来自完成委托、计时器、执行选择器或操作队列的回调中。后一种方法可以包括延迟。您必须知道如何解构循环以及如何保存本地状态才能做到这一点。使用回调退出循环大致相当于延迟的“GOTO”语句。
为了使 NSTimer 调用在循环处理中有用,您需要退出循环和方法,因为只有这样才会调用计时器的回调选择器。
You should not pause loops on iOS. If you cannot complete your processing promptly and synchronously, you should exit the loop and exit the method containing the loop.
To complete your loop's processing, your app should start another iteration of the loop later, such as in a callback from a completion delegate, a timer, a performselector, or an operation queue. The latter methods can include a delay. You will have to know how to unstructure loops and how to save local state to do this. Exiting a loop with a callback in place is roughly equivalent to a delayed "GOTO" statement.
For an NSTimer call to be useful in loop processing, you need to exit your loop and method, as only then will the timer's callback selector be called.