如何在不同视图控制器中写入同一索引处的 CoreData 实体(也是 swift 文件)

How to write into the CoreData entity at the same index in different view controller (also swift files)

我在 table 视图中使用 'NSFetchedResultController' 和 'managedObjectContext' 创建了核心数据。但是在后面的view controller中,在收集加速度计数据并进行计算后,我会得到一些我也想和我之前创建的核心数据存储在同一行索引中的结果。

我怎样才能做到这一点?如果我再次创建 managedObjectContext,它将在这个 table.

中创建另一个 'row' 核心数据

tableViewController中的代码:

'
让 managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext

var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchedResultController = getFetchedResultController()
    fetchedResultController.delegate = self
    fetchedResultController.performFetch(nil)

}

func getFetchedResultController() -> NSFetchedResultsController {
    fetchedResultController = NSFetchedResultsController(fetchRequest: trialFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
    return fetchedResultController
}

func trialFetchRequest() -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "Trials")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
    return fetchRequest
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

   override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    let numberOfSections = fetchedResultController.sections?.count
    return numberOfSections!
}

   override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects
return numberOfRowsInSection!  
}

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

    let trial = fetchedResultController.objectAtIndexPath(indexPath) as Trials
    cell.textLabel?.text = trial.trialName
    cell.detailTextLabel?.text = trial.date?.description

    return cell
}

 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        let managedObject:NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as NSManagedObject

         managedObjectContext?.deleteObject(managedObject)
         managedObjectContext?.save(nil)
    }

func controllerDidChangeContent(controller: NSFetchedResultsController!) {
        tableView.reloadData()
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "showTrial" {
        let indexPath = tableView.indexPathForSelectedRow()
        let trial:Trials = fetchedResultController.objectAtIndexPath(indexPath!) as Trials

        let trialController:TrialDetailViewController = segue.destinationViewController as TrialDetailViewController

            trialController.trial = trial
      }

    }

'

createTrial控制器中的代码:

' let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "createTrial"  {
        if theTrialName.text != "" {
            createTrial()
        } else {

            let alertView = UIAlertController(title: "", message: "Trial name couldn't be empty", preferredStyle: .Alert)
            alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
            presentViewController(alertView, animated: true, completion: nil)

        }
    }
}

func createTrial() {
    let entityDescripition = NSEntityDescription.entityForName("Trials", inManagedObjectContext: managedObjectContext!)
    let trial = Trials(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
    trial.trialName = theTrialName.text
    trial.location = theLocation.text
    trial.notes = theNotes.text

    if theArm.on {
        trial.arm = 1
    } else {
        trial.arm = 0
    }
    managedObjectContext?.save(nil)
}

'

ps。我想从中获取数据的视图控制器不是下面的 segue,而是之后的 3 个视图。我已经创建了一个字符串来存储我在该视图中需要的数据。

我通过在 tableViewController 中创建对象并使用 segue 两次将其传输到 secondViewControllerthirdViewController 来解决问题。所以它将一直处于相同的索引。我可以在以下两个视图控制器中更改核心数据的内容。