I am working with a method which returns different types of objects, I have used the Any type return, but is there a better option to achieve this? This is my method:
override fun getNavItemById(dCSServiceContext: DCSServiceContext): Observable<Any> {
return scribeProvider.getNavItemById(dCSServiceContext).map { navItem ->
scribePresenter.presentNativeItem(navItem)
}.toObservable()
}
After I am doing a casting of the returned object, using the when operator, and I am doing something like this:
when (item) {
is NavItem -> {
if (parentItem.hasChildren) {
parentItem.items?.add(item)
recursiveItem = item
}
}
is Image -> {
if (parentItem.hasImages) {
parentItem.image = Image(item.image, item.selectedImage)
recursiveItem = parentItem
}
}
}
And my other doubt is how can I use this method and extract this type of object with another approach.
Thanks!!!