我有点困惑尝试快速从 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 ?
非常感谢!