ios – 在Cocoa Touch中实现Debounced / Coalesced模式,如`layoutSubviews`

许多 Cocoa Touch类利用了合并事件的设计模式.例如,UIViews有一个方法setNeedsLayout,它可以在不久的将来调用layoutSubviews.这在许多属性影响布局的情况下尤其有用.在每个属性的setter中,您可以调用[self setNeedsLayout],这将确保布局将更新,但如果一次更改多个属性或者即使单个属性修改多个,也会阻止对布局的许多(可能是昂贵的)更新运行循环的一次迭代中的时间.其他昂贵的操作,如setNeedsdisplay和drawRect:方法对遵循相同的模式.

实现这样的模式的最佳方法是什么?具体来说,我想将一些依赖属性绑定到一个昂贵的方法,如果属性发生了变化,每次迭代运行循环需要调用一次.

可能的解决方案:

使用CAdisplayLink或NSTimer你可以得到这样的工作,但两者似乎都比必要的更多参与,我不确定将它添加到许多对象(尤其是计时器)的性能影响是什么.毕竟,性能是做这样的事情的唯一原因.

在某些情况下我使用过这样的东西:

- (void)debounceSelector:(SEL)sel withDelay:(CGFloat)delay {
    [NSObject cancelPrevIoUsPerformRequestsWithTarget:self selector:sel object:nil];
    [self performSelector:sel withObject:nil afterDelay:delay];
}

这在用户输入应该仅在连续动作或类似事件时触发某些事件的情况下工作得很好.当我们想要确保触发事件没有延迟时,我们只想在同一个运行循环中合并调用,这似乎很笨拙.

解决方法

NSNotificationQueue就是你想要的东西.请参阅 Coalescing Notifications上的文档

这是UIViewController中的一个简单示例:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addobserver:self
                                             selector:@selector(configureView:)
                                                 name:@"CoalescingNotificationName"
                                               object:self];

    [self setNeedsReload:@"viewDidLoad1"];
    [self setNeedsReload:@"viewDidLoad2"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setNeedsReload:@"viewWillAppear1"];
    [self setNeedsReload:@"viewWillAppear2"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self setNeedsReload:@"viewDidAppear1"];
    [self setNeedsReload:@"viewDidAppear2"];
}

- (void)setNeedsReload:(Nsstring *)context
{
    NSNotification *notification = [NSNotification notificationWithName:@"CoalescingNotificationName"
                                                                 object:self
                                                               userInfo:@{@"context":context}];

    [[NSNotificationQueue defaultQueue] enqueueNotification:notification
                                               postingStyle:NSPostASAP
                                               coalesceMask:NSNotificationCoalescingOnName|NSNotificationCoalescingOnSender
                                                   forModes:nil];
}

- (void)configureView:(NSNotification *)notification
{
    Nsstring *text = [Nsstring stringWithFormat:@"configureView called: %@",notification.userInfo];
    NSLog(@"%@",text);
    self.detailDescriptionLabel.text = text;
}

您可以签出文档并使用postingStyle来获取所需的行为.在此示例中,使用NSPostASAP将为我们提供输出

configureView called: {
    context = viewDidLoad1;
}
configureView called: {
    context = viewDidAppear1;
}

意味着已经合并了对setNeedsReload的背靠背调用.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...