meituan / WMRouter

WMRouter是一款Android路由框架,基于组件化的设计思路,有功能灵活、使用简单的特点。
https://tech.meituan.com/meituan_waimai_android_open_source_routing_framework.html
Apache License 2.0
2.31k stars 342 forks source link

Application的onCreate中在子线程使用Router.lazyInit(),同时onCreate的主线程中使用了Router.getService,导致偶现路由跳转页面失败 #73

Open jiangming8 opened 4 years ago

jiangming8 commented 4 years ago

如题,在Application的onCreate方法中,在使用后台线程进行懒加载Router.lazyInit()。同时onCreate的主线程中使用了Router.getService,导致偶现路由跳转页面失败。调试后发现是路由表ServiceLoaderInit类的init方法还没有执行完成,就开始调用了UriAnnotationHandler的performInit方法。

请问这种情况应该怎么处理。

` public class MApplication extends Application {

public void onCreate() {
       DefaultRootUriHandler rootHandler = new DefaultRootUriHandler(context);
       Router.init(rootHandler);
       // 懒加载后台初始化(可选)
       new AsyncTask<Void, Void, Void>() {
          @Override
          protected Void doInBackground(Void... voids) {
              Router.lazyInit();
              return null;
          }
      }.execute();
     ...
     Router.getService(IHomeMineModule.class, "homemine");
}

}

`

jzj1993 commented 4 years ago

Router.lazyInit()和UriAnnotationHandler中的init都调用了LazyInitHelper,有用synchronized做线程同步处理。理论上来说如果lazyInit还没执行完,主线程会等待lazyInit执行结束再跳转,不会出现跳转失败的情况。