Стефан Јовановић
11/12/2023, 10:25 AMfun fetchProducts(limit: Int): Flow<RequestState<List<Product>>> {
return flow {
emit(RequestState.Loading)
// Delay to show a loading state.
delay(2000)
try {
emit(
RequestState.Success(
data = httpClient.get(urlString = "${baseUrl}products?limit=$limit").body()
)
)
} catch (e: Exception) {
emit(RequestState.Error(e.message.toString()))
}
}
}
Filip Dolník
11/12/2023, 11:33 AMList<Product>
type is usually converted to [Product]
- which would retain the type. However, here the type is used as a type argument (in generics) which cannot be converted to [Product]
( Swift Array) because it’s not a reference type which is required for Obj-C type arguments. Therefore it’s automatically converted by the Swift compiler to NSArray
instead - which does not have generics in Swift and therefore the Product
type is lost.
(but if you had just RequestState<Product>
then it would work)Стефан Јовановић
11/12/2023, 11:58 AMval items: List<Product>
If I understand correctly, the subclasses of a sealed class will be converted as regular classes in Swift. As a someone who's coming from Android world, I would want to declare a variable something like:
val fetchedProducts: MutableState<RequestState<Products>> = remember {
mutableStateOf(RequestState.Idle)
}
LaunchedEffect(key1 = Unit) {
ProductApi().fetchProducts(limit = 10).collectLatest {
fetchedProducts.value = it
}
}
But in swift a RequestStateIdle class is not recognized as a subclass of RequestState? 🤔Filip Dolník
11/12/2023, 12:36 PMclass Idle<T> : RequestState<T>
and then in Swift wrote = RequestStateIdle<Products>()
response: RequestState<Products> = RequestStateIdle() as! RequestState<Products>
That wouldn’t work with Swift objects but with Obj-C objects it should.Стефан Јовановић
11/12/2023, 12:52 PMFilip Dolník
11/13/2023, 7:30 AM