Comments (5)
My main stack is Java Spring and Angular, so I don't have to worry about object lifetimes as much because Spring IoC takes mostly care of it.
Your solution sounds fine to me. I'll look at the PR when it's ready!
from toast-swift.
Hi @julianpomper,
While developing a PR I realised a similar behaviour with pan gesture that made me wonder.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
Toast.text("I am a text").show() // pan gesture is enabled by default
}
}
}
When running the application with the above the pan gesture was stopped working or was not working all the times.
As bad as it sounds there is a hidden problem with the above code. Toast.text()
returns a Banner object which is not referenced by any variable (Hence ARC is 0).
And because of that the object is deallocated from memory. Thus, the panGesture is not going to be called any more (as it is living in Toast class).
The gesture works well for me if I convert the above code to:
class ViewController: UIViewController {
var toast: Toast! // Adds 1 to ARC so that the Toast is not deallocated.
override func viewDidLoad() {
super.viewDidLoad()
self.toast = Toast.text("I am a text")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.toast.show() // pan gesture is enabled by default
}
}
}
@BastiaanJansen I do have some ideas but I want to hear if you are thinking of any solution first 😄
from toast-swift.
Hi @Nikoloutsos
Thanks for the explanation. Swift and iOS development is not my main stack, so any suggestions and PR's are appreciated!
Of course always having to define a toast variable isn't preferred. So I would like to hear your ideas.
from toast-swift.
No worries, OOC may I know what's your main stack 😄?
My idea to tackle this problem is simple. I think the lifetime of the Toast must be the same with the ToastView.
ToastView has a guaranteed reference from topViewController that is displaying it since it is its subview.
So a quick solution would be to refactor the ToastView to strongly reference the Toast instance.
That way when toastView is removed from controller the Toast instance will be deallocated from memory and will have fulfil it's purpose.
But I want to think it more and see if I come up with a better solution than this
from toast-swift.
In AppleToastView, making the toast variable strongly referenced and overriding removeFromSuperview to prevent the reference cycle fixes the issue.
private var toast: Toast?
public override func removeFromSuperview() {
super.removeFromSuperview()
self.toast = nil
}
from toast-swift.
Related Issues (20)
- Custom toast view HOT 1
- Dismiss callback HOT 2
- Custom styles HOT 1
- Running resolver because the following dependencies were added: 'toast-swift'
- Support animation type HOT 2
- Image doesn't show up
- Toast bottom direction broke swipe to remove HOT 14
- Add snapshot tests HOT 2
- Localization Support
- Binary operator '>' cannot be applied to operands of type 'CGFloat' and 'Double' HOT 2
- I have a problem. A really, really, big problem. HOT 7
- Number of lines option HOT 4
- Background colour customisation HOT 1
- unable to modify the color of the toast icon HOT 1
- There is a library with the same name. HOT 3
- Add visionOS support
- UIStackView.init(frame:) must be used from main thread only HOT 2
- Will strong references to "view" lead to memory leaks?
- when i add an image to the toastview it animates the image
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 toast-swift.