0

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

@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 正在写很多颜色,导致颜色面板滞后。有什么想法可以解决这个问题吗?

4

1 回答 1

1

合并 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,这将保持立即响应,但是如果通过读取刚刚写入的值来使用颜色境界,还是会有延迟的。

于 2019-11-05T15:21:06.597 回答