gpt4 book ai didi

ios - 无法在 IOS 中多次执行 X 分钟的函数调用

转载 作者:行者123 更新时间:2023-11-28 22:05:18 24 4
gpt4 key购买 nike

我试图创建每分钟调用一次的函数(没有计时器)。
当我运行该应用程序时,它会在一分钟后执行,然后再也不会执行。
我可能错过了什么,但找不到什么?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:date];

NSTimeInterval timeSinceLastSecond = date.timeIntervalSince1970 - floor(date.timeIntervalSince1970);
NSTimeInterval timeToNextMinute = (60 - dateComponents.second) - timeSinceLastSecond;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeToNextMinute * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[self myFunction];

});

return YES;
}

最佳答案

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    [self executeEveryOneMinute];    return YES: } - (void)executeEveryOneMinute {    [self myFunction]    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [self executeEveryOneMinute];    }); }

关于ios - 无法在 IOS 中多次执行 X 分钟的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com