我想用UI测试来测试内存泄漏并做到这一点我想在调用视图控制器的deinit的时候在UI测试用例中得到消息。所以,我想出了这个提供IPC机制:
/**
Provides simple IPC messaging. To use this class you need to include
#include <notify.h>
in your bridging header. (Ab)uses UIPasteboard for message delivery, ie.
should not be used in production code.
*/
public class SimpleIPC {
/// Event notification name for libnotify.
private static let notifyEventName = "com.foo.SimpleIPC"
/// libnotify token.
private var token: Int32 = 0
/// Starts listening to the events
public func listen(callback: (String? -> Void)) {
notify_register_dispatch(SimpleIPC.notifyEventName, &token, dispatch_get_main_queue()) { token in
callback(UIPasteboard.generalPasteboard().string)
}
}
public class func send(message: String) {
UIPasteboard.generalPasteboard().string = message
notify_post(SimpleIPC.notifyEventName)
}
deinit {
notify_cancel(token)
}
}
它使用的libnotify
和UIPasteboard
组合的通知+数据传输。可用于单向通信,对于双向通信,可使有效负载包含发件人令牌或使用具有参数化libnotify事件名称的2个实例。
你打电话,你要听的addObserver?你确定在postNotification之前调用addObserver吗? – picciano
从MyApp目标发布通知可以正常工作,但是从UI测试发布目标dose't时。我也更新了问题。 – Ramis
偏离主题,但发送'self'作为'object'参数是一种很好的做法(或惯例?)。 –