I have successfully implemented a closure in class to get filtered contacts from my phonebook but when I call this closure it creates a leak, I tested it in Xcode instrument tool.
See my implementation,
class CR: NSObject {
func GetAllSBUser(handler:@escaping (Array<SBUserModel>?, Error?) -> ()) {
CRBlock = handler
if self.AllUSersModels.count>0 {
self.CRBlock(self.AllUSersModels, nil)
} else {
self.CRBlock(nil, err)
}}}
I use this method in another class, see my implementation.
I also have a global instance in my app delegate like this
let app = UIApplication.shared.delegate as! AppDelegate
class friendsVC: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.app.cri?.AllSBFriends(handler: { (SBfriendsUIDs, error) in
if error == nil{
// Do something with list
} else{ self.friendsCountLbl.text = "Friends \(0)" }
})
}
}
In class friendsVC, this method produces a leak. How can I remove it? Should I use [unowned self] or weak? [unowned self] or weak may create a crash in some special cases of retain cycle. Please suggest me, how to fix it.