委托完成后如何触发回调

时间:2019-04-19 12:23:27

标签: ios objective-c react-native delegates react-native-ios

好的,这是我的难题。

我正在与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方法进行回调的最简单方法是什么?

先谢谢您。

0 个答案:

没有答案