【发布时间】:2018-07-02 09:25:40
【问题描述】:
我对原生代码和桥接机制与原生代码的反应非常陌生,尤其是当框架有委托时。假设我正在尝试连接以下框架:
@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
我可以轻松调用虚拟方法并在日志中查看结果。但是,从不调用委托方法 didFinishSearch()。我从 javascript 中调用它如下:
componentDidMount() {
let networkManager = NativeModules.BRPtouchNetworkManager;
networkManager.startSearchWithTimeout(5.0);
}
我有什么遗漏的吗?我是否正确实施委托?这种功能是否可能(似乎不可能,因为 iOS 社区长期使用委托方法)。非常感谢您的帮助。
编辑
我发现将以下内容添加到我的网桥管理器文件中会使委托触发(感谢this post)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
但是,尽管这解决了问题,但我希望对这里发生的事情有更技术性的了解,因为我似乎无法完全掌握它。谢谢
【问题讨论】:
标签: objective-c react-native delegates