gpt4 book ai didi

swift - Realm 写入使 NSColorPanel 滞后

转载 作者:行者123 更新时间:2023-11-28 13:23:42 25 4
gpt4 key购买 nike

在我的 Catalyst 应用程序中,我使用 Mac Helpers 存储库中的代码生成一个带有回调的 NSColorPanel

@interface IPDFMacColorPanel : NSObject

+ (void)showColorPanelWithColorChangeHandler:(void(^)(UIColor *color))colorChangeHandler;

/// Hides the color picker panel and removes the colorChangeHandler observer
+ (void)hide;

+ (void)removeColorChangeHandlerObserver;

@end

@interface NSColorPanel_Catalyst : NSObject

+ (instancetype)sharedColorPanel;

- (void)makeKeyAndOrderFront:(id)sender;
- (void)orderFront:(id)sender;
- (void)orderOut:(id)sender;

- (void)setTarget:(id)target;
- (void)setAction:(SEL)action;

- (UIColor *)color;

@end

@interface IPDFMacColorPanel ()

@property (nonatomic,copy) void(^colorChangeHandler)(UIColor *color);

@end

@implementation IPDFMacColorPanel

+ (instancetype)sharedPanel
{
static IPDFMacColorPanel *panel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
panel = [IPDFMacColorPanel new];
});
return panel;
}

+ (NSColorPanel_Catalyst *)colorPanel
{
return [NSClassFromString(@"NSColorPanel") performSelector:@selector(sharedColorPanel)];
}

+ (void)showColorPanelWithColorChangeHandler:(void(^)(UIColor *color))colorChangeHandler
{
IPDFMacColorPanel *observer = [IPDFMacColorPanel sharedPanel];
observer.colorChangeHandler = colorChangeHandler;

NSColorPanel_Catalyst *colorPanel = [self colorPanel];
[colorPanel setTarget:observer];
[colorPanel setAction:@selector(colorChange:)];
[colorPanel orderFront:nil];
}

+ (void)hide
{
[[self colorPanel] orderOut:nil];
[self removeColorChangeHandlerObserver];
}

+ (void)removeColorChangeHandlerObserver
{
[IPDFMacColorPanel sharedPanel].colorChangeHandler = nil;
}

- (void)colorChange:(NSColorPanel_Catalyst *)colorPanel
{
if (self.colorChangeHandler) self.colorChangeHandler(colorPanel.color);
}

@end

我是这样生成它的:

IPDFMacColorPanel.show { (color) in
if let color = color {
self.saveColor(color)
}
}

然后写入到Realm中:

public func saveColor(_ color: UIColor) {
try! realm.write {
let converted = ColorUtility.convert(color: color)
if let green = converted["green"] {
self.restaurant.green = green
}
if let red = converted["red"] {
self.restaurant.red = red
}
if let blue = converted["blue"] {
self.restaurant.blue = blue
}
if let alpha = converted["alpha"] {
self.restaurant.alpha = alpha
}
}
}

但是,发生的事情是,因为我收到了太多回调,Realm 正在写入大量颜色,导致颜色面板滞后。我有什么办法可以解决这个问题吗?

最佳答案

合并 Objective-C 端的写入。

// new property - add after `@interface IPDFMacColorPanel` line
@property (nonatomic,copy) UIColor *pendingNewColor;

- (void)colorChange:(NSColorPanel_Catalyst *)colorPanel
{
if (self.pendingNewColor) {
self.pendingNewColor = colorPanel.color;
} else {
self.pendingNewColor = colorPanel.color;
[self performSelector:@selector(doUpdateColor:) withObject:nil afterDelay:0.4];
}
}

- (void)doUpdateColor:(id)ignore {
UIColor *color = self.pendingNewColor;
self.pendingNewColor = nil;
if (self.colorChangeHandler) self.colorChangeHandler(color);
}

当颜色改变时,如果存在挂起的颜色,它假定 doUpdateColor: 消息将在 0.4 秒内发送并且只更新挂起的颜色,否则它设置挂起的颜色并调度要发送的 doUpdateColor: 消息.

此解决方案牺牲了一些明显的响应能力。您也可以在接收 Swift 端做类似的事情,通过将颜色存储在某处并且暂时不将其写入 Realm,这将保持即时响应,但如果某些东西正在使用颜色从 Realm 读取刚刚写入的值,那里仍然会有延迟。

关于swift - Realm 写入使 NSColorPanel 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714205/

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