UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Image data won't delete from CoreData

Forums > Swift

I'm deleting image data associated with a word. The image data is stored in an attribute called 'image' in a CoreData object called 'Words'. When I relaunch my app, the image is still there! Here is my delete method:

func deleteImage(action: UIAlertAction) {
        finalImage.image = nil
        finalImage.frame(forAlignmentRect: CGRect(origin: .zero, size: CGSize(width: screenWidth, height: screenHeight)))
        drawImage.frame(forAlignmentRect: CGRect(origin: .zero, size: CGSize(width: screenWidth, height: screenHeight)))
        currentWord?.image = nil
        currentWord?.ckupload = true
        print("current image: \(currentWord?.image)")
//        currentWord?.ckimage = true
//        currentWord?.ckreference = self.currentLanguage?.ckrecordname

        saveDB()

        swiped = false

        undoImages.removeAll()
        undoLastImage = true
        enableButtons()
    }
and my saveDB() method:
    func saveDB() {
        DispatchQueue.main.async {
            self.managedObjectContext.performAndWait() {
                do {
                    try self.managedObjectContext.save()
                    appData.uploadRecords()
                } catch { print("Error") }
            }
        }
    }

The print statement produces 'nil', but I'm not sure if assigning 'nil' to the image attribute is the correct way to 'empty out' the image data attribute.

3      

Did you isolate the problem to the line where you assign the image to nil?

currentWord?.image = nil  // Did you isolate the problem to this line?

Instead try assigning a totally different image to this property. For example, add an image "simpleBox.jpg" to your assets catalog and assign this to your currentWord?.image property.

Then save.

When you reload data, do you get your old image? or the newly assigned simpleBox image?

3      

I get the "simpleBox" image. However, when I relaunch the app and go into the view controller that displays the image I get the old image! There is no sign of the new image anymore.

3      

Are you sure you aren't nil coalescing to a default image somewhere?

3      

I checked all my code, the only thing that comes close is in my ApplicationData class that handles uploading and downloading CoreData records:

if let asset = record["image"] as? CKAsset {
    let image = UIImage(contentsOfFile: asset.fileURL?.path ?? ""
    word.image = image?.pngData()
}

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.