Skip to content

DamianW93/Leakify

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Leakify

Functional way to prevent memory leaks

Version License Platform

Tired of writing `[unowned/weak self] closures on every callback function? You might consider using passing method as a parameter with little help from Leakify.

Usually we encourage sytuation like this

    class Foo {
    
        let service: BazService

        func foo() {
            service.bar(then: { [unowned self] p1, p2 in self.handleResponse(param1: p1, param2: p2) })
        }
        
        func handleResponse(param1: A, param2: B) {
        
        }
    }

Woudn't be great if we just pass function as a parameter?

    
    func foo() {
        service.bar(then: handleResponse)
    }

Yeah, but there is one big con of that approach. We ended up with nice Strong reference cycle between service and Foo instance. What can we do now?

We can import Leakify to our project and use one of it's higher-order fuctions for avoiding strong reference cycles

    func foo() {
        service.bar(then: unown(self, self.lk.handleResponse)
    }

}

Installation

Leakify is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Leakify'

Author

Michał Wójtowicz

License

Leakify is available under the MIT license. See the LICENSE file for more info.

References

Inspired by this article

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 92.0%
  • Ruby 8.0%