I’m following VIPER and I wish to optimize my interactor extra for reusability. I’ve an technique in interactor known as cancelRequest()
which might be invoked from presenter to cancel ongoing community calls. Most of my interactors have this technique and I’m making an attempt to separate it out as a generic technique utilizing protocols.
protocol CancellableService {
var community: NetworkManager { get }
func cancelRequest()
}
extension CancellableService {
func cancelRequest() {
community.cancel()
}
}
protocol AnyFeatureService: AnyObject, CancellableService {
func fetch()
func deleteItem(with id: Int)
}
class MyFeatureService: AnyFeatureService {
// Implementation
}
protocol CancellableInteractor {
associatedtype ServiceType: CancellableService
var service: ServiceType! { get set }
func cancelRequest()
}
extension CancellableInteractor {
func cancelRequest() {
service.cancelRequest()
}
}
protocol MyFeatureInteractable: AnyObject, CancellableInteractor {
var presenter: MyFeaturePresentableOutput? { get set }
func fetchList()
func deleteItem(with id: Int)
}
Downside is available in MyFeatureInteractor
implementation. I don’t know learn how to declare service, that conforms to MyFeatureService
(which additionally conforms to CancellableService
) within the interactor.
With under implementation, I get
Kind ‘MyFeatureInteractor’ doesn’t conform to protocol ‘CancellableInteractor’. Presumably supposed match ‘MyFeatureInteractor.ServiceType’ (aka ‘AnyFeatureService’) doesn’t conform to ‘CancellableService’
class MyFeatureInteractor: MyFeatureInteractable {
typealias ServiceType = AnyFeatureService
weak var presenter: MyFeaturePresentableOutput?
var service: AnyFeatureService!
// func implementation
}
additionally tried declaring MyFeatureInteractable
as under, which complains
Kind ‘MyFeatureInteractor’ doesn’t conform to protocol ‘CancellableInteractor’. Candidate can’t infer ‘ServiceType’ = ‘any AnyFeatureService’ as a result of ‘any AnyFeatureService’ shouldn’t be a nominal kind and so cannot conform to ‘CancellableService’`
protocol MyFeatureInteractable: AnyObject, CancellableInteractor the place ServiceType: AnyFeatureService {
// Declarations
}
What’s the clear method to do it?