1

I am trying to save two types of arrays using Core Data. One array contains UIimages, and the other Array contains URL's of videos. I am able to successfully save them using this method below.

 let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newVideo = NSEntityDescription.insertNewObject(forEntityName: "Content", into: context)
    videosArray.append(session.outputURL!)
    let thumbnail = self.getThumbnail(session.outputURL!)
    thumbnails.append(thumbnail)
    newVideo.setValue(videosArray, forKey: "videos")
    newVideo.setValue(thumbnails, forKey: "thumbnails")

    do {

        try context.save()
        print("Save")

    } catch {
        print("Error")
        // Process error
    }

I get the Save message printed out. However while trying to load them in the collection view i get a crash.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Content")
    request.returnsObjectsAsFaults = false

    do {
        let results = try context.fetch(request)
        if results.count > 0 {
            for result in results as! [NSManagedObject] {
                if let fetchedThumbnails = (result).value(forKey: "thumbnails") as? Array<Any> {
                    return fetchedThumbnails.count
                }
            }
        }
    } catch {
        print("There was a crash fetching content")
    }

    return 0
}

The collection view is supposed to return as many thumbnails that are in the array. However it crashes and takes me to the app delegate file.

I set a breakpoint at

let results = try context.fetch(request)

And it goes to the breakpoint.

Then i set another breakpoint at

if results.count > 0 {

and the app crashes and takes me to the app delegate with this error:

Terminating app due to uncaught exception 
'NSInvalidArgumentException', 
reason: '-[Content initWithCoder:]: 
unrecognized selector sent to instance 0x174a6d840'

Image of how I Declare Entity

5
  • 1
    Can you add the declaration of type Content? Commented Aug 2, 2017 at 5:51
  • Do you mean when i declare it in my core data file? Commented Aug 2, 2017 at 5:56
  • @NandiinBao I added an image Commented Aug 2, 2017 at 6:02
  • Given the details you present, I can only tell that the Content type somehow does not conform to the protocol NSCoding and the CoreData framework needs it to conform to that protocol. initWithCoder: is a method declared in the NSCoding protocol. CoreData assumes the entityName you passed in conforms to it and called initWithCoder: upon it and then crash occurred Commented Aug 2, 2017 at 6:02
  • I'm sorry i made a typo, the array doesn't contain URL's for images, it contains the pure UIImage. Is this what is causing my problem? Commented Aug 2, 2017 at 6:37

1 Answer 1

1

You will need to conform to NSCoding protocol for your Content entity. You will need to implement following two methods to do so

required init?(coder aDecoder: NSCoder) {

}

func encodeWithCoder(aCoder: NSCoder) {
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please show me how to do this. It is my first time using core data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.