CATextLayer not updating foreground color property on appearance change

I have an issue with my macOS objC app that uses CATextLayer instances that adapt to the app appearance (dark, light). I had no problem with Ventura, so I suppose this is a Sonoma bug. But maybe I'm not doing the right things.

Within -updateLayer , I call stringLayer.foregroundColor = NSColor.textColor.CGColor. (stringLayer is an instance of CATextLayer.)

NSColor.textColor should adapt to the app appearance. But the color doesn't always change when the app appearance changes. So the text would turn black in dark mode (hence illegible) and white in light mode when I toggle the mode in the system preferences.

To investigate wether the issues was specific to the system text color, I tried (again, within -updateLayer):

NSColor *color = [NSApp.effectiveAppearance.name isEqualToString:NSAppearanceNameDarkAqua]? NSColor.redColor : NSColor.greenColor;

stringLayer.foregroundColor = color.CGColor; 

I basically get the same issue. The correct color shows when the app launches, but doesn't change the first time I toggle the mode (dark/light). So the wrong color associates with the app appearance (red with light mode and green with dark mode).

The code below works :

NSColor *color = [NSApp.effectiveAppearance.name isEqualToString:NSAppearanceNameDarkAqua]? NSColor.redColor : NSColor.greenColor;

NSDictionary *dic = @{NSFontAttributeName: [NSFont labelFontOfSize:10.0], NSForegroundColorAttributeName:color};
	
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"test"
										attributes: dic];

stringLayer.string = string;   

But the code below doesn't. The layer text color doesn't change when the app appearance changes.

NSDictionary *dic = @{NSFontAttributeName: [NSFont labelFontOfSize:10.0], NSForegroundColorAttributeName:NSColor.textColor};
	
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"test"
										attributes: dic];

stringLayer.string = string;   

Note that the issue appears to be specific to the foreground color. The background color (which I update in the same method) is always set properly.