Swift Code Not Saving image to core data

I want my swift code to save a image to core data right now that is not working but there are not runtime errors. Core Data Entity is named Info attribute is imp.



class DataBaseHelper {
    
    static let shareInstance = DataBaseHelper()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    func saveImage(data: Data) {
        let imageInstance = Info(context: context)
        imageInstance.img = data
            
        do {
            try context.save()
            print("Image is saved")
        } catch {
            print(error.localizedDescription)
        }
    }
    
    func fetchImage() -> [Info] {
        var fetchingImage = [Info]()
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Info")
        
        do {
            fetchingImage = try context.fetch(fetchRequest) as! [Info]
        } catch {
            print("Error while fetching the image")
        }
        
        return fetchingImage
    }
   
   
    
  
    
   
}

Other class

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let gwen = UIImage(named: "f.jpeg")
        if let imageData = gwen.self?.pngData() {
            DataBaseHelper.shareInstance.saveImage(data: imageData)
        }

        let arr = DataBaseHelper.shareInstance.fetchImage()
        



        print("test number : ",arr)
        

Hi! If I’m not mistaken, this has already been answered for you on the Discord Server?

This topic was automatically closed after 166 days. New replies are no longer allowed.