ios 如何在 Core Data 中使用带有 fetchRequest 的谓词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41641705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use predicates with fetchRequest in Core Data
提问by vrao
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
我将联系人标识符从联系人表视图控制器传递到另一个位置表视图控制器。所以我定义了一个委托 ContactSelectionDelegate 并在 Location tableview 控制器中实现方法 userSelectedContact 并获得 contactIdentifierString
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.
我正在搜索数据库以查找 contactIdentifierString 的匹配项并查找另一个属性提供程序名称的值。这涉及搜索整个数据库。是否有通过将谓词分配给上下文 fetchRequest 的更快方法。我认为这会更快,代码行更少。
Can someone suggest how to use predicates with contact fetchRequest?
有人可以建议如何将谓词与 contact fetchRequest 一起使用吗?
ContactTableViewController code:
ContactTableViewController 代码:
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 code:
LocationTableViewController 代码:
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
}
}
}
}
回答by vadian
First of all get rid of all NSString
and NSDictionary
occurrences. This is Swift!. Use the Swift native structs String
and Dictionary
.
首先摆脱所有NSString
和NSDictionary
事件。这是斯威夫特!。使用 Swift 原生结构String
和Dictionary
.
Second of all put always all goodcode in the do
scope of a do - catch
block.
其次,始终将所有好的代码放在块的do
范围内do - catch
。
A CoreData predicate has a simple format attribute == value
which is very similar to aContact.uniqueId! == contactIdentifierString
:
CoreData 谓词有一个简单的格式attribute == value
,非常类似于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)
}
}
The code assumes that there is a NSManagedObject
subclass Contact
containing
该代码假定有一个NSManagedObject
子类Contact
含
@nonobjc public class func fetchRequest() -> NSFetchRequest<Contact> {
return NSFetchRequest<Contact>(entityName: "Contact")
}