如何从关系实体中进行简单的提取?迅速

Posted

技术标签:

【中文标题】如何从关系实体中进行简单的提取?迅速【英文标题】:How to make a simple fetch from relationship entities? Swift 【发布时间】:2015-08-21 18:02:13 【问题描述】:

我在 Xcode 中制作了两个关系实体,我使用的是 Swift。我在 Core Data 中建立了一个简单的关系。

公司代码

import Foundation
import CoreData

    @objc(Company)
    class Company: NSManagedObject 

        @NSManaged var nameCompany: String
        @NSManaged var additional: NSSet

    

附加代码

import Foundation
import CoreData

@objc(Additional)
class Additional: NSManagedObject 

    @NSManaged var acitvityCompany: String
    @NSManaged var founded: String
    @NSManaged var company: Company


我编写了以下代码,将数据添加到实体中。有用。

       @IBAction func saveData(sender: UIBarButtonItem) 
        var companyEntity = NSEntityDescription.insertNewObjectForEntityForName("Company", inManagedObjectContext: managedObjectContext) as! NSManagedObject
        var additionalEntity = NSEntityDescription.insertNewObjectForEntityForName("Additional", inManagedObjectContext: managedObjectContext) as! NSManagedObject

        companyEntity.setValue(nameCompanyTextField.text, forKey: "nameCompany")

        additionalEntity.setValue(activityTextField.text, forKey: "acitvityCompany")
        additionalEntity.setValue(foundedTextField.text, forKey: "founded")

        companyEntity.setValue(NSSet(object: additionalEntity), forKey: "additional")

        managedObjectContext.save(nil)

        var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        var navi = storyboard.instantiateViewControllerWithIdentifier("navi") as! UINavigationController
        self.presentViewController(navi, animated: true, completion: nil)

//        println("company \(companyEntity)")
//        println("additional \(additionalEntity)")
    

当我移至显示来自实体的数据的 UITableViewController 时。如果我从附加实体获取数据,我会收到错误消息。 CoreData:致命错误:部分信息的持久缓存与当前配置不匹配。你非法改变了 NSFetchedResultsController 的获取请求、谓词或排序描述符,而没有禁用缓存或使用 +deleteCacheWithName:

如果我只从公司实体中获取数据,效果很好。

我的UITableViewController

    override func viewDidLoad() 
    super.viewDidLoad()
    self.definesPresentationContext = true 
    fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "nameCompany", cacheName: "nameCompany")
    fetchedResultsController.delegate = self
    fetchedResultsController.performFetch(nil)

    fetchedResultsController2 = NSFetchedResultsController(fetchRequest: fetchRequest2(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "company", cacheName: "company")
    fetchedResultsController2.delegate = self
    fetchedResultsController2.performFetch(nil)


override func viewDidAppear(animated: Bool) 
    super.viewDidAppear(animated)
    self.tableView.reloadData()


// MARK: - var and let
var managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

// MARK: - fetchedResultsController
var fetchedResultsController: NSFetchedResultsController!
var fetchedResultsController2: NSFetchedResultsController!

func fetchRequest() -> NSFetchRequest 
    let fetchRequest = NSFetchRequest(entityName: "Company")
    let sortDescriptor = NSSortDescriptor(key: "nameCompany", ascending: true)
    fetchRequest.fetchBatchSize = 50
    fetchRequest.predicate = nil
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest


func fetchRequest2() -> NSFetchRequest 
    let fetchRequest = NSFetchRequest(entityName: "Additional")
    let sortDescriptor = NSSortDescriptor(key: "company", ascending: true)
    fetchRequest.fetchBatchSize = 50
    fetchRequest.predicate = nil
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest


 override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    return fetchedResultsController.sections?.count ?? 0


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return fetchedResultsController.sections?[section].numberOfObjects ?? 0


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    if var dataForCell = fetchedResultsController.objectAtIndexPath(indexPath) as? Company 
        var additionalCell = fetchedResultsController2.objectAtIndexPath(indexPath) as! Additional
        cell.textLabel?.text = dataForCell.nameCompany
        cell.detailTextLabel?.text = "\(additionalCell.acitvityCompany) - \(additionalCell.founded)"
    

    return cell

【问题讨论】:

【参考方案1】:

我知道怎么做。

var currentDate: NSDate!

    // MARK: - NSFetchedResultsController
    var fetchedResultsController: NSFetchedResultsController!

    func fetchRequest() -> NSFetchRequest 
        let fetchRequest = NSFetchRequest(entityName: "Car")
        let sortDescriptor = NSSortDescriptor(key: "personCar", ascending: true)
        fetchRequest.predicate = NSPredicate(format: "personRelationship.datePerson contains[c] %@", currentDate)
        fetchRequest.fetchBatchSize = 50
        fetchRequest.fetchLimit = 50
        fetchRequest.sortDescriptors = [sortDescriptor]
        println(fetchRequest)
        return fetchRequest
    

【讨论】:

以上是关于如何从关系实体中进行简单的提取?迅速的主要内容,如果未能解决你的问题,请参考以下文章

对许多实体和实体关系进行过滤的核心数据提取

iPhone核心数据关系

spacy 提取实体关系解析深度树

知识图谱关系抽取与总结展望

论文解读丨图神经网络应用于半结构化文档的命名实体识别和关系提取

2. 观点提取和聚类代码详解