In a crud func fetch 2nd ordered item

I am getting an error of Cannot assign value of type ‘String?’ to type ‘UIImage?’ at imagePlace.image = picture.pic. I want to fetch the 2 item saved as core data binary. I added an image of my core data.

    func fetchImages() -> [Picture] {
        
        var arrImages = [Picture]()
        
        fetchRequest.returnsObjectsAsFaults = false
        
        do {
            arrImages = try context.fetch(fetchRequest) as? [Picture] ?? [Picture]()
            print("Images while fetching from coreData: \(arrImages)") // this will print all objects saved in coreData in an array form.
        } catch let error {
            print("Could not fetch images: \(error.localizedDescription)")
        }
        return arrImages
    }
     var imagePlace = UIImageView()
    
    
    var resultImages = [Picture]()
    func fetchImagesFromCoreData() {
        if let picture = DataBaseHelper.shareInstance.fetchImageBy(id:  1) {
            print("one")
           imagePlace.image = picture.pic
            
            
            resultImages = [picture]
            
            
          //  pic.image = resultImages.self
            
        } else {
            print("two")
            resultImages = []
        }
      //  resultImages = DataBaseHelper.shareInstance.fetchImagesByID(id: 2) // this will give fetched images
        
    }

Two points here:

  1. While it’s technically possible to store binary data in Core Data, that’s not the purpose of it. It won’t be efficient and syncs (if using Cloud) will become slow. There are other ways to store blobs, including Documents (which can be synchronized with the cloud if so desired).

  2. If you opt to go the Core Data route, I recommend you convert the binary data into text (non-binary) data, lots of ways to do that. You will need to decode that when loading into binary.

Unless you are doing the kind of decoding I mentioned in (2) above, I’m guessing (can’t see all the code) that you are loading from Core Data directly into the image (hence the String? assignment to UIImage? fail).

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