4

我正在创建一个 NSTimer 并将其添加到后台线程的运行循环中。我的代码就像这个答案的后台线程示例:iPhone-SDK:Call a function in the background?

在创建计时器并将其从 gdb I 附加到运行循环后po runLoop,它会输出以下内容:

<CFRunLoop 0x695b090 [0x16a62c0]>{wakeup port = 0x6907, stopped = false,
current mode = kCFRunLoopDefaultMode,
common modes = <CFBasicHash 0x6936e60 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
    1 : <CFString 0x16abba8 [0x16a62c0]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x695e160 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopTimer 0x69398a0 [0x16a62c0]>{valid = Yes, interval = 6, next fire date = 329774303, callout = __NSFireTimer (0x212399), context = <CFRunLoopTimer context 0x6903a10>}
}
,
modes = <CFBasicHash 0x6904120 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
    1 : <CFRunLoopMode 0x6946180 [0x16a62c0]>{name = kCFRunLoopDefaultMode, port set = 0x6807, timer port = 0x6b03, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = <CFArray 0x695e180 [0x16a62c0]>{type = mutable-small, count = 1, values = (
    0 : <CFRunLoopTimer 0x69398a0 [0x16a62c0]>{valid = Yes, interval = 6, next fire date = 329774303, callout = __NSFireTimer (0x212399), context = <CFRunLoopTimer context 0x6903a10>}
)}
},

}
}

这表明 1 个计时器已附加到 runloop 之后,在我使计时器无效之后, NSRunloop 运行方法不会退出,但是在我暂停调试器并从 gdb 中我po runLoop再次看起来像这样:

<CFRunLoop 0x695b090 [0x16a62c0]>{wakeup port = 0x6907, stopped = false,
current mode = kCFRunLoopDefaultMode,
common modes = <CFBasicHash 0x6936e60 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
    1 : <CFString 0x16abba8 [0x16a62c0]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x695e160 [0x16a62c0]>{type = mutable set, count = 0,
entries =>
}
,
modes = <CFBasicHash 0x6904120 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
    1 : <CFRunLoopMode 0x6946180 [0x16a62c0]>{name = kCFRunLoopDefaultMode, port set = 0x6807, timer port = 0x6b03, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = <CFArray 0x695e180 [0x16a62c0]>{type = mutable-small, count = 0, values = ()}
},

}
}

现在“计时器”条目有 0 个对象。但线程继续运行。我经常离开屏幕然后回来,所以这会导致后台线程的建立,在使用太多资源后最终会杀死应用程序。定时器在失效后不会触发,但后台线程仍然存在。

我知道我可以将计时器移回主线程或使用 NSThread sleepForTimeInterval 创建自己的简单计时器线程,但我想保留主线程以进行 GUI 更新并尽可能使用 NSTimer。

4

3 回答 3

2

这对我来说停止了一个运行循环:

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate date]];

但是,如果您正在编写可能在主线程上运行的代码(因此使用主运行循环),您应该在执行此操作之前检查您当前的运行循环是否是主运行循环 - 否则您会炸毁您的应用程序。

// Kill the runloop now.
NSRunLoop* rl = [NSRunLoop currentRunLoop]; // Get the runloop
if (rl != [NSRunLoop mainRunLoop])
{
    // Set it running again, but only until now. 
    // In other words, STOP!!!
    [rl runUntilDate: [NSDate date]];
}
于 2012-05-25T04:38:43.800 回答
1

-[NSRunLoop run]文档:

从运行循环中手动删除所有已知的输入源和计时器并不能保证运行循环将退出。

您应该使用不同的方法,可能runMode:beforeDate:在循环中,并且在使计时器无效的同时,设置一个指示运行循环应该结束的标志。

于 2011-06-14T20:53:51.450 回答
0

我需要阅读 [NSTimer invalidate] 的文档...。必须从安装了计时器的同一线程调用此方法。如果您不从同一个线程调用它,则计时器线程将不会退出。

于 2011-06-14T22:02:47.003 回答