1

我的应用程序中有一个设置面板。每当用户按下按钮时,对象都会随着 UI 的更新而在另一个线程上更新。我在主视图上有一个单独的标签,它应该在对象完成更新时更新对象计数(无论设置面板是向上还是向下,我都希望发生这种情况)。我已经尝试关注关于这个主题的苹果文档,但它似乎对我来说并不奏效——也就是说,主视图控制器似乎由于某种原因从未收到通知。有人对如何提醒主视图控制器传递给另一个线程的对象已完成更新有任何建议吗?这是我正在使用的代码(其中大部分是从该文档中复制的):

对象等级:

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

主视图控制器

- (void)setUpThreadingSupport
{
    if (self.notifications) {
        return;
    }
    self.notifications = [[NSMutableArray alloc] init];
    self.notificationLock = [[NSLock alloc] init];
    self.notificationThread = [NSThread currentThread];

    self.notificationPort = [[NSMachPort alloc] init];
    [self.notificationPort setDelegate: self];
    [[NSRunLoop currentRunLoop] addPort: self.notificationPort
                            forMode: (NSString *)kCFRunLoopCommonModes];
}

- (void)handleMachMessage:(void *)msg
{
    [self.notificationLock lock];

    while ([self.notifications count]) {
        NSNotification *notification = [self.notifications objectAtIndex: 0];
        [self.notifications removeObjectAtIndex: 0];
        [self.notificationLock unlock];
        [self processNotification: notification];
        [self.notificationLock lock];
    };

    [self.notificationLock unlock];
}

- (void)processNotification:(NSNotification *)notification{

    if ([NSThread currentThread] != self.notificationThread) {
        // Forward the notification to the correct thread.
        [self.notificationLock lock];
        [self.notifications addObject: notification];
        [self.notificationLock unlock];
        [self.notificationPort sendBeforeDate: [NSDate date]
                                   components: nil
                                         from: nil
                                     reserved: 0];
    } else {
        [self updateScaleCount];
    }
}

- (void)updateScaleCount
{
    NSLog(@"[ScalesViewController - updateScaleCount]: Scales updated from notification center.");
    if([UserDefinedScales areScalesGrouped] == YES){
        self.groupCountLabel.text = [NSString stringWithFormat: @"Group Count: %i", [[UserDefinedScales sortedKeys] count]];
    } else {
        self.groupCountLabel.text = @"Group Count: 1";
    }
    self.scaleCountLabel.text = [NSString stringWithFormat: @"Scale Count: %i", [UserDefinedScales scaleCount]];
}

主视图控制器 - 视图已加载:

[self setUpThreadingSupport];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(processNotification:)
                                                 name: @"ScaleCountUpdated"
                                               object: nil];

如果您对如何更改此代码以使其正常运行有任何建议,或者有其他解决方案可以实现这一点,我们将不胜感激!谢谢你。

4

2 回答 2

0

我会去一个更简单的实现。NSNotificationCenter已经提供了在应用程序中广播和接收消息所需的所有机制。

如果您从后台线程触发通知,您可以使用GCD dispatch_async使其在主线程上传递。

项目等级

dispatch_async(dispatch_get_main_queue(), ^{
    //  You don't need to pass the object itself here as you are not using it later.     
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ScaleCountUpdated"];
}

主视图控制器

-(void)viewDidLoad
{
    [super viewDidLoad];
    //  Register your controller as an observer for a specific message name
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(updateScaleCount)
                                                 name: @"ScaleCountUpdated"
                                               object: nil];

}
- (void)updateScaleCount
{
    NSLog(@"[ScalesViewController - updateScaleCount]: Scales updated from notification center.");
    if([UserDefinedScales areScalesGrouped] == YES){
        self.groupCountLabel.text = [NSString stringWithFormat: @"Group Count: %i", [[UserDefinedScales sortedKeys] count]];
    } else {
        self.groupCountLabel.text = @"Group Count: 1";
    }
    self.scaleCountLabel.text = [NSString stringWithFormat: @"Scale Count: %i", [UserDefinedScales scaleCount]];
}
于 2013-07-31T07:17:34.090 回答
0

在我看来,您做得正确,即注册通知并发送。

据我从您的代码和您提供的信息中可以看出,您基本上可以完全忘记setupThreadingSupport. 你绝对应该在没有它的情况下测试它。不知道你想要达到什么,但看起来有点矫枉过正,一个简单的块可能就足够了。在后台线程上收听通知是否有令人信服的理由?为什么不让通知中心决定呢?

addObserver记录通知的发送和接收——postNotification实际上,所有这些机制都需要按预期工作。

于 2013-07-27T19:51:44.447 回答