Alecrim / AlecrimCoreData

Core Data made simple.
MIT License
778 stars 100 forks source link

NSFetchedResultsControllerDelegate events not fired #10

Closed vitalyster closed 9 years ago

vitalyster commented 10 years ago
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var frc : CoreDataFetchedResultsController<Message>?    
    @IBOutlet var tableView: UITableView!    
    @IBAction func addNew(sender: UIBarButtonItem) {
        let msg = db.messages.createEntity()
        msg.text = "Yo!"
        db.save()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        frc = db.messages.sortBy("text", ascending: true).toFetchedResultsController()
        frc!.bindTo(tableView: self.tableView, rowAnimation: .Fade)
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return frc!.sections.count
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return frc!.sections[section].numberOfObjects
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
        as UITableViewCell
        let message = frc!.entityAtIndexPath(indexPath)
        cell.textLabel!.text = message.text
        return cell      
    }
}

I see in code CoreDataFetchedResultController<T> implements NSFetchedResultsControllerDelegate, but I didn't see tableView updates until restart. Wrong context?

vmartinelli commented 10 years ago

In the latest version the table view is reloaded when this method (bindTo) is called. Please check if this solves the problem.

vitalyster commented 10 years ago

when or after this method called? Anyway, still does not work

vmartinelli commented 9 years ago

Please check the last version. This bug should be fixed. :-)

vitalyster commented 9 years ago

It works now, thanks :+1: