react-native-vision-camera
react-native-vision-camera copied to clipboard
🐛 Using callback (RCTResponseSenderBlock) as an argument in a frame processor
What were you trying to do?
Hello
I'm having issues using callbacks as arguments for my exported function.
I would like to use something like this in my frame processor:
__detectRectangles(frame, (result, error) => { // callback });
However, I don't know how to cast the arguments into RCTResponseSenderBlock
or how to call the callback from my Swfit code.
Reproduceable Code
No response
What happened instead?
Can't call provided callback
Relevant log output
No response
Device
iOS
VisionCamera Version
2.13.3
Additional information
- [X] I am using Expo
- [X] I have read the Troubleshooting Guide
- [X] I agree to follow this project's Code of Conduct
- [X] I searched for similar issues in this repository and found none.
Everything should be synchronous, callbacks will introduce async callback hell. I think it would be possible to support them though if really needed
You can do it in Objective-C. I tried with React Native 0.69 and OK.
@implementation YourProcessor
+ (void)asyncProcessFrame:(Frame * _Nonnull)frame args:(NSArray * _Nonnull)args callback:(void (^ _Nonnull)(NSDictionary<NSString *, id> * _Nullable, NSError * _Nullable))callback
{
/* async operation */
/* ... */
callback(dict, [NSNull null]);
}
static inline id processFrame(Frame* frame, NSArray* args) {
RCTResponseSenderBlock responseSender = args[1];
[YourProcessor asyncProcessFrame:frame args:args callback:^(NSDictionary<NSString *,id> * _Nullable result, NSError * _Nullable error) {
responseSender(@[
error != nil ? error : [NSNull null],
result != nil ? result : [NSNull null]
]);
}];
return @[];
}
VISION_EXPORT_FRAME_PROCESSOR(processFrame)
@end
You can do it in Objective-C. I tried with React Native 0.69 and OK.
@implementation YourProcessor + (void)asyncProcessFrame:(Frame * _Nonnull)frame args:(NSArray * _Nonnull)args callback:(void (^ _Nonnull)(NSDictionary<NSString *, id> * _Nullable, NSError * _Nullable))callback { /* async operation */ /* ... */ callback(dict, [NSNull null]); } static inline id processFrame(Frame* frame, NSArray* args) { RCTResponseSenderBlock responseSender = args[1]; [YourProcessor asyncProcessFrame:frame args:args callback:^(NSDictionary<NSString *,id> * _Nullable result, NSError * _Nullable error) { responseSender(@[ error != nil ? error : [NSNull null], result != nil ? result : [NSNull null] ]); }]; return @[]; } VISION_EXPORT_FRAME_PROCESSOR(processFrame) @end
Thanks, I'm going to try this and report back
Hey - callbacks don't really make sense here as FPs are synchronous. You can't really have long running tasks here, this will just stall the Camera