swift core data fetching relationship

Stefano Vet picture Stefano Vet · Sep 17, 2014 · Viewed 14.3k times · Source

I'm a little bit confused try to fetch relation data from coredata in swift

Person Entity contains the name of person and Unique Id. The relation with Books is ONE to Many

For example

Person Entity: 
idPerson = 25 - namePerson = John
idPerson = 26 - namePerson = Steve

Books Entity contains the title of books, Unique Id for the book and a relation ID with person (personBook). The relation with Person is ONE to ONE For example

Books Entity: 
idBook = 2543 - titleBook = title one - personBook = 25
idBook = 2544 - titleBook = title two - personBook = 25
idBook = 2545 - titleBook = title three - personBook = 26

here my data model screenshot: (no image because i have no reputation)

Person class

        @objc(Person)
        class Person: NSManagedObject {
              @NSManaged var idPerson: String
              @NSManaged var namePerson: String
              @NSManaged var booksRel: NSSet

        }

Books class

        @objc(Books)
        class Books: NSManagedObject {
              @NSManaged var bookTitle: String
              @NSManaged var idBook: String
              @NSManaged var personBook: String
              @NSManaged var personRel: Person

        }

Fetch code

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let request = NSFetchRequest(entityName: "Books")
    request.returnsObjectsAsFaults = false

    request.fetchLimit = 30

    ////////////////////////////
    // CODE TO JOIN Person entity WHERE personBook = idPerson
    ////////////////////////////

    var results:NSArray = context.executeFetchRequest(request, error: nil)!

    for temp in results {
         var data = temp as Books
         ///////////////////////////////////////
         //println(data.namePerson) ----> not working
         ///////////////////////////////////////
    }

is possibile to fetch for every book the related namePerson based on namePerson = personBook ?

Thank you very much!

Answer

Imanou Petit picture Imanou Petit · Sep 17, 2014

You don't need a property personBook for your Books entity.

Let's create a Person managedObject (using Swift 1.0):

let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: context) as Person
person.idPerson = 23
person.namePerson = "John"

var error: NSError?
if !context.save(&error) {
    println("Unresolved error \(error), \(error!.userInfo)")
    abort()
}

When you create a Books managedObject, you can link it to person like this:

let book = NSEntityDescription.insertNewObjectForEntityForName("Books", inManagedObjectContext: context) as Books
book.bookTitle = "My book Title"
book.idBook = 2547
book.personRel = person

var error: NSError?
if !context.save(&error) {
    println("Unresolved error \(error), \(error!.userInfo)")
    abort()
}

Now, when you want to make a fetch on Books, you can do like this:

let fetchRequest = NSFetchRequest(entityName: "Books")
var error: NSError?
var booksArray = context.executeFetchRequest(fetchRequest, error:&error)
if let error = error {
    println("Unresolved error \(error), \(error.userInfo)")
    abort()
}

for book in booksArray as [Books] {
    let person = book.personRel as Person

    println(person.namePerson)
    println(person.idPerson)
}