Wraith
Wraith

Reputation: 501

How refresh data in app after update some data?

I have a problem with update contact data. View no.1 main view with tableView i have list of contacts. After tap some person from list i have next tableView (view no.2 [push from view 1 to view 2]) with details and button with EDIT. Next if i press Edit i have modal view no3 with edit input when i can change NAME. After SAVE how can i refresh

  1. title in tableView navBar
  2. title in backButton
  3. main tableView in view1

i am using in my view no1

let managedObjectContext: NSManagedObjectContext? = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
    var fetchedResultsController: NSFetchedResultsController?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.configureView()
        fetchedResultsController = NSFetchedResultsController(fetchRequest: CLInstance.allContactsFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController?.delegate = self
        do { try fetchedResultsController?.performFetch() } catch _ {}
        tableView.reloadData()
    }

 func controllerWillChangeContent(controller: NSFetchedResultsController) {
        tableView.beginUpdates()
    }

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
        tableView.endUpdates()
    }



    func controller(
        controller: NSFetchedResultsController,
        didChangeObject anObject: AnyObject,
        atIndexPath indexPath: NSIndexPath?,
        forChangeType type: NSFetchedResultsChangeType,
        newIndexPath: NSIndexPath?) {

            switch type {
            case .Insert:
                tableView.insertRowsAtIndexPaths(NSArray(object: newIndexPath!) as! [NSIndexPath], withRowAnimation: .Fade)
                break
            case .Delete:
                tableView.deleteRowsAtIndexPaths(NSArray(object: indexPath!) as! [NSIndexPath], withRowAnimation: .Fade)
                break
            case .Move:
                print("move")
                //tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
                break
            case .Update:
                print("update")
                tableView.cellForRowAtIndexPath(indexPath!)
                break
            }
    }

I expected when i save data in this code NSFetchedResultsController make update but on my log file i see print out "MOVE" not "UPDATE"how it is possible to do this in some other way ?

Upvotes: 1

Views: 513

Answers (1)

Glenn
Glenn

Reputation: 2806

First of all you need to configure Outlets to any of the on screen elements you want to update. Then you just set their .text attribute to the new value.

The tableView is a bit different though, you have to reload it : myTableView.reloadData()

Just a few gotcha's:

  1. If your 'myTableView isn't reloading, make sure that the

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

function where the tableViewCell is created is using the updated source for creating the cell.

  1. Do all your updates on the main thread (if you're using threads)

Upvotes: 1

Related Questions