If there exists multiple retrofit call, how can i make a singleton of a retrofit, so that there won\'t be repeated codes within the class, thereby get rid of unnecessary cod
i have tried in kotlin :
class RetrofitClient{
companion object
{
var retrofit:Retrofit?=null;
fun getRetrofitObject():Retrofit?
{
if(retrofit==null)
{
synchronized(RetrofitClient ::class.java)
{
retrofit=Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("YOUR_BASE_URL")
.build()
}
}
return retrofit
}
}
}
and then:
var service:ServicesInterface?= RetrofitClient.getRetrofitObject()?.create(ServicesInterface::class.java)