我对原生代码和桥接机制与原生代码的 react 非常陌生,尤其是当框架有委托(delegate)时。假设我正在尝试桥接以下框架:
@protocol BRPtouchNetworkDelegate;
@class PLNetworkModule;
@interface BRPtouchNetworkManager : NSObject <NSNetServiceBrowserDelegate,NSNetServiceDelegate>
@property(retain, nonatomic) NSMutableArray* registeredPrinterNames;
@property(assign, nonatomic) BOOL isEnableIPv6Search;
- (int)startSearch: (int)searchTime;
- (NSArray*)getPrinterNetInfo;
- (BOOL)setPrinterNames:(NSArray*)strPrinterNames;
- (BOOL)setPrinterName:(NSString*)strPrinterName;
- (id)initWithPrinterNames:(NSArray*)strPrinterNames;
- (id)initWithPrinterName:(NSString*)strPrinterName;
@property (nonatomic, assign) id <BRPtouchNetworkDelegate> delegate;
@end
@protocol BRPtouchNetworkDelegate <NSObject>
-(void) didFinishSearch:(id)sender;
@end
以下是我实现的桥接模块:
RCTBRPtouchNetworkManager.h
#import <React/RCTBridgeModule.h>
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
@interface RCTBRPtouchNetworkManager : NSObject <RCTBridgeModule, BRPtouchNetworkDelegate>
@end
RCTBRPtouchNetworkManager.m
#import "RCTBRPtouchNetworkManager.h"
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#import <React/RCTLog.h>
@implementation RCTBRPtouchNetworkManager {
BRPtouchNetworkManager *_networkManager;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location); //a dummy method to test the bridge
}
RCT_EXPORT_METHOD(startSearchWithTimeout:(int)time) {
RCTLogInfo(@"Bridge started search with time %d", time);
_networkManager = [[BRPtouchNetworkManager alloc] init];
_networkManager.delegate = self; //I'm setting delegate here
_networkManager.isEnableIPv6Search = NO;
NSString * path = [[NSBundle mainBundle] pathForResource:@"PrinterList" ofType:@"plist"];
if( path )
{
NSDictionary *printerDict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *printerList = [[NSArray alloc] initWithArray:printerDict.allKeys];
[_networkManager setPrinterNames:printerList];
} else {
RCTLogInfo(@"PrinterList path not found");
}
// Start printer search
[_networkManager startSearch: 5.0];
}
- (void)didFinishSearch:(id)sender {
NSLog(@"didFinishedSearch"); //this delegate method is not called
}
@end
我可以轻松调用虚拟方法并在日志中查看结果。但是,从不调用委托(delegate)方法 didFinishSearch()。我从 javascript 调用它如下:
componentDidMount() {
let networkManager = NativeModules.BRPtouchNetworkManager;
networkManager.startSearchWithTimeout(5.0);
}
我有什么我想念的吗?我是否正确实现委托(delegate)?这种功能是否可能(似乎不可能,因为 iOS 社区长期使用委托(delegate)方法)。非常感谢您的帮助。
编辑
我发现将以下内容添加到我的网桥管理器文件中会触发委托(delegate)(感谢 this post)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
但是,即使这解决了问题,我也希望对这里发生的事情有更技术性的了解,因为我似乎无法完全掌握它。谢谢
最佳答案
我知道这不是对帖子的回答,而是针对您要求更多技术理解的部分 - dispatch_get_main_queue();将委托(delegate)方法响应放在主线程上。由于 JS 是单线程的,因此后台线程上的任何进程都不会对其可见。
关于objective-c - React Native 和 Objective C 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51132862/