Skip to content

Latest commit

 

History

History
68 lines (45 loc) · 1.79 KB

README.md

File metadata and controls

68 lines (45 loc) · 1.79 KB

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