iosswiftcachinghaneke

How to delete cache using HanekeSwift Swift?


I am stuck with a problem. I want to populate a tableview with some text and a profile image that can change i use this function for it.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CommonCellView!
    cell = self.tableView.dequeueReusableCellWithIdentifier("CommonCellView") as! CommonCellView
    cell.nameLabel.text = self.userCollection[indexPath.row].display_name
    cell.companyLabel.text = self.userCollection[indexPath.row].user_organisation
    cell.profileImage.hnk_setImageFromURL(NSURL(string: self.userCollection[indexPath.row].profile_picture)!)
    self.makeImageViewCircular(cell.profileImage.layer, cornerRadius: cell.profileImage.frame.height)
    cell.profileImage.clipsToBounds = true

    return cell
}

nothing to suprising here. But when i change my own profile picture then i send it to the API and revist this function it shows the cached image. So i tought i might try something a bit diffent why not get all the images for every cell using Alamofire.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CommonCellView!
    cell = self.tableView.dequeueReusableCellWithIdentifier("CommonCellView") as! CommonCellView
    cell.nameLabel.text = self.userCollection[indexPath.row].display_name
    cell.companyLabel.text = self.userCollection[indexPath.row].user_organisation
   cell.profileImage.image = UIImage()
    //getting the cell image
    Alamofire.request(.GET, self.userCollection[indexPath.row].profile_picture)
        .response {(request, response, avatarData, error) in

                        let img = UIImage(data: avatarData!)
                        cell.profileImage.image = img

    }
    self.makeImageViewCircular(cell.profileImage.layer, cornerRadius: cell.profileImage.frame.height)
    cell.profileImage.clipsToBounds = true

    return cell
}

this works to a point where user scrolles very fast the image of a different user will be shown until the request gets fulfilled. Okey so make that happen somewere else and use an array for the images. I also tried that. but because its async the images would go into the array in the wrong order. So back to HanekeSwift. I read the documentation and saw i had a cache on disk but i could not clear or delete it.

to clear the cache i also tried: NSURLCache.sharedURLCache().removeAllCachedResponses()

but i did not do a thing. it works in the Alamofire situation but its not a good solution either.

I want to use hanekeSwift because HanekeSwift is fast enough to get all the images. but i want to clear the cache everytime the contoller loads.

Any suggestions would be appreciated!

Cees


Solution

  • I found a the problem.

    First i was running an older version of the pod. So after updating i could use the function.

    Shared.imageCache.removeAll()

    after you import haneke into the controller.

    the final pice of code looked like this. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: CommonCellView!
        cell = self.tableView.dequeueReusableCellWithIdentifier("CommonCellView") as! CommonCellView
        cell.nameLabel.text = self.userCollection[indexPath.row].display_name
        cell.companyLabel.text = self.userCollection[indexPath.row].user_organisation
        cell.profileImage.image = UIImage()
        //getting the cell image
        cell.profileImage.hnk_setImageFromURL(NSURL(string: self.userCollection[indexPath.row].profile_picture)!)
        //deleting it from cache
        Shared.imageCache.removeAll()
        self.makeImageViewCircular(cell.profileImage.layer, cornerRadius: cell.profileImage.frame.height)
        cell.profileImage.clipsToBounds = true
    
        return cell
    }
    

    it works now but removing the cache all the time an other cell gets filled seems a bit overkill.