Skip to content

Commit

Permalink
Merge pull request #122 from bustoutsolutions/stoppedObserving-issues
Browse files Browse the repository at this point in the history
Fixed: stoppedObserving incorrectly not called
  • Loading branch information
pcantrell authored Oct 6, 2016
2 parents ec8dbec + bcd4fb2 commit dbc4e9d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 61 deletions.
21 changes: 17 additions & 4 deletions Source/Siesta/Resource/ResourceObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,21 @@ public protocol ResourceObserver
func resourceRequestProgress(for resource: Resource, progress: Double)

/**
Called when this observer stops observing a resource. Use for making `removeObservers(ownedBy:)` trigger
other cleanup.
Called when this observer stops observing a resource, if the observer itself still exists.
Use for making `removeObservers(ownedBy:)` trigger other cleanup.
- Warning: This method is **not** called for self-owned observers when the observer itself being deallocated is
what caused it to stop observing. This is because there is no way for Siesta to know that observer is _about_
to be deallocated; it can only check whether the observer is already gone.
For example:
var myObserver = MyObserver()
resource.addObserver(myObserver) // myObserver is self-owned, so...
myObserver = nil // this deallocates it, but...
// ...myObserver never receives stoppedObserving(resource:).
In the situation above, `MyObserver` should implement any end-of-lifcycle cleanup using `deinit`.
*/
func stoppedObserving(resource: Resource)

Expand Down Expand Up @@ -335,7 +348,7 @@ internal class ObserverEntry: CustomStringConvertible

deinit
{
debugLog(.observers, [self, "removing observer whose owners are all gone:", self])
debugLog(.observers, ["removing observer of", resource, "whose owners are all gone:", self])
observer?.stoppedObserving(resource: resource)
}

Expand Down Expand Up @@ -374,7 +387,7 @@ internal class ObserverEntry: CustomStringConvertible
// Look for weak refs which refer to objects that are now gone
externalOwners.filterInPlace { $0.value != nil }

observerRef.strong = !externalOwners.isEmpty
observerRef.strong = !observerIsOwner || !externalOwners.isEmpty
}

var isDefunct: Bool
Expand Down
Loading

0 comments on commit dbc4e9d

Please sign in to comment.