Closed MarkusBux closed 3 years ago
Thank you for the PR @MarkusBux and bringing attention to the issue.
Looks like there are quite a few places in this file that the username
is assumed to be non-nil. If it's possible for it to actually be nil, it could effect the other spots as well. It would be better to mark it as optional and handle that throughout this file.
It's not clear who originally wrote this file from the git blame. @cyrusingraham, @watkyn, @macblazer Do any of you know why all the variables at the top of this file are @objc dynamic
and implicitly unwrapped?
The @objc dynamic
is required for the use of KVO to handle property updates. See Apple docs on KVO.
I haven't tried KVO with Optionals but a simple Google search seems to indicate that there may be problems with it. Somebody could give it a try to see what the current state of Swift 5 with KVO and Optionals is.
If KVO does work with Optionals, I highly recommend we move away from the implicitly unwrapped Optional and make it explicit. The code is much safer with that approach, and there are several other places in this file that check for nil as well.
Implicitly unwrapped Optional is probably OK for use with the @IBOutlet tag, but even then if the storyboard is changed and the outlet connection is dropped then the app will crash while trying to access that outlet. Explicit optionals are much better to prevent app crashes, although it's hard to know what to do in the code when a required outlet is not loaded from the storyboard.
Ah ok. Good to know, I've never worked with KVO before.
It looks like we do have a couple variables set as optional — siteName
and siteId
— and they are being used in an addObserver
method — which I assume is KVO-related. So maybe that means it's OK to use optionals now.
So I will try to change this for this file and give it a try...
KVO with explicit Optionals are working. Updated the code to use explicit instead of implicit Optionals
@hisaac, @macblazer Thanks a lot. This is my first PR in a repo not owned by myself and my every first swift related part. Just started with swift a month ago. So thanks for all the help in getting to the right direction!
This change fixes an app crash in case of cancelling an upload action.
Steps to reproduce the crash: