swift核心数据获取关系

时间:2014-09-17 16:01:21

标签: core-data swift ios8 relationship xcode6gm

我有点困惑尝试从swift中的coredata获取关系数据

人员实体 包含人名和唯一ID。与书籍的关系是一对多

例如

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

图书实体包含图书标题,图书的唯一标识以及与人的关系标识(personBook)。与人的关系是一对一的 例如

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

这里我的数据模型截图:(没有图片,因为我没有声誉)

人类

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

        }

图书班

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

        }

获取代码

    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
         ///////////////////////////////////////
    }

是否有可能根据namePerson = personBook为每本书获取相关的namePerson?

非常感谢!

2 个答案:

答案 0 :(得分:10)

您的personBook实体不需要属性Books

让我们创建一个Person managedObject:

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()
}

创建Books managedObject时,可以将其链接到person,如下所示:

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()
}

现在,当你想在Books上进行抓取时,你可以这样做:

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)
}

答案 1 :(得分:1)

如果我已正确理解您的问题/数据结构,您将需要执行以下操作:

let fetchRequest = NSFetchRequest(entityName: "Books")
let bookPersonPredicate = NSPredicate(format: "personRel.idPerson == %@", person.idPerson)
fetchRequest.predicate = bookPersonPredicate