Hi community,
Thank you for taking the time to read this.
Currently, I’m working on a sample project to better understand how to address various challenges involved in migrating from Swift 5 to Swift 6.
Now I’m encountering a challenge that I believe is quite common.
final class MarvelWorldRepository: MarvelWorldRepositoryInput {
weak var output: (any MarvelWorldRepositoryOutput)? = nil
private let marvelCharacterServiceInput: any MarvelCharacterServiceInput
private let marvelBinaryDataSource: any MarvelBinaryDataSource
init(
marvelCharacterServiceInput: any MarvelCharacterServiceInput,
marvelBinaryDataSource: any MarvelBinaryDataSource
) {
self.marvelCharacterServiceInput = marvelCharacterServiceInput
self.marvelBinaryDataSource = marvelBinaryDataSource
}
func getMarvelCharacter(_ identifier: String) {
Task { [weak self] in
guard let self else {
return
}
do {
let character = try await marvelCharacterServiceInput.getCharacter(identifier)
output?.characterResponse(.success(character))
} catch {
print("Something went wrong \(error.localizedDescription)")
}
}
}
}
And the compiler in swift 5 mode with complete concurrency enabled warns.
I have a couple of thoughts on this. First, I considered making the MarvelWorldRepository
an actor, but that requires using await
and involves an external task when calling the repository, which is what I wanted to avoid.
My second thought was to change the output variable to a let
, which resolves the warning. However, that means you can't set it outside of the initialization, and of course, the reference cycle isn't broken due to the lack of a weak reference.
Any advice on this would be greatly appreciated.