Test tester
Test tester

Reputation: 113

Retrieving image with firebase gives: EXC_BAD_ACCESS (code=1, address=0x10)

So, I am trying to cache images retrieved from firebase:

class AccountSettings: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

@IBOutlet weak var profileImage: UIImageView!
var imageRef: StorageReference{
    return Storage.storage().reference().child("profile_images")
}

let cache = NSCache<NSString, UIImage>()
var activityIndicator = UIActivityIndicatorView()

override func viewDidLoad() {
    activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
    activityIndicator.hidesWhenStopped = true

    profileImage.addSubview(activityIndicator)
    activityIndicator.startAnimating()


    if let image = cache.object(forKey: "\(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString){
        profileImage.image = image
    }else{
        retrieveCurrentProfileImg()
    }

}

func retrieveCurrentProfileImg(){
    let downloadImageRef = imageRef.child("\(UserDefaults.standard.string(forKey: "unique_id")!).jpg")

    let downloadTask = downloadImageRef.getData(maxSize: 1024*1024*12) { (data, error) in
        if let data = data{
            let image = UIImage(data: data)
            self.profileImage.image = image
            self.cache.setObject(image!, forKey: "\(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString)
            self.activityIndicator.stopAnimating()
        }

        print(error ?? "No error")

    }
    downloadTask.observe(.progress) { (snapshot) in

    }
    downloadTask.resume()

}

}

At first appear, the view loads the image, but next time the view appears this happens:

I noticed that it crashes after printing "No error".

I want to know what causes my problem and how I can solve it. I think that this has something to do with completionHandlers, since the error line implies so.

I have also tried to set the cache inside my completionHandler, but nothing happens. What am I doing wrong?

Upvotes: 2

Views: 510

Answers (2)

Test tester
Test tester

Reputation: 113

I do not know why, but this seemed to do the trick:

func retrieveCurrentProfileImg(){
    var downloadImageRef = imageRef.child("\(UserDefaults.standard.string(forKey: "unique_id")!).jpg")
    print(downloadImageRef)
    downloadImageRef.getData(maxSize: 1024*1024*12) { (dataResponse, errorResponse) in
        if let data = dataResponse{
            let image = UIImage(data: data)
            self.profileImage?.image = image
        }

        print(errorResponse ?? "No error")
    }
}

If anyone could explain why this works, it would be appreciated:)

Upvotes: 0

selton
selton

Reputation: 77

please check if fetcherCompletion is wild pointer, such as adding NSLog(@"%@",weakSelf.fetcherCompletion)

If that's true, maybe it is Firebase's bug. The property of fetcherCompletion should add copy or strong.

Upvotes: 1

Related Questions