问题描述
首次在项目中集成weex的SDK(sdk版本是0.8.0,Xcode 8),但是出现一些问题,
1. sdk放在podfile同级出现各种编译不过
这个问题没有截图,主要是由于 Test里面的代码,会有很多非arc的代码,后来我仿照playground的格式,将sdk代码放到项目根目录的外面,可以顺利解决编译不过的问题
2. js文件加载不出来
当我集成好了sdk,做好初始化,去实例化一个_instance,去加载一个已经编译好的js文件时,出现以下错误<Weex>[info]WXBridgeContext.m:292, No send queue for instance:1, may it has been destroyed so method:fireEvent is ignored [;
并且所有的渲染工作都没有做
我将同样的js文件放到playground中,就可以顺利的渲染成功,一直都不明白该如何解决,找到最近一次打印的代码,
- (void)executeJsMethod:(WXBridgeMethod *)method
{
WXAssertBridgeThread();
if (!method.instance) {
WXLogError(@"Instance doesn't exist!");
return;
}
NSMutableArray *sendQueue = self.sendQueue[method.instance];
if (!sendQueue) {
WXLogInfo(@"No send queue for instance:%@, may it has been destroyed so method:%@ is ignored", method.instance, method.method);
return;
}
[sendQueue addObject:method];
[self performSelector:@selector(_sendQueueLoop) withObject:nil];
}
该问题还未解决,解决后来更新答案
更新
对比了一下,playground和自己项目的代码流程,貌似项目中的代码,网络请求灭有回调
__block NSURLResponse *urlResponse;
[networkHandler sendRequest:request
withSendingData:^(int64_t bytesSent, int64_t totalBytes) {}
withResponse:^(NSURLResponse *response) {
urlResponse = response;
}
withReceiveData:^(NSData *data) {}
withCompeletion:^(NSData *totalData, NSError *error) {
//TODO 304
if (!error && [urlResponse isKindOfClass:[NSHTTPURLResponse class]] && ((NSHTTPURLResponse *)urlResponse).statusCode != 200) {
error = [NSError errorWithDomain:WX_ERROR_DOMAIN
code:((NSHTTPURLResponse *)urlResponse).statusCode
userInfo:@{@"message":@"status code error."}];
}
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Connection to %@ occurs an error:%@", request.URL, error.localizedDescription];
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_DOWNLOAD, errorMessage, weakSelf.pageName);
if (weakSelf.onFailed) {
weakSelf.onFailed(error);
}
return;
}
if (!totalData) {
NSString *errorMessage = [NSString stringWithFormat:@"Connection to %@ but no data return", request.URL];
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_DOWNLOAD, errorMessage, weakSelf.pageName);
if (weakSelf.onFailed) {
weakSelf.onFailed(error);
}
return;
}
NSString *script = [[NSString alloc] initWithData:totalData encoding:NSUTF8StringEncoding];
if (!script) {
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_STRING_CONVERT, @"data converting to string failed.", weakSelf.pageName)
return;
}
WX_MONITOR_SUCCESS_ON_PAGE(WXMTJSDownload, weakSelf.pageName);
WX_MONITOR_INSTANCE_PERF_END(WXPTJSDownload, weakSelf);
[weakSelf renderView:script options:newOptions data:data];
}];
再次更新
经过多次调试发现以下问题:
WX_MONITOR_INSTANCE_PERF_START(WXPTJSDownload, self);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:[WXUtility userAgent] forHTTPHeaderField:@"User-Agent"];
[request setValue:@"weex" forHTTPHeaderField:@"f-refer"];
id<WXNetworkProtocol> networkHandler = [self networkHandler];
__block NSURLResponse *urlResponse;
[networkHandler sendRequest:request
withSendingData:^(int64_t bytesSent, int64_t totalBytes) {}
withResponse:^(NSURLResponse *response) {
urlResponse = response;
}
withReceiveData:^(NSData *data) {}
withCompeletion:^(NSData *totalData, NSError *error) {
//TODO 304
if (!error && [urlResponse isKindOfClass:[NSHTTPURLResponse class]] && ((NSHTTPURLResponse *)urlResponse).statusCode != 200) {
error = [NSError errorWithDomain:WX_ERROR_DOMAIN
code:((NSHTTPURLResponse *)urlResponse).statusCode
userInfo:@{@"message":@"status code error."}];
}
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Connection to %@ occurs an error:%@", request.URL, error.localizedDescription];
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_DOWNLOAD, errorMessage, weakSelf.pageName);
if (weakSelf.onFailed) {
weakSelf.onFailed(error);
}
return;
}
if (!totalData) {
NSString *errorMessage = [NSString stringWithFormat:@"Connection to %@ but no data return", request.URL];
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_DOWNLOAD, errorMessage, weakSelf.pageName);
if (weakSelf.onFailed) {
weakSelf.onFailed(error);
}
return;
}
NSString *script = [[NSString alloc] initWithData:totalData encoding:NSUTF8StringEncoding];
if (!script) {
WX_MONITOR_FAIL_ON_PAGE(WXMTJSDownload, WX_ERR_JSBUNDLE_STRING_CONVERT, @"data converting to string failed.", weakSelf.pageName)
return;
}
WX_MONITOR_SUCCESS_ON_PAGE(WXMTJSDownload, weakSelf.pageName);
WX_MONITOR_INSTANCE_PERF_END(WXPTJSDownload, weakSelf);
[weakSelf renderView:script options:newOptions data:data];
}];
在我的项目中,当程序执行到这个请求js文件的网络方法时,不会走WXNetworkDefaultImpl
中的创建请求的方法:
- (id)sendRequest:(NSURLRequest *)request withSendingData:(void (^)(int64_t, int64_t))sendDataCallback
withResponse:(void (^)(NSURLResponse *))responseCallback
withReceiveData:(void (^)(NSData *))receiveDataCallback
withCompeletion:(void (^)(NSData *, NSError *))compeletionCallback
{
WXNetworkCallbackInfo *info = [WXNetworkCallbackInfo new];
info.sendDataCallback = sendDataCallback;
info.responseCallback = responseCallback;
info.receiveDataCallback = receiveDataCallback;
info.compeletionCallback = compeletionCallback;
if (!_session) {
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
}
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
if (!_callbacks) {
_callbacks = [NSMutableDictionary dictionary];
}
[_callbacks setObject:info forKey:task];
[task resume];
return task;
}
所以导致js文件请求根本就没有发出来,就去执行了WXBridgeContext.m
中的,下列代码,由于sendQueue = nil
然后结束了运行
- (void)executeJsMethod:(WXBridgeMethod *)method
{
WXAssertBridgeThread();
if (!method.instance) {
WXLogError(@"Instance doesn't exist!");
return;
}
NSMutableArray *sendQueue = self.sendQueue[method.instance];
if (!sendQueue) {
WXLogInfo(@"No send queue for instance:%@, may it has been destroyed so method:%@ is ignored", method.instance, method.method);
return;
}
[sendQueue addObject:method];
[self performSelector:@selector(_sendQueueLoop) withObject:nil];
}
表示求同学解答下哈,实在是有点不太明白,我的所有配置和playground中一致,为啥会出现这样的情况呢
解决了 这是一个尴尬的结果
是因为我写的初始化sdk的方法没有调用 : )