diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 8f747c51b..fc5552856 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -257,9 +257,8 @@ open class FenixApplication : LocaleAwareApplication(), Provider { // no-op, LeakCanary is disabled by default } - /** - * See [TopsiteStore.prefetch] for details on pre fetching. - */ + // This is for issue https://github.com/mozilla-mobile/fenix/issues/11660. We prefetch our info for startup + // so that we're sure that we have all the data available as our fragment is launched. private fun prefetchForHomeFragment() { StrictMode.allowThreadDiskReads().resetPoliciesAfter { components.core.topSiteStorage.prefetch() diff --git a/app/src/main/java/org/mozilla/fenix/components/TopSiteStorage.kt b/app/src/main/java/org/mozilla/fenix/components/TopSiteStorage.kt index 854cfa3bc..c7c0347bd 100644 --- a/app/src/main/java/org/mozilla/fenix/components/TopSiteStorage.kt +++ b/app/src/main/java/org/mozilla/fenix/components/TopSiteStorage.kt @@ -14,7 +14,7 @@ import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSiteStorage import mozilla.components.support.locale.LocaleManager import org.mozilla.fenix.R -import org.mozilla.fenix.ext.observeOnceAndRemoveObserver +import org.mozilla.fenix.ext.observeOnce import org.mozilla.fenix.ext.settings import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.settings.advanced.getSelectedLocale @@ -88,15 +88,8 @@ class TopSiteStorage(private val context: Context) { } } - /** - * This is for issue https://github.com/mozilla-mobile/fenix/issues/11660. We prefetch the top - * sites for startup so that we're sure that we have all the data available as our fragment is - * launched to make sure that we can display everything on the home screen on the first drawing pass. - * This method doesn't negatively affect performance since the [getTopSites] runs on the a - * background thread. - */ fun prefetch() { - getTopSites().observeOnceAndRemoveObserver { + getTopSites().observeOnce { cachedTopSites = it } } diff --git a/app/src/main/java/org/mozilla/fenix/ext/LiveData.kt b/app/src/main/java/org/mozilla/fenix/ext/LiveData.kt index 5b082f2fc..718385219 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/LiveData.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/LiveData.kt @@ -10,11 +10,11 @@ import androidx.lifecycle.Observer /** * Observe a LiveData once and unregister from it as soon as the live data returns a value */ -fun LiveData.observeOnceAndRemoveObserver(callback: (T) -> Unit) { +fun LiveData.observeOnce(observer: (T) -> Unit) { observeForever(object : Observer { override fun onChanged(value: T) { removeObserver(this) - callback(value) + observer(value) } }) }