gpt4 book ai didi

iphone - 主线程上的 NSOperation 和 NSNotificationCenter

转载 作者:太空狗 更新时间:2023-10-30 03:13:52 26 4
gpt4 key购买 nike

我有一个 NSOperation。完成后,我触发 NSNotificationCenter 让程序知道 NSoperation 已完成并更新 gui。

据我了解,NSNotification 的监听器不会在主线程上运行,因为 NSOperation 不在主线程上。

我怎样才能让监听器在我触发事件时在主线程上运行?

[[NSNotificationCenter defaultCenter] postNotificationName:@"myEventName" object:self]; 

最佳答案

更新:调度队列使得在主线程上发布通知变得非常容易。

dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter] postNotification...];
});

要等待通知处理程序完成,只需将 dispatch_async 替换为 dispatch_sync。


在 notnoop 的回答之后,这里有一些基础设施,您可以使用它来安全地将通知发布到主线程上, 无需等待它们完成。希望有人会发现这有帮助!

NSNotificationCenter+Utils.h:

@interface NSNotificationCenter (Utils)

-(void)postNotificationOnMainThread:(NSNotification *)notification;
-(void)postNotificationNameOnMainThread:(NSString *)aName object:(id)anObject;
-(void)postNotificationNameOnMainThread:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

@end

NSNotificationCenter+Utils.m:

@interface NSNotificationCenter (Utils_Impl) {
}

-(void)postNotificationOnMainThreadImpl:(NSNotification*)notification;
-(void)postNotificationNameOnMainThreadImpl:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

@end

@implementation NSNotificationCenter (Utils)

-(void)postNotificationOnMainThread:(NSNotification *)notification {
[notification retain];
[notification.object retain];
[self performSelectorOnMainThread:@selector(postNotificationOnMainThreadImpl:)
withObject:notification
waitUntilDone:NO];
}

-(void)postNotificationNameOnMainThread:(NSString *)aName object:(id)anObject {
[self postNotificationNameOnMainThread:aName object:anObject userInfo:nil];
}

-(void)postNotificationNameOnMainThread:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo {
[aName retain];
[anObject retain];
[aUserInfo retain];

SEL sel = @selector(postNotificationNameOnMainThreadImpl:object:userInfo:);
NSMethodSignature* sig = [self methodSignatureForSelector:sel];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:sel];
[invocation setArgument:&aName atIndex:2];
[invocation setArgument:&anObject atIndex:3];
[invocation setArgument:&aUserInfo atIndex:4];
[invocation invokeOnMainThreadWaitUntilDone:NO];
}

@end

@implementation NSNotificationCenter (Utils_Impl)

-(void)postNotificationOnMainThreadImpl:(NSNotification*)notification {
[self postNotification:notification];
[notification.object release];
[notification release];
}

-(void)postNotificationNameOnMainThreadImpl:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo {
[self postNotificationName:aName object:anObject userInfo:aUserInfo];
[aName release];
[anObject release];
[aUserInfo release];
}

@end

NSInvocation+Utils.h:

@interface NSInvocation (Utils)

-(void)invokeOnMainThreadWaitUntilDone:(BOOL)wait;

@end

NSInvocation+Utils.m:

@implementation NSInvocation (Utils)

-(void)invokeOnMainThreadWaitUntilDone:(BOOL)wait
{
[self performSelectorOnMainThread:@selector(invoke)
withObject:nil
waitUntilDone:wait];
}

@end

关于iphone - 主线程上的 NSOperation 和 NSNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1973106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com