iOS4 创建后台定时器

发布于 2024-10-01 17:39:27 字数 212 浏览 1 评论 0原文

我(基本上)需要在 iOS 4 上创建一个后台计时器,它允许我在经过特定时间后执行一些代码。我读到您可以使用一些 [NSThread detachNewThreadSelector: 目标: withObject:]; 但这在实践中是如何工作的呢?我怎样才能确保线程也保持在后台。本地通知对我来说不起作用,因为我需要执行代码,而不是通知用户。

帮助将不胜感激!

I (Basically) need to create a background timer on iOS 4 that will allow me to execute some code when a specific amount of time has passed. I have read that you can accomplish this using some [NSThread detachNewThreadSelector:
toTarget:
withObject:];
but how does that work in practice? How can I ensure that the thread remains in the background also. Local notifications will NOT work for me, as I need to execute code, not notify the user.

Help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人疚 2024-10-08 17:39:27

您还可以使用 Grand Central Dispatch (GCD) 来执行此操作。这样,您可以使用块将代码保存在一个位置,并且如果您需要在完成后台处理后更新 UI,请确保再次调用主线程。这是一个基本示例:

#import <dispatch/dispatch.h>

…

NSTimeInterval delay_in_seconds = 3.0;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delay_in_seconds * NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

UIImageView *imageView = tableViewCell.imageView;

// ensure the app stays awake long enough to complete the task when switching apps
UIBackgroundTaskIdentifier taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:{}];

dispatch_after(delay, queue, ^{
    // perform your background tasks here. It's a block, so variables available in the calling method can be referenced here.        
    UIImage *image = [self drawComplicatedImage];        
    // now dispatch a new block on the main thread, to update our UI
    dispatch_async(dispatch_get_main_queue(), ^{        
      imageView.image = image;
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
    });
}); 

Grand Central Dispatch (GCD) 参考:
http://developer.apple.com/ Library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

块参考:
http://developer .apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23//apple_ref/doc/uid/TP40009758

后台任务参考:
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler

You can also do this using Grand Central Dispatch (GCD). This way you can use blocks to keep your code in one place, and be sure you're calling the main thread again if you need to update your UI after you've finished background processing. Here's a basic example:

#import <dispatch/dispatch.h>

…

NSTimeInterval delay_in_seconds = 3.0;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delay_in_seconds * NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

UIImageView *imageView = tableViewCell.imageView;

// ensure the app stays awake long enough to complete the task when switching apps
UIBackgroundTaskIdentifier taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:{}];

dispatch_after(delay, queue, ^{
    // perform your background tasks here. It's a block, so variables available in the calling method can be referenced here.        
    UIImage *image = [self drawComplicatedImage];        
    // now dispatch a new block on the main thread, to update our UI
    dispatch_async(dispatch_get_main_queue(), ^{        
      imageView.image = image;
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
    });
}); 

Grand Central Dispatch (GCD) reference:
http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

Blocks reference:
http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23//apple_ref/doc/uid/TP40009758

Background Task reference:
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:

欢烬 2024-10-08 17:39:27

您可以使用这些调用在新线程 (detachNewThred) 中执行带有某些参数 (withObject) 的对象 (toTarget) 的方法(选择器)。

现在,如果您想执行延迟任务,最好的方法可能是performSelector:withObject:afterDelay:,如果您想在后台运行任务,请调用detachNewThreadSelector:toTarget:withObject:

You can use those call to execute a method (selector) of an object (toTarget) with some parameter (withObject) in a new thread (detachNewThred).

Now if you want to execute a delayed task may be the best approach is performSelector: withObject: afterDelay: and the if you want to run the task on background call the detachNewThreadSelector: toTarget: withObject:

嘿嘿嘿 2024-10-08 17:39:27

这些建议的方法是否仅适用于应用程序首先启用后台执行(使用 UIBackgroundMode)的情况?

我想如果一个应用程序不能合法地声称自己是一个 voip/音乐/位置感知应用程序,那么如果它实现了此处描述的内容,那么当时间间隔到期时它不会执行?

Are these suggested methods only applicable if the application is enabled for background execution (using UIBackgroundMode) in the first place?

I presume if an application cannot legitimately claim to be a voip/music/location aware app then if it implements what is described here it will not execute when the time interval expires?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文