您可以使用 Grand Central Dispatch 中的调度源来合并属性更改观察结果,这样它们就不会比您处理它们更频繁地发生。
@implementation Controller
{
dispatch_source_t source;
}
- (id)init
{
self = [super init];
if (self)
{
//We are using data add source type, but not actually using the added data.
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
//Insert your network call to load data from the network.
//The event handler will only be called when another event handler is not being processed. So you won't attempt to do another network call until the last call was completed.
});
//Dispatch sources always start out suspended so you can add the event handler. You must resume them after creating them if you want events to be delivered)
dispatch_resume(source);
}
return self;
}
- (void)dealloc
{
dispatch_release(source);
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
//Tell dispatch source some data changed. It will only call the event handler if an event is not currently being handled.
//You could add a custom timer based coalesce here for when no events are currently being processed if you want to delay all initial events to potentially wait for more changes
dispatch_source_merge_data(source, 1);
}
@end
所以第一个属性更改通知会触发调度源事件处理程序。在现有事件运行时发生的后续属性更改将排队等待最后一个事件完成后立即运行。这意味着如果 5 个属性快速连续更改,您将获得 2 个网络调用(而不是 5 个网络调用)。如果您希望牺牲对通知的即时响应以消除第二次网络调用,则可以在未处理任何事件时添加基于自定义计时器的合并。