swift core data fetching relationship
Asked Answered
O

2

12

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!

Oahu answered 17/9, 2014 at 16:1 Comment(0)
M
13

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)
}
Mccullers answered 17/9, 2014 at 17:12 Comment(2)
thanks @POB , but the coredata are saved from a json and then fetched by the application. For example today you receveid only 1 new person and 50 books.Oahu
I try to explain better. The app have the two entity update in different time from a json. My main question is .... When i fetch the Books entity for example in a table view or in a collection view can i join the Books entit with person entity for idPerson=personBook to retrive the bookTitle and the associated namePerson. Thanks!!!!Oahu
D
2

If I have understood your question / data structure correctly, you will want to do something like:

let fetchRequest = NSFetchRequest(entityName: "Books")
let bookPersonPredicate = NSPredicate(format: "personRel.idPerson == %@", person.idPerson)
fetchRequest.predicate = bookPersonPredicate
Damselfly answered 4/8, 2015 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.