好的,这是我的难题。
我正在与Brother SDK一起为我们的移动应用程序创建一个React Native包装器,并且我们正在开发SDK中的自动发现功能。
BRPTouchNetworkDelegate
仅具有一个要遵循的功能,它看起来像:
-(void)didFinishSearch:(id)sender
我们的头文件如下:
@protocol BRWLANPrintOperationDelegate <NSObject>
- (void) showPrintResultForWLAN;
@end
@interface RNB2MatchRnPrinter : NSObject <RCTBridgeModule, BRPtouchNetworkDelegate>
{
BRPtouchNetworkManager *networkManager;
BRPtouchPrinter *ptp;
BRPtouchPrintInfo *printerInfo;
NSString *paperSize;
NSString *filePath;
NSString *ipAddress;
NSString *printerModel;
NSArray *allowedFileTypes;
NSMutableArray *printerList;
NSDictionary *printerDict;
BOOL shouldAutocut;
}
@property NSArray *imagePathArray;
@property NSString *customPaperFilePath;
@property(nonatomic, assign) BOOL communicationResultForWLAN;
@property (nonatomic, weak)id<BRWLANPrintOperationDelegate> delegate;
我们的didFinishSearch
函数如下所示:
-(void)didFinishSearch:(id)sender
{
NSLog(@"didFinishSearch");
printerList = (NSMutableArray *)[networkManager getPrinterNetInfo];
NSLog(@"_brotherDeviceList [%@]", (NSMutableArray *)[networkManager getPrinterNetInfo]);
return;
}
和我们的React Native方法如下:
RCT_EXPORT_METHOD(printerSearch:(RCTResponseSenderBlock)callback)
{
[networkManager startSearch: 5];
int64_t delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
RCTLogInfo(@"Dispatch has been called");
NSMutableArray* connectedPrinters = (NSMutableArray*)[self->networkManager getPrinterNetInfo];
for (BRPtouchDeviceInfo *printer in connectedPrinters) {
self->printerDict = [[NSDictionary alloc] initWithObjectsAndKeys:
printer.strModelName, @"modelName",
printer.strIPAddress, @"ipAddress",
printer.strMACAddress, @"macAddress", nil
];
[self->printerList addObject:self->printerDict];
}
callback(@[[NSNull null], self->printerList]);
});
}
现在,发生的事情是在线程中,有时我们无法从网络管理器[networkManager getPrinterInfo]
得到结果,但是我们确实在didFinishSearch
中得到了结果。
由于这是网络操作,因此我们不知道需要多少时间来超时。
想法是从printerList
获取didFinishSearch
数据。
有没有一种方法,将结果从didFinishSearch
返回到React Native方法进行回调的最简单方法是什么?
先谢谢您。