我正在编写一个在iOS 9中发布的NetworkExtension框架中扩展NEPacketTunnelProvider的NetworkExtension扩展,我遇到的情况是,一旦使用6MB内存,iOS就会扼杀这个扩展。
在普通的iOS应用程序中,有两种方法可以检测内存警告并对其进行处理。通过[UIApplicationDelegate applicationDidReceiveMemoryWarning:(UIApplication*)app]或[UIViewController didReceiveMemoryWarning]
是否有类似的方法来检测扩展中的内存警告?我在iOS扩展文档中上下搜索过,但到目前为止已经是空的了。
发布于 2015-12-08 05:35:26
我对扩展API并不十分熟悉,但是我的基本直觉是,您可以在该类中将任何对象注册为UIApplicationDidReceiveMemoryWarningNotification的观察者:
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidReceiveMemoryWarningNotification,
object: nil, queue: .mainQueue()) { notification in
print("Memory warning received")
}发布于 2020-10-19 20:11:24
奥兹古尔的回答行不通。是一个UIKit事件,我还没有找到从扩展访问该事件的方法。要走的路是这些选择的最后一个: DISPATCH_SOURCE_TYPE_MEMORYPRESSURE。
我在广播上传扩展中使用了以下代码(Swift),并使用断点确认在扩展中断之前在内存事件中调用它。
let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: nil)
let q = DispatchQueue.init(label: "test")
q.async {
source.setEventHandler {
let event:DispatchSource.MemoryPressureEvent = source.mask
print(event)
switch event {
case DispatchSource.MemoryPressureEvent.normal:
print("normal")
case DispatchSource.MemoryPressureEvent.warning:
print("warning")
case DispatchSource.MemoryPressureEvent.critical:
print("critical")
default:
break
}
}
source.resume()
}https://stackoverflow.com/questions/34148041
复制相似问题