I am creating a Node addon to export a video file from macOS Photos library, as it takes a few seconds, I wrapped the code into an AsyncWorker
.
The C++ / Objective-C code:
class Napi_PhotosExport_AsyncWorker : public Napi::AsyncWorker
{
void Execute()
{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[[PHImageManager defaultManager] requestExportSessionForVideo:... {
// this should be called, but never be called.
dispatch_group_leave(group);
}];
// this blocks the current thread and wait for the above callback.
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}
};
The code above works in my macOS Cocoa application, but it doesn't work in Electron / Node environment for some reason. I expected the dispatch_group_leave
can be called at some point, so that dispatch_group_wait
returns properly instead of blocking the thread.
I am looking for some help from both macOS and Electron developers. Maybe Grand Central Dispatcher should not be used in Node addon, and I need to use C++ lock instead?