Comments (5)
The problem is that Swift has no notion of closure identity or comparison: there’s no way for Siesta to know that you’re adding “the same” closure twice, because there’s no such thing as “the same” closure.
If you add an observer that’s an object, then Siesta will prevent dups. But otherwise, it’s up to you to make sure you don’t add redundant observers — by adding them in viewDidLoad
instead of viewWillAppear
, for example.
from siesta.
Prompted by this, I added some information about duplicate observers to the API docs. Thanks for the good question!
Also, an aside on the code you posted: in Swift, when the last arg is a closure, you can place it as a block after the method call. So instead of this:
MyAPI.resource('/home').addObserver(owner: self, closure: {
[weak self] resource, event in
print(event)
})
…you can do this:
MyAPI.resource('/home').addObserver(owner: self) {
[weak self] resource, event in
print(event)
}
You may already know that, but I though I’d mention it just in case.
from siesta.
That makes a lot of sense with Swift. I guess from the API of a Siesta Service class I made an assumption that 'self' would be the qualifier for adding an extra observer to that resource. Is there a use case to adding multiple 'self' owners to a specific resource?
from siesta.
The owners are about lifecycle, not uniqueness. When the owner goes away, the observer stops observing. When you want to remove observers, you do it by owner. There’s a detailed explanation in the docs.
Is there a use case to adding multiple 'self' owners to a specific resource?
Observers have owners, not resources, so I’m not quite sure what you mean by that. I’m guessing you mean, “Is there a use case for having multiple observers with the same owner?” If so, then sure; in fact, it’s quite common. For example, a view controller might might do this:
foo.addObserver(self)
bar.addObserver(self)
bar.addObserver(owner: self) {
[weak barDisplayerWidget] in
barDisplayerWidget?.refresh()
}
Here, the VC depends on two resources, so it observes both of them (as a self-owned observer). It also owns a barDisplayerWidget
that wants to observe the second resource. All three observers are owned by self
, so when the VC is deallocated, Siesta automatically stops sending events to all of them.
This also has the advantage that you can remove multiple observers at once:
for resource in [foo, bar] {
resource.removeObservers(ownedBy: self)
}
…which is a common idiom when one VC wants to change which resource it’s observing. There’s an example of this in the sample project.
from siesta.
I understand now with that example. My pattern was to only have one observer per a resource per a controller. Having multiple closures available I can see being helpful. I will switch to using the just addObserver(self) for my current case and implementing ResourceObserver protocol.
from siesta.
Related Issues (20)
- Possible memory leak in ProgressTracker HOT 13
- tvOS Carthage compilation broken for Xcode 9 HOT 2
- Swift 4.2 + Xcode 10 HOT 5
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:66: 'UIApplicationDidReceiveMemoryWarning' has been renamed to 'UIApplication.didReceiveMemoryWarningNotification' HOT 1
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:42: Type 'NSNotification.Name' has no member 'UIApplication' HOT 1
- Readme 404 link HOT 2
- unexpected duplicate task error for new Xcode build system from Xcode 10 IOS 12.0.1 HOT 1
- How to refresh same web service ?
- Wildcard in PipelineStage.add(_, contentTypes:) doesn't work as expected
- RemoteImageView placeholderImage not showing when imageResource or ImageURL is nil HOT 2
- File-based caching for Siesta HOT 4
- Unexpected version number in 'available' attribute for non-specific platform '*' HOT 2
- Cached Resource timestamp refresh after loading
- Proposal: More elegant way to avoid memory leak in closures HOT 4
- Y'all got wiki-spammed HOT 2
- Chained request progress support
- 404 errors from a resource don't clear as expected from a resource("static url").load()
- Xcode SPM will not accept Quick@around-each dependency HOT 7
- The package product 'Alamofire' requires minimum platform version 10.0 for the tvOS platform, but this target supports 9.0 HOT 3
- Quick dependancy is not correctly versioned HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from siesta.