jms*_*jms 7 delegates objective-c react-native
我对本机和桥接机制与本机代码的反应是相当陌生的,尤其是在框架具有委托的情况下。假设我正在尝试桥接以下框架:
@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
Run Code Online (Sandbox Code Playgroud)
以下是我实现的桥模块:
RCTBRPtouchNetworkManager.h
#import <React/RCTBridgeModule.h>
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
@interface RCTBRPtouchNetworkManager : NSObject <RCTBridgeModule, BRPtouchNetworkDelegate>
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我可以轻松地调用虚拟方法,并在日志中查看结果。但是,永远不会调用委托方法didFinishSearch()。我从javascript调用此方法,如下所示:
componentDidMount() {
let networkManager = NativeModules.BRPtouchNetworkManager;
networkManager.startSearchWithTimeout(5.0);
}
Run Code Online (Sandbox Code Playgroud)
我有我想念的东西吗?我是否正确实施了代表?这种功能是否可能(由于iOS社区很长时间使用了委托方法,因此似乎无法实现)。非常感谢您的帮助。
编辑
我发现将以下内容添加到我的网桥管理器文件中会使代表被解雇(由于这篇文章)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
Run Code Online (Sandbox Code Playgroud)
但是,即使这解决了问题,我还是希望对这里发生的事情有更多的技术了解,因为我似乎无法完全掌握它。谢谢
我知道这不是 xe2x80x99t 帖子的答案,但对于您 xe2x80x99ve 要求更多技术理解的部分 -dispatch_get_main_queue(); 将委托方法响应放在主线程上。由于 JS 是单线程的,后台线程上的任何进程都对其不可见。
\n| 归档时间: |
|
| 查看次数: |
651 次 |
| 最近记录: |