如何在Core Data中使用带有fetchRequest的谓词 [英] How to use predicates with fetchRequest in Core Data
问题描述
我将联系人tableview控制器中的联系人标识符传递给另一个位置tableview控制器。所以我定义一个委托ContactSelectionDelegate并在Location tableview控制器中实现方法userSelectedContact并获取contactIdentifierString
I pass a contact Identifier from Contacts tableview controller to another Location tableview controller. So I define a delegate ContactSelectionDelegate and implement method userSelectedContact in Location tableview controller and get contactIdentifierString
我正在搜索数据库以找到contactIdentifierString的匹配项并找到另一个属性提供者的值名称。这涉及搜索整个数据库。通过为上下文fetchRequest分配谓词是否有更快的方法。我认为这会更快,更少的代码行。
I am searching the database to find a match for contactIdentifierString and find value for another attribute provider name. This involves searching the whole database. Is there a faster way by assigning a predicate to the context fetchRequest. I thought that would be faster and fewer lines of code.
有人可以建议如何使用联系人fetchRequest来使用谓词吗?
Can someone suggest how to use predicates with contact fetchRequest?
ContactTableViewController代码:
ContactTableViewController code:
protocol ContactSelectionDelegate{
func userSelectedContact(contactIdentifier:NSString)
}
class ContactTableViewController: UITableViewController {
var delegate:ContactSelectionDelegate? = nil
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (delegate != nil){
let contactDict:NSDictionary = allContacts.object(at: indexPath.row) as! NSDictionary
let identifier = contactDict.object(forKey: "uniqueId")
delegate!.userSelectedContact(contactIdentifier: identifier as! NSString)
self.navigationController!.popViewController(animated: true)
}
}
}
LocationTableViewController代码:
LocationTableViewController code:
class LocationTableViewController: UITableViewController, ContactSelectionDelegate, CLLocationManagerDelegate {
var contactIdentifierString:NSString = NSString()
func userSelectedContact(contactIdentifier: NSString) {
var fetchedResults: [Contact] = []
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
fetchedResults = try context.fetch(Contact.fetchRequest())
}
catch {
print ("fetch task failed")
}
if fetchedResults.count > 0 {
for contact in fetchedResults{
let aContact:Contact = contact
if (aContact.uniqueId! == contactIdentifierString as String) {
providerName.text = aContact.providerName
}
else {
continue
}
}
}
}
推荐答案
首先摆脱所有 NSString
和 NSDictionary
出现次数。这是斯威夫特!使用Swift本机结构字符串
和字典
。
First of all get rid of all NSString
and NSDictionary
occurrences. This is Swift!. Use the Swift native structs String
and Dictionary
.
其次,总是将所有好的代码放在的范围内
do - catch
阻止。
Second of all put always all good code in the do
scope of a do - catch
block.
CoreData谓词的格式为 attribute == value
,与<$ c $非常相似C> aContact.uniqueId! == contactIdentifierString :
A CoreData predicate has a simple format attribute == value
which is very similar to aContact.uniqueId! == contactIdentifierString
:
var contactIdentifierString = ""
func userSelectedContact(contactIdentifier: String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
let fetchRequest : NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", contactIdentifier)
let fetchedResults = try context.fetch(fetchRequest) as! [Contact]
if let aContact = fetchedResults.first {
providerName.text = aContact.providerName
}
}
catch {
print ("fetch task failed", error)
}
}
代码假定有一个 NSManagedObject
子类联系
包含
The code assumes that there is a NSManagedObject
subclass Contact
containing
@nonobjc public class func fetchRequest() -> NSFetchRequest<Contact> {
return NSFetchRequest<Contact>(entityName: "Contact")
}
这篇关于如何在Core Data中使用带有fetchRequest的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!