-1

i am trying to make relation entity between 2 entities , unfortunately its not working well since i am still new with swift.

for example i have playlist table and songs table i want to make relation between them ?

enter image description here

and this is the Fetch Request :

let fetchRequest = NSFetchRequest(entityName: "Playlist")

//        // Add Sort Descriptor
//        let sortDescriptor = NSSortDescriptor(key: "playlist_name", ascending: true)               
//        fetchRequest.sortDescriptors = [sortDescriptor]


        let context = Appdelegate().managedObjectContext

        // Execute Fetch Request
        do {
            let result = try context.executeFetchRequest(fetchRequest)
            print(result)
        } catch {
            let fetchError = error as NSError
            print(fetchError)
        }

can i create third table to make relation between songs and playlists like this ?

enter image description here

Kodr.F
  • 13,932
  • 13
  • 46
  • 91
  • You have to define relationship. between two tables , lets say playList entity should have relationship (one to many) with songs – dip Oct 06 '17 at 10:49
  • @dip i am still new with coredata can u please explain more ? – Kodr.F Oct 06 '17 at 11:04

1 Answers1

0

You need to create the relationship in the Core Data model editor. It's easier to use the model editor if you switch it to table mode using this control at the bottom of the window:

enter image description here

When you do that, the main part of the window will look something like this:

enter image description here

Click the "+" button in the relationships section to add a relationship. Fill in the destination entity and be sure to create an inverse relationship.

You should not create a third table to create the relationship. That's something you do in SQL but not in Core Data in most cases.

You may also want to review the relationships section of Apple's Core Data Programming Guide

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170