2017-10-01 1 views
1

VacationFragmentのonAttach()では、VacationViewModel - > VacationRepository、 からfeaturedDays()を呼び出しています。VacationFragmentにナビゲートするビューをクリックしたときに気付いたことは、遅延で開きます。数ミリ秒待っていますそれは、VacationFragmentを開きます。 と私はvacationViewModel.featureDays(たstartDate、endDateに) をコメントしていたときに、それは完全なチャット/コメントを読んだことはありません、誰のために遅延なぜオブジェクトを注入し、io()スレッドでapi呼び出しを行うと、Fragmentを開くのが遅くなるのですか?

class MainApplication : Application(), HasActivityInjector, HasSupportFragmentInjector { 
override fun onCreate() { 
super.onCreate() 
appComponent = DaggerAppComponent.builder().androidModule(AndroidModule(this)).build() 
appComponent.inject(this) 
} 

class VacationFragment : Fragment() { 

    override fun onAttach(context: Context?) { 
AndroidSupportInjection.inject(this) 
super.onAttach(context) 
vacationViewModel = ViewModelProviders.of(activity, icaViewModelFactory).get(VacationViewModel::class.java) 
vacationViewModel.featureDays(startDate,endDate)} 
} 

class VacationViewModel : ViewModel(), AppComponent.Injectable { 

private val disposables: CompositeDisposable = CompositeDisposable() 

    @Inject 
lateinit var vacationRepository: VacationRepository 
val featuredFeaturedCalendar: MediatorLiveData<FeaturedCalendar> = MediatorLiveData() 

fun featureDays(from: String, till: String) { 

val disposable = vacationRepository 
    .featuredCalendar(from, till, KEY, ID) 
    .map { featuredCalendar -> 
     val holidays = arrayListOf<FeaturedCalendar.Holiday>() 
     featuredCalendar.holidays.forEach { holidayItem -> 
     val holiday = FeaturedCalendar.Holiday(format.parse(holidayItem.date), holidayItem.holiday) 
     holidays.add(holiday) 
     } 
     FeaturedCalendar(featuredCalendar.status, holidays) 
    } 
    .subscribeOn(Schedulers.io()) 
    .observeOn(mainThread()) 
    .subscribe(
     { 
      featuredFeaturedCalendar.postValue(it) 
     }, 
     { 
      throw IllegalStateException(it) 
     }) 

    disposables.add(disposable) 
}} 


class VacationRepository(private val apiDaysSeResource: ApiDaysSeResource) { 

    fun featuredCalendar(from: String, till: String, key: String, id: String): Flowable<FeaturedCalendar> { 
return apiDaysSeResource.days(from, till, key, id)//.toFlowable() 
    } 

}

public interface ApiDaysSeResource { 
Flowable<FeaturedCalendar> days(@Query("fran") String fran, @Query("till") String till, @Query("key") String key, @Query("id") String id); 
} 
+0

いくつかの理由が考えられます。1. 'map()'が起点スレッドで起きています。2. 'observeOn()'は本当に遅い処理をしている可能性があります。購読した後に使い捨ては本当に「ヌル」になることができますか?あなたはその小切手が必要と思わないでください。 – milosmns

+0

@milosmns 私はチェックの部分を削除しました map()の部分とこの行のコードfeaturedFeaturedCalendar.postValue(it) と同じですが、違いはありません –

+0

Hm。さて、おそらくそれは何とか遅いモデルですか?たぶんあなたの 'onAttach()'はそれを遅くする何かをしますか? – milosmns

答えて

0

ずに断片を開きますOPのセクション - ApiDaysSeResourceクラスの問題でした。その作成には多くの時間がかかります。 onAttach()で注射が同期して起こるので、減速は始動時に明らかです。テストOPからは、減速は1.0 - 1.5秒であり、それは多くあるようだ。

空き時間がある場合は、この問題に関する長い記事と、コンストラクタにアクセスする時間がかかりすぎるとシングルトンを非同期的に挿入する方法を書きました。記事を読むにはClick here

基本的な考え方は、YourSlowThingの代わりにObservable<YourSlowThing>を挿入することです。これにより、インスタンスが使用可能になるのを待つことができますが、起動時にはnull以外の依存関係が残ります。

関連する問題