0
点赞
收藏
分享

微信扫一扫

weex开发 - 加载index.js崩溃,白屏(may it has been destroyed so method:fireEvent is ignored,Url must be passe)


最近,博主在做weex项目中发现了一个奇怪的问题,分别会报如下几个错误:

1.[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]
2.<Weex>[info]WXBridgeContext.m:552, No send queue for instance:<WXSDKInstance: 0x7ff1645261d0; id = 0; rootView = (null); url= (null)>, may it has been destroyed so method:fireEvent is ignored
3.<Weex>[error]WXSDKInstance.m:149, Url must be passed if you use renderWithURL

博主加载index.js代码是这么写的:

//跳转
WXDemoViewController *demo = [[WXDemoViewController alloc] init];
demo.url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/index.js",[NSBundle mainBundle].bundlePath]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:demo];
[[UIApplication sharedApplication] delegate].window.rootViewController = nav;

//加载
NSURL *URL = [self testURL: [self.url absoluteString]];
NSString *randomURL = [NSString stringWithFormat:@"%@%@random=%d",URL.absoluteString,URL.query?@"&":@"?",arc4random()];
[_instance renderWithURL:[NSURL URLWithString:randomURL] options:@{@"bundleUrl":randomURL} data:nil];

其中其他的初始化和配置博主就不一一贴出来了,不会的可以weex platform add ios来增加,weex run ios查看,也可以在生成的工程中打开target运行查看。

以上和通过weex platform add ios生成的工程写法一样,但是却莫名其妙白屏,现实url为空,博主又尝试了将这样写:

[_instance renderWithURL:[NSURL URLWithString:self.url] options:@{@"bundleUrl":self.url.absoluteString} data:nil];

//urlStr是一个字符串
demo.urlStr = [NSString stringWithFormat:@"file://%@/index.js",[NSBundle mainBundle].bundlePath];
[_instance renderWithURL:[NSURL URLWithString:urlStr] options:@{@"bundleUrl":urlStr} data:nil];

但是结果,经过[NSURL URLWithString:urlStr]之后的NSURL类型却是空,前者显示为空白,后者直接崩溃了,是不是很奇怪,博主经过断点进入到WXSDKInstance.m中发现如下代码,可以知道罪魁祸首是url=nil导致的。但是明显url不应该为空的,所以就很奇怪了。

//后者出现的问题
- (void)renderWithURL:(NSURL *)url options:(NSDictionary *)options data:(id)data
{
if (!url) {
WXLogError(@"Url must be passed if you use renderWithURL");
return;
}

self.needValidate = [[WXHandlerFactory handlerForProtocol:@protocol(WXValidateProtocol)] needValidate:url];

WXResourceRequest *request = [WXResourceRequest requestWithURL:url resourceType:WXResourceTypeMainBundle referrer:@"" cachePolicy:NSURLRequestUseProtocolCachePolicy];
[self _renderWithRequest:request options:options data:data];
[WXTracingManager startTracingWithInstanceId:self.instanceId ref:nil className:nil name:WXTNetworkHanding phase:WXTracingBegin functionName:@"renderWithURL" options:@{@"bundleUrl":url?[url absoluteString]:@"",@"threadName":WXTMainThread}];
}
在WXBridgeContext.m文件中,也有着一段代码
//前者出现的问题
- (void)executeJsMethod:(WXCallJSMethod *)method
{
WXAssertBridgeThread();

if (!method.instance) {
WXLogError(@"Instance doesn't exist!");
return;
}

NSMutableArray *sendQueue = self.sendQueue[method.instance.instanceId];
//sendQueue莫名其妙变nil了,实在是找不到任何原因,一步步运行,也毫无头绪
if (!sendQueue) {
WXLogInfo(@"No send queue for instance:%@, may it has been destroyed so method:%@ is ignored", method.instance, method.methodName);
return;
}

[sendQueue addObject:method];
[self performSelector:@selector(_sendQueueLoop) withObject:nil];
}

经过以上探索,发现似乎和instance和url有关,所以尝试直接把url写死在加载的地方:

NSString *bundleUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/index.js",[NSBundle mainBundle].bundlePath]].absoluteString;
[_instance renderWithURL:[NSURL URLWithString:bundleUrl] options:@{@"bundleUrl":bundleUrl} data:nil];

发现困扰了博主快两天的问题就这么解决了,最后的结论是没有结论,猜测可能在初始化SDK的时候有个时间差,有些东西没有初始化好,因为属性传值绝对不可能变nil的啊,也许目前这样写也只是一个权宜之计,但却真正的解决了这个问题,在官方没回复前就先这么些吧,有结果再更新。

补充一下,还有一个坑造成这个问题的原因,你的url中有中文,需要进行转码:

[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


举报
0 条评论