iOS swift Apple reports my app crashes during TestFlight Review

136 views Asked by At

Second time in a row got a reject on a TestFlight review from Apple. This time got crash logs from them, and apparently it appears that my app is crashing on async image download and caching (used in UITableView). Reviewed the code few times, but still have no clue what's wrong, the app working without crashes on devices and simulators, with and without the internet connection. Please take a look with a crashing line highlighted:

var imgCache = [String: UIImage]()

func downloadAndCacheImg(url: String?, completion: ((UIImage)->())?) {
    if url == nil { return }
    if let img = imgCache[url!] {
        completion?(img)
        return
    }
    if let img = UIImage(contentsOfFile: formatFileNameForCachedImg(url!)) {
        self.imgCache[url!] = img
        completion?(img)
        return
    }
    if let nsurl = NSURL(string: url!) {
        NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: nsurl), queue: NSOperationQueue.mainQueue(), completionHandler: { (response, data, error) -> Void in
            if let img = UIImage(data: data) {
                self.imgCache[url!] = img    <<<<< CRASH HERE
                data.writeToFile(self.formatFileNameForCachedImg(url!), atomically: true)
                completion?(img)
            }
        })
    }
}

func formatFileNameForCachedImg(url: String) -> String {
    let dir = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
    let dirName = dir[0] as! String
    var fname = url.stringByReplacingOccurrencesOfString("/", withString: "", options: nil, range: nil)
    let out = String(format: "%@/%@", dirName, fname)
    return out
}
0

There are 0 answers