Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:为什么';尝试分配给变量时是否运行此Transformation.map?_Android_Firebase_Kotlin_Android Livedata - Fatal编程技术网

Android 安卓:为什么';尝试分配给变量时是否运行此Transformation.map?

Android 安卓:为什么';尝试分配给变量时是否运行此Transformation.map?,android,firebase,kotlin,android-livedata,Android,Firebase,Kotlin,Android Livedata,我试图在项目中使用Firebase API,但视图模型中变量authenticationState的Transformations.map未运行。我一直在关注谷歌的教程(链接到该项目的ViewModel) 我希望以后能够添加转换。将代码映射到FirebaseUserLiveData文件,但我似乎不明白为什么它不运行 FirebaseUserLiveData class FirebaseUserLiveData: LiveData<FirebaseUser?>() { pri

我试图在项目中使用Firebase API,但视图模型中变量
authenticationState
Transformations.map
未运行。我一直在关注谷歌的教程(链接到该项目的ViewModel)

我希望以后能够添加
转换。将
代码映射到
FirebaseUserLiveData
文件,但我似乎不明白为什么它不运行

FirebaseUserLiveData

class FirebaseUserLiveData: LiveData<FirebaseUser?>() {

    private val firebaseAuth = FirebaseAuth.getInstance()
    private val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
        value = firebaseAuth.currentUser
    }


    override fun onActive() {
        firebaseAuth.addAuthStateListener { authStateListener }
    }

    override fun onInactive() {
        firebaseAuth.removeAuthStateListener(authStateListener)
    }
}
SearchMovieFragment

class SearchMovieFragment : Fragment(), MovieSearchItemViewModel {

    companion object {
        fun newInstance() = SearchMovieFragment()
    }

    private lateinit var searchMovieFragmentViewModel: SearchMovieFragmentViewModel
    private lateinit var binding: SearchMovieFragmentBinding
    private lateinit var movieRecyclerView: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        binding = DataBindingUtil.inflate(inflater, R.layout.search_movie_fragment, container, false)
        searchMovieFragmentViewModel = ViewModelProvider(this).get(SearchMovieFragmentViewModel::class.java)
        binding.lifecycleOwner = this
        binding.viewmodel = searchMovieFragmentViewModel

        binding.signOutButton.setOnClickListener {
            AuthUI.getInstance().signOut(requireContext())
        }

        searchMovieFragmentViewModel.authenticationState.observe(viewLifecycleOwner, Observer { state ->
            when (state) {
                AUTHENTICATED -> searchMovieFragmentViewModel.signedIn = View.VISIBLE
                UNAUTHENTICATED -> searchMovieFragmentViewModel.signedIn = View.GONE
            }
        })
        return binding.root
    }
}

这是因为一旦您开始像观察
Transformations.map(FirebaseUserLiveData()){user->
一样观察
FirebaseUserLiveData()的引用,您就不会保留它。
您必须拥有要映射或传输到另一种形式的LivedataLivedata的引用

它就像一个观察链中的所有LiveData都应该被观察,或者应该有某种类型的观察者,主要用例是将某种形式的LiveData转换为您想要的内容,例如:

class YourRepository{  // your repo, that connected to a network that keeps up to date some data
  val IntegerResource: LiveData<Int> = SomeRetrofitInstance.fetchFromNetwork() //updating some resource from network
}

class YourViewModel{
  val repository = YourRepository()

 //this will start observe the repository livedata and map it to string resource
  var StringResource: Livedata<String> = Transformations.map( repository.IntegerResource ) { integerValue ->
    integerValue.toString()
}
class YourRepository{//YourRepo,连接到保持某些数据最新的网络
val IntegerResource:LiveData=SomeInstance.fetchFromNetwork()//从网络更新某些资源
}
为YourViewModel设置类{
val repository=YourRepository()
//这将开始观察存储库livedata并将其映射到字符串资源
var StringResource:Livedata=Transformations.map(repository.IntegerResource){integerValue->
integerValue.toString()的
}

我的观点是你必须保持你正在转换的LiveData的活力。希望有帮助。

应该是
。addAuthStateListener(authStateListener)
而不是
{authStateListener}

,因为FirebaseUserLiveData()没有发出任何价值。谢谢!我不敢相信我错过了!
class YourRepository{  // your repo, that connected to a network that keeps up to date some data
  val IntegerResource: LiveData<Int> = SomeRetrofitInstance.fetchFromNetwork() //updating some resource from network
}

class YourViewModel{
  val repository = YourRepository()

 //this will start observe the repository livedata and map it to string resource
  var StringResource: Livedata<String> = Transformations.map( repository.IntegerResource ) { integerValue ->
    integerValue.toString()
}