I just learned how to use the Result type recently from Sean Allen's video, and I get the idea of using it. However, while I was writing a code, there is a line I don't understand.
The line is 87 in the picture (or this -> guard let self = self else { return }
)
At first, I was just doing the same stuff as he did, but I wonder why he add the line in the code. I think he wrote it because the self can be nil and wanted to make sure if the self is not nil and return from the function if it gets nil.
And my question is
Your concern is correct. Because getFollowers
is an async task, user could go back to previous screen while the task is running. In that case, self
could be nil
and the return
is okay.
On the other hand, to make sure there's no problem with completion block of getFollowers
task, self
will be captured as strong
reference, that could cause memory leak even if user already leave that screen.
He uses weak self
to prevent that happens.