I would like to fetch my data from Core Data, where the books assigned to the person "Max". For this, I have the following code:
我想从Core Data中获取我的数据,其中分配给“Max”的人的书籍。为此,我有以下代码:
let request = NSFetchRequest<Book>(entityName: "Book")
request.predicate = NSPredicate(format: "person.firstName = %@", "Max")
do {
let books = try context.fetch(request)
for x in 0 ..< books.count {
print(books[x].name)
}
} catch { }
But now I would like to fetch the data where the books are assigned to a person with the Object ID "xyz".
但是现在我想获取将书籍分配给对象ID为“xyz”的人的数据。
I tried this, but it doesn't work:
我试过这个,但它不起作用:
request.predicate = NSPredicate(format: "person.self = %@", myPersonObjectID)
1 个解决方案
#1
0
You should get the object with existingObject
from the Object ID and use that in the predicate.
您应该从Object ID获取带有existingObject的对象,并在谓词中使用它。
if let myPerson = context.existingObject(with: myPersonObjectID) {
request.predicate = NSPredicate(format: "person = %@", myPerson)
}
#1
0
You should get the object with existingObject
from the Object ID and use that in the predicate.
您应该从Object ID获取带有existingObject的对象,并在谓词中使用它。
if let myPerson = context.existingObject(with: myPersonObjectID) {
request.predicate = NSPredicate(format: "person = %@", myPerson)
}