NSTimer无效后,后台线程NSRunloop运行不会退出!为什么?

时间:2011-06-14 20:44:30

标签: iphone objective-c nstimer nsthread nsrunloop

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

创建计时器并将其从gdb I po runLoop附加到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 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 = 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。

3 个答案:

答案 0 :(得分:1)

来自-[NSRunLoop run]文档:

  

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

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

答案 1 :(得分:1)

这样可以阻止运行循环:

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

如果您正在编写可能在主线程上运行的代码(因此使用主runloop),那么在执行此操作之前,您应该检查当前的runloop是否是主要的runloop - 否则你会轰炸你的应用

// 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]];
}

答案 2 :(得分:0)

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