Chr*_*eid 6 objective-c ios react-native
我正在编写一个本地模块,该模块从我无法访问的封闭源框架二进制文件中调用函数。它阻止网络呼叫并激活网络指示灯。我希望能够在后台异步运行它,这是开箱即用的,但是当我这样做时,我得到了这个警告/错误,并且调用停滞了一点:
Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:]
如果在我的RN模块中使用以下代码将其放在主队列中,则调用将阻塞所有操作,直到完成。
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
Run Code Online (Sandbox Code Playgroud)
这是暴露给JS的方法的示例:
RCT_REMAP_METHOD(createSession,
userIDtoken:(NSString *)userIDtoken
createSessionWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Boolean connected = [_networkController startSession:userIDtoken isTest:true];
NSString *success = @"session created";
if (connected){
resolve(success);
} else {
NSError *error = [NSError errorWithDomain:@"ConnectionError" code:1 userInfo:nil];
reject(@"session_creation_failed", @"Cannot create sessions", error);
}
}
Run Code Online (Sandbox Code Playgroud)
_networkController startSession
应该在后台运行而不会引发警告/错误,也不会阻塞主线程。有没有办法做到这一点?
我通过扩展 UIApplication 找到了解决方案。
创建一个main.h
并声明一个MyApplication
扩展的类UIApplication
:
#import <UIKit/UIKit.h>
@interface MyApplication : UIApplication
@end
Run Code Online (Sandbox Code Playgroud)
然后通过覆盖setNetworkActivityIndicatorVisible
in来实现它main.m
并将其传递给UIApplicationMain
:
#import <UIKit/UIKit.h>
#import "main.h"
#import "AppDelegate.h"
@implementation MyApplication
- (void)setNetworkActivityIndicatorVisible:(BOOL)setVisible {
// do nothing or something
}
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(
argc,
argv,
NSStringFromClass([MyApplication class]),
NSStringFromClass([AppDelegate class]));
}
}
Run Code Online (Sandbox Code Playgroud)