0

I have two entities

@objc(Movies)
public class Movies: NSManagedObject {

}
extension Movies {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Movies> {
        return NSFetchRequest<Movies>(entityName: "Movies")
    }

    @NSManaged public var id: Int64
    @NSManaged public var isFav: Bool
    @NSManaged public var overview: String?
    @NSManaged public var poster: String?
    @NSManaged public var release_date: String?
    @NSManaged public var title: String?
    @NSManaged public var genresID: NSSet?

}

// MARK: Generated accessors for genresID
extension Movies {

    @objc(addGenresIDObject:)
    @NSManaged public func addToGenresID(_ value: GenresID)

    @objc(removeGenresIDObject:)
    @NSManaged public func removeFromGenresID(_ value: GenresID)

    @objc(addGenresID:)
    @NSManaged public func addToGenresID(_ values: NSSet)

    @objc(removeGenresID:)
    @NSManaged public func removeFromGenresID(_ values: NSSet)

}
@objc(GenresID)
public class GenresID: NSManagedObject {

}
extension GenresID {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<GenresID> {
        return NSFetchRequest<GenresID>(entityName: "GenresID")
    }

    @NSManaged public var id: Int64
    @NSManaged public var name: String?
    @NSManaged public var movieID: Movies?

}

When I click a button an action is triggered to save a movie, and then I would like to save that movie that has been "favored". A movie can have multiples genres (one-to-many relationship).

Method action:

func saveMoviesDB (movie: Movie) {
        
        guard let appDelegate =
            UIApplication.shared.delegate as? AppDelegate else {
                return
        }
    
        let managedContext = appDelegate.persistentContainer.viewContext
        let entityMovie = NSEntityDescription.entity(forEntityName: "Movies", in: managedContext)!
        let entityGenres = NSEntityDescription.entity(forEntityName: "GenresID", in: managedContext)!
        let movieDB = NSManagedObject(entity: entityMovie, insertInto: managedContext)
        let genresDB = NSManagedObject(entity: entityGenres, insertInto: managedContext)
        
        movieDB.setValue(movie.id, forKey: "id")
        movieDB.setValue(movie.title, forKey: "title")
        movieDB.setValue(movie.isFav, forKey: "isFav")
        movieDB.setValue(movie.poster, forKey: "poster")
        movieDB.setValue(movie.overview, forKey: "overview")
        movieDB.setValue(movie.releaseDate, forKey: "release_date")
        
        do{
            try managedContext.save()
            moviesDB.append(movieDB)
            
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }
    }

I don't know how to assign multiple genresID to the Movie. For each movie, there is an Int array containing the Ids of the genres.

EDIT: I decided to remove the genre entity and create a transformable type property in Movies to store the arrays of Int. It went like this:

extension Movies {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Movies> {
        return NSFetchRequest<Movies>(entityName: "Movies")
    }

    @NSManaged public var id: Int64
    @NSManaged public var isFav: Bool
    @NSManaged public var overview: String?
    @NSManaged public var poster: String?
    @NSManaged public var release_date: String?
    @NSManaged public var title: String?
    @NSManaged public var genresID: [NSNumber]?

}

And then I cast it before saving it to the database

let genresID = movie.genre as [NSNumber]
Eduardo
  • 25
  • 5
  • Why do you have an array of the genre ID's rather than GenresID objects instead? – Joakim Danielson Jul 30 '20 at 19:39
  • @JoakimDanielson It's array genre ID's is fetched from an API. In method saveMoviesDB I get a movie that has a movie.id attribute which is an array of int. I want to assign this array to genreID and then assign it to the Movie – Eduardo Jul 30 '20 at 19:51
  • @JoakimDanielson thank's for the comment – Eduardo Jul 30 '20 at 19:51
  • Then create a fetch request with a predicate that fetches all genre objects for those ID's and add that array of objects to your movie. See [this question](https://stackoverflow.com/questions/51264432/how-to-fetch-array-of-objects-using-array-of-ids-using-core-data) for instance – Joakim Danielson Jul 30 '20 at 19:55
  • @JoakimDanielson I have no ID stored in the core data, only in the array. Isn't the fetch request just to fetch from the database? – Eduardo Jul 30 '20 at 23:08
  • The films are filled in the array from a request in the API. I need to save to the database only movies that have been favorited. So I pass an object of type movie in the method to be able to persist it. – Eduardo Jul 30 '20 at 23:13
  • _I have no ID stored_ but what is `public var id: Int64` in GenresID then? And where does the array of Id's that you have come from? – Joakim Danielson Jul 31 '20 at 06:50
  • @JoakimDanielson In the specification of the app, I have to store only favorite movies on the disc. Each movie has an array of genre ids. What I'm trying to do is: save a movie together with its genre ids. It would be a one-to-many relationship. A movie can contain multiple genre ids – Eduardo Jul 31 '20 at 14:10
  • I don't follow any more, you have an entity for genres but you don't now how to use it? I don't think I have anything more to contribute with here, good luck. – Joakim Danielson Jul 31 '20 at 17:07
  • @JoakimDanielson Thank's for your time. I removed the gender entity and created a property of type Transformable [NSNumber]. I'll edit the post with the change I made. – Eduardo Jul 31 '20 at 17:40

0 Answers0