Git Product home page Git Product logo

Comments (5)

BastiaanJansen avatar BastiaanJansen commented on June 7, 2024 1

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.

Nikoloutsos avatar Nikoloutsos commented on June 7, 2024

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.

BastiaanJansen avatar BastiaanJansen commented on June 7, 2024

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.

Nikoloutsos avatar Nikoloutsos commented on June 7, 2024

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.

efehelvaci avatar efehelvaci commented on June 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.