xcode debugger says terminated due to memory issue in swift 4

extension UIImageView {
func tokenImageFrom(link:String, contentMode: UIViewContentMode) {

    let headers: HTTPHeaders = [
        "Authorization" : "Bearer sH3aqTaFFP4xhGSaOTAWSoyHx8ndAXC5",
        "Accept": "application/json"
    ]

    Alamofire.request("\(StructOperation.glovalVariable.strhobbyurl)\(link)",method : .get,  headers: headers).responseJSON { response in

        DispatchQueue.global(qos: .background).async {
            if ((response.data != nil) && ((UIImage(data: response.data!)) != nil))
            {
                self.contentMode =  .scaleAspectFill

                let image = UIImage(data: response.data!)

                if response.data != nil { self.image = image}
            }
        }

    }


}

}

while trying for infinite loading of images(json) the debuggers says Terminated due to memory issue.any help is appreciated, thanks in advance.

1 answer

  • answered 2018-10-22 10:44 Ramon Vasconcelos

    Try to use weak self inside your block, you're always keeping the reference to self inside the block.

    DispatchQueue.global(qos: .background).async { [weak self] in
                if ((response.data != nil) && ((UIImage(data: response.data!)) != nil))
                {
                    self?.contentMode =  .scaleAspectFill
    
                    let image = UIImage(data: response.data!)
    
                    if response.data != nil { self?.image = image}
                }
            }
    }