LiveData zwraca null bez aktualizowania sie - problem

0

Can someone explain me why my MutableLiveData is always "null" ?
In another project I didnt this same like here and it working. Maybe I'm blind or something and I cannot see easy errors or something.

Próbuję użyć LIveData do wyciągnięcia z API kilku rzeczy, ale nie mam pojecia czemu nie śmiga i dostaje ciągle jako wynik "null'a" i w ogóle nie wyskakuje "aktualizacja" gdy dany String jest nadpisany. Widzi ktoś jakiś błąd, który może to powodować? Z góry dzięki :)

class HomeViewModel : ViewModel() {

    val mutableLiveData = MutableLiveData<String>()

    fun setString(word: String) {
        mutableLiveData.value = word
    }

    fun getString(): LiveData<String> {
        return mutableLiveData
    }
}

class HomeFragment : Fragment() {

    lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        val textView: TextView = root.findViewById(R.id.text_home)

        homeViewModel = activity!!.run {
            ViewModelProviders.of(this).get(HomeViewModel::class.java)
        }

        homeViewModel.getString().observe(this, Observer<String>{
                t -> textView.text = t
        })

        AsyncTaskAllInvoices(ApiConnection().connectionApiInitialize(), this).execute()
        return root
    }
}

class AsyncTaskAllInvoices(val apiService: ApiService, val activity: HomeFragment) : AsyncTask<Any, Any, Any>() {
    override fun doInBackground(vararg params: Any?) {
        ApiConnection().showAllInvoices(apiService, activity)
    }
}

class ApiConnection: Fragment()  {

    private lateinit var homeViewModel: HomeViewModel

    fun connectionApiInitialize(): ApiService {
        val retrofit = Retrofit.Builder()
            .baseUrl(LocalVariables.baseApiURl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return retrofit.create(ApiService::class.java)
    }

   
    fun showAllInvoices(apiService: ApiService, activity: Fragment)  {
        homeViewModel = activity!!.run {
            ViewModelProviders.of(activity).get(HomeViewModel::class.java)
        }
        val call = apiService.getAllInvoices()
        call.enqueue(object: Callback<List<InvoiceResponse>> {
            override fun onFailure(call: Call<List<InvoiceResponse>>?, t: Throwable?) {
                Log.v("parser", "call onFailure")
            }
            override fun onResponse(call: Call<List<InvoiceResponse>>?, response: Response<List<InvoiceResponse>>?) {
                if (response != null) {
                    if (response.isSuccessful()) {
                        homeViewModel.setString(response!!.body()!![0].driver)                    
                        Log.v("parser", homeViewModel.getString().value)
                    }
                }
            }
        })
    }
}

Gdy użyje:

Log.v("parser", homeViewModel.getString().value)

w onResponse to dostaje normalnie informacje, że został String nadpisany tym co chciałem ale jak już wchodzę do miejsca, w którym chcę go użyć to dostaje null'a.

Dzieki :_)

0

fun showAllInvoices(apiService: ApiService, activity: Fragment) Przekazujesz fragment a nie aktywność, więc nie współdzielisz view modelu i w konsekwencji pracujesz na różnych LiveData. A swoją drogą ApiConnection jako fragment to niezły WTF. Czemu HomeViewModel nie może mieć ApiService w sobie?

0

Dzięki wielkie za odpowiedź. Poradziłem sobie - rozwiązaniem było złe przekazywane Activity (za dlugo czlowiek siedzi przy kompie i sie mozg lasuje). Jezeli chodzi o te ApiCOnnection i fragment to spowodowane to bylo zbyt dlugim dlubaniem w kodzie i jakie bzdury sie wkradaja :D Dzieki wielkie za odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1