有没有办法像线程一样调用 NSNotificationQueue ?

发布于 2024-11-15 00:54:57 字数 388 浏览 4 评论 0原文

是否可以像这样运行通知,

-(void) testMethod
{
   [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification    notificationWithName:@"TEST123" object:self userInfo:parms]postingStyle:NSPostNow  coalesceMask:NSNotificationNoCoalescing forModes:nil];

NSLog(@"test123");
//.....

以便“-(void)testMethod”继续执行 NSLog,...,而无需完成通知调用的方法。 有没有办法在没有线程的情况下解决这个问题?

Is it possible to run the notifications like

-(void) testMethod
{
   [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification    notificationWithName:@"TEST123" object:self userInfo:parms]postingStyle:NSPostNow  coalesceMask:NSNotificationNoCoalescing forModes:nil];

NSLog(@"test123");
//.....

so that the "-(void)testMethod" go on an execute the NSLog,..., without that the methods, which are called by the notifications did finish.
Is there anyway to solve it, without threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离不开的别离 2024-11-22 00:54:58

我认为您想异步调用方法。
答案是否定的:您不能在同一线程中异步调用方法。
检查文档:

通知中心提供
给观察员的通知
同步地。换句话说,
postNotification:方法不
返回,直到所有观察者都完成
收到并处理了
通知。发送通知
异步使用
NSNotificationQueue。

I think you want to invoke methods asynchronously.
The answer is NO: you can't invoke methods asynchronously in the same thread.
Check the docs:

A notification center delivers
notifications to observers
synchronously. In other words, the
postNotification: methods do not
return until all observers have
received and processed the
notification. To send notifications
asynchronously use
NSNotificationQueue.

画离情绘悲伤 2024-11-22 00:54:58

NSNotificationQueue 不用于异步目的。
参考:
NSNotificationQueue 对象(或简称通知队列)充当通知中心(NSNotificationCenter 的实例)的缓冲区。通知中心在发布时分发通知,而放入队列中的通知可以延迟,直到运行循环的当前传递结束或直到运行循环空闲。还可以合并重复的通知,以便在发布多条通知的同时仅发送一条通知

NSNotificationQueue is not used for asynchronous purpose.
Reference:
NSNotificationQueue objects (or simply notification queues) act as buffers for notification centers (instances of NSNotificationCenter). Whereas a notification center distributes notifications when posted, notifications placed into the queue can be delayed until the end of the current pass through the run loop or until the run loop is idle. Duplicate notifications can also be coalesced so that only one notification is sent although multiple notifications are posted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文