Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed observer race condition #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Sources/ESRefreshComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import UIKit
public typealias ESRefreshHandler = (() -> ())

open class ESRefreshComponent: UIView {


private var observer: UIView?

open weak var scrollView: UIScrollView?

/// @param handler Refresh callback method
Expand All @@ -55,7 +57,6 @@ open class ESRefreshComponent: UIView {
}

/// @param tag observing
fileprivate var isObservingScrollView = false
fileprivate var isIgnoreObserving = false

public override init(frame: CGRect) {
Expand Down Expand Up @@ -171,18 +172,18 @@ extension ESRefreshComponent /* KVO methods */ {
}

fileprivate func addObserver(_ view: UIView?) {
if let scrollView = view as? UIScrollView, !isObservingScrollView {
if let scrollView = view as? UIScrollView, observer == nil {
observer = scrollView
scrollView.addObserver(self, forKeyPath: ESRefreshComponent.offsetKeyPath, options: [.initial, .new], context: &ESRefreshComponent.context)
scrollView.addObserver(self, forKeyPath: ESRefreshComponent.contentSizeKeyPath, options: [.initial, .new], context: &ESRefreshComponent.context)
isObservingScrollView = true
}
}

fileprivate func removeObserver() {
if let scrollView = superview as? UIScrollView, isObservingScrollView {
if let scrollView = observer {
scrollView.removeObserver(self, forKeyPath: ESRefreshComponent.offsetKeyPath, context: &ESRefreshComponent.context)
scrollView.removeObserver(self, forKeyPath: ESRefreshComponent.contentSizeKeyPath, context: &ESRefreshComponent.context)
isObservingScrollView = false
observer = nil
}
}

Expand Down