Md Hanif
12/18/2020, 2:12 PMreified
and it seems to work fine with android
but throws an error in iOS
This is my common api call return in Shared file.
@Throws(Exception::class)
suspend inline fun<reified T> post(url: String, requestBody: HashMap<String, Any>?) : Either<CustomException, T> {
try {
val response = <http://httpClient.post|httpClient.post><T> {
url(BASE_URL.plus(url))
contentType(ContentType.Any)
if (requestBody != null) {
body = requestBody
}
headers.remove("Content-Type")
headers {
append("Content-Type", "application/json")
append("Accept", "application/json")
append("Time-Zone", "+05:30")
append("App-Version", "1.0.0(0)")
append("Device-Type", "0")
}
}
return Success(response)
} catch(e: Exception) {
return Failure(e as CustomException)
}
}
It works good in android if I call it like this :-
<http://api.post|api.post><MyDataClassHere>(url = "url", getBody()).fold(
{
handleError(it)
},
{
Log.d("Success", it.toString())
}
)
But I am not able to get it run on iOS devices it shows me error like this :-
`some : Error Domain=KotlinException Code=0 "unsupported call of reified inlined function `com.example.myapplication.shared.apicalls.SpaceXApi.post`" UserInfo={NSLocalizedDescription=unsupported call of reified inlined function <http://com.example.myapplication.shared.apicalls.SpaceXApi.post|com.example.myapplication.shared.apicalls.SpaceXApi.post>
, KotlinException=kotlin.IllegalStateException: unsupported call of reified inlined function <http://com.example.myapplication.shared.apicalls.SpaceXApi.post|com.example.myapplication.shared.apicalls.SpaceXApi.post>
, KotlinExceptionOrigin=}`
Any help in this is appreciated. ThanksMarc Knaup
12/18/2020, 2:15 PMMd Hanif
12/18/2020, 2:31 PMMarc Knaup
12/18/2020, 2:36 PMsuspend inline fun<reified T> post()
directly from Swift?Md Hanif
12/18/2020, 2:50 PMMarc Knaup
12/18/2020, 2:53 PMreified
functions from Swift afaik.
You’ll have to provide a Swift-specific function in Kotlin for iOS only that works without reified
and calls post()
internally.
How do you call it?Md Hanif
12/18/2020, 2:54 PMMarc Knaup
12/18/2020, 2:56 PMMarc Knaup
12/18/2020, 2:56 PMMd Hanif
12/18/2020, 2:58 PM<http://api.post|api.post>(url: "api/login", requestBody: map) { Result,arg *in*
*if* (arg == *nil*) {
Result?.fold(failed: { (error) in
}, succeeded: { (response) *in*
})
} *else* { }
}
I always knew am doing something wrong as I am not specifying what kind of response do I need in return but I didn't how to do thatMd Hanif
12/18/2020, 3:00 PMMarc Knaup
12/18/2020, 3:00 PMApiClient
in Kotlin like that:
interface ApiClient {
suspend fun logIn(…): …
suspend fun createBlogPost(…): …
// etc
}
And then use that one from Android and iOS.
The platforms don’t need to know about api/login
endpoints etc. That can be hidden behind a multiplatform interface.Marc Knaup
12/18/2020, 3:01 PMMarc Knaup
12/18/2020, 3:01 PMApiClient
functions.Md Hanif
12/18/2020, 3:03 PMJoost Klitsie
12/19/2020, 9:50 AMMd Hanif
12/19/2020, 10:51 AMMarc Knaup
12/19/2020, 10:53 AMMd Hanif
12/19/2020, 11:00 AMJoost Klitsie
12/19/2020, 11:21 AMMd Hanif
12/19/2020, 11:30 AM