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

时间:2015-03-05 22:02:48

标签: core-data nsfetchedresultscontroller nsmanagedobject

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

我怎样才能做到这一点?如果我再次创建managedObjectContext,它将在此表中创建另一个“行”核心数据。

tableViewController中的代码:


让managedObjectContext =(UIApplication.sharedApplication()。委托作为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 Controller中的代码:

'让managedObjectContext =(UIApplication.sharedApplication()。委托作为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个视图。我创建了一个字符串来存储该视图中所需的数据。

1 个答案:

答案 0 :(得分:0)

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