12

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

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

例如

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

Books Entity包含书籍的标题、书籍的唯一 ID 以及与人员 (personBook) 的关系 ID。与 Person 的关系是 ONE to ONE 例如

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 ?

非常感谢!

4

2 回答 2

13

personBook您不需要Books实体的属性。

让我们创建一个PersonmanagedObject(使用 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()
}

当你创建一个BooksmanagedObject 时,你可以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()
}

现在,当你想在 上进行 fetch 时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)
}
于 2014-09-17T17:12:53.770 回答
1

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

let fetchRequest = NSFetchRequest(entityName: "Books")
let bookPersonPredicate = NSPredicate(format: "personRel.idPerson == %@", person.idPerson)
fetchRequest.predicate = bookPersonPredicate
于 2015-08-04T13:45:21.603 回答