Avoiding race conditions in Kotlin, Smartcast is impossible runtime exception
Avoiding race conditions in Kotlin, Smartcast is impossible runtime exception
The line:
viewAdapter.setTitleData(bezinningModels)
in the below snippet is giving me the runtime warning:
Smart cast to 'BezinningAdapter' is impossible, because 'viewAdapter'
is a mutable property that could have been changed by this time
I went through a lot of posts with a similar runtime warning, and I understood why this happens. Basically Kotlin wants to avoid race conditions so that the value of a mutable object could not change,but still I do not get what I should do to avoid this mistake so that I can call the retrofit call into the RecyclerView Array
I tried to change from var
to val
the value of RecyclerView
but did not work
RecyclerView Array
var
val
RecyclerView
Here the snippet that causes the problem:
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
class MainFragment : android.app.Fragment()
private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater!!.inflate(R.layout.bezinning_fragment, container, false)
viewManager=LinearLayoutManager(activity)
viewAdapter=BezinningAdapter()
recyclerView = view.findViewById(R.id.recycler_view) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager=viewManager
recyclerView.adapter=viewAdapter
val subscribe = bezinningListViewModel!!.showBezinningLijst("132")
?.subscribeOn(schedulerProvider!!.io())
?.observeOn(schedulerProvider!!.ui())
?.subscribe(object : FlowableSubscriber<List<BezinningModel>>
override fun onError(t: Throwable?)
Log.d("IVO", "onError: ")
override fun onComplete()
Log.d("IVO", "onComplete: ")
override fun onSubscribe(s: Subscription)
s.request(Long.MAX_VALUE);
override fun onNext(bezinningModels: List<BezinningModel>?)
val JSONResponseBody = bezinningModels!!
[0].attribute.body
viewAdapter.setTitleData(bezinningModels)
val bodyBezinningParsedHtml =
Html.fromHtml(JSONResponseBody)
)
return view
val viewAdapter = BezziningAdapter(); viewAdapter.whatever(); /*...*/ this.viewAdapter = viewAdapter
val
1 Answer
1
If you know that viewAdapter
is going to be a BezinningAdapter
, then declare it as such:
viewAdapter
BezinningAdapter
private lateinit var viewAdapter: BezinningAdapter<*>
Thanks. it is working if I do like that
private lateinit var viewAdapter: BezinningAdapter
without the diamond. So I am starting to understand, the reason why I get this Smartcast exception is the fact that I am kind of initializing it twice?– Drocchio
Aug 20 at 17:14
private lateinit var viewAdapter: BezinningAdapter
It has to be casted in general because you declared
viewAdapter
as a RecyclerView.Adapter
at first and then assigned it to a BezinningAdapter
later. Smartcast isn't possible here (on var
types) because the reference could be any subtype of RecyclerView.Adapter
at any time.– natonomo
Aug 20 at 17:22
viewAdapter
RecyclerView.Adapter
BezinningAdapter
var
RecyclerView.Adapter
thank you makes perfectly sense
– Drocchio
Aug 20 at 17:31
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you can also always smart-cast local variables. so something like
val viewAdapter = BezziningAdapter(); viewAdapter.whatever(); /*...*/ this.viewAdapter = viewAdapter
would work in the general case, since aval
stack variable will never change value– Groostav
Aug 20 at 22:19