The problem is that sometimes (not always) my completion handler become nil.
I've already tried [weak self] but it doesn't work.
func setup() {
_collectionView.refreshControl?.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)
}
@objc private func refresh(sender: UIRefreshControl) {
print("request1")
request(refresh: true, completion: { () -> Void in
// When completion become nil, this is not called.
(sender as UIRefreshControl).endRefreshing()
print("completion1")
})
}
fileprivate func request(refresh: Bool, completion: (() -> Void)? = nil) {
guard !isFetchInProgress else {
return
}
isFetchInProgress = true
APIRequest.request(request: request).success { [weak self] result in
Log.debugLog(“success!:%@“, result)
if let _self = self {
if RequestResponseManager.sharedInstance.handleResponse(response: result) {
if let _data = result.data {
print(_data)
}
}
}.failure { error, _ in
Log.debugLog(“%@“, error.debugDescription)
completion?()
isFetchInProgress = false
}.then { _,_ in
Log.debugLog(“then!“)
// SOMETIMES (not always) completion?() become nil
completion?()
isFetchInProgress =false
}
}
public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var target: UICollectionReusableView? = nil
if kind == UICollectionView.elementKindSectionFooter {
target = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerId, for: indexPath)
if isNeedRequest {
print("request2")
self.request(refresh: false)
}
}
return target!
}
1、When scroll down & up speed is normal, log shows the below and that's fine.
request1
completion1
request2
2、However when scroll down & up speed is faster, log shows the below and never called completion1 and stopped refreshControl.
request1
request2
I wonder how I can handle it properly? Thank you.
isFetchInProgressis true, the API request never even starts and the completion handler is never called. - Rengerscompletionis strongly referenced and may not in any case be set tonil, not even explicitly. The only way this isnilbecause it is already injected asnil. Ergo somewhere something is callingrequest(refresh: flag, completion: nil). - Matic Oblakrequest(refresh: false)is called from other method when the collectionView is scrolled to the end. How can I avoid@objc private func refresh(sender: UIRefreshControl)completionis injected as nil? - Alea