I'm trying to convert a table view to a collection view. I have it display the cells fine but any changes made to them aren't being displayed until i leave the view/come back or scroll the cell off screen. The data comes from core data
The core data objects are updated using
Code:
override func viewWillAppear(animated: Bool) {
var stepCount = 0.0
if let results = fetchedResultsController.fetchedObjects {
if results.count > 0 {
for i in 0...results.count-1 {
if let userP : UserPet = results[i] as? UserPet {
let yesterday = userP.expLastUpdated
HealthKitManager().recentSteps(yesterday!) { steps, error in
if let exp = userP.exp {
let newExp = exp.doubleValue + steps
userP.exp = newExp
stepCount = steps
}
}
userP.expLastUpdated = NSDate()
self.appData.saveContext()
}
}
}
}
}
I have the following delegate methods for nsfetchedresultscontroller
Code:
private var blockOperations : [NSBlockOperation] = []
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
NSLog("\(type)")
let tooManyEggs = UIAlertController(title: "Alert", message: "Type: \(type)", preferredStyle: .Alert)
let noActionEggs = UIAlertAction(title: "OK", style: .Cancel) { (alert: UIAlertAction!) -> Void in
}
tooManyEggs.addAction(noActionEggs)
presentViewController(tooManyEggs, animated: true, completion:nil)
switch type {
case .Insert:
guard let newIndexPath = newIndexPath else { return }
let op = NSBlockOperation { [weak self] in self?.collectionView?.insertItemsAtIndexPaths([newIndexPath]) }
blockOperations.append(op)
NSLog("Insert")
case .Update:
guard let newIndexPath = newIndexPath else { return }
let op = NSBlockOperation { [weak self] in self?.collectionView?.reloadItemsAtIndexPaths([newIndexPath]) }
blockOperations.append(op)
NSLog("Update")
let tooManyEggs = UIAlertController(title: "Alert", message: "Update", preferredStyle: .Alert)
let noActionEggs = UIAlertAction(title: "OK", style: .Cancel) { (alert: UIAlertAction!) -> Void in
}
tooManyEggs.addAction(noActionEggs)
presentViewController(tooManyEggs, animated: true, completion:nil)
case .Move:
guard let indexPath = indexPath else { return }
guard let newIndexPath = newIndexPath else { return }
let op = NSBlockOperation { [weak self] in self?.collectionView?.moveItemAtIndexPath(indexPath, toIndexPath: newIndexPath)}
blockOperations.append(op)
NSLog("Move")
case .Delete:
guard let newIndexPath = newIndexPath else { return }
let op = NSBlockOperation { [weak self] in self?.collectionView?.deleteItemsAtIndexPaths([newIndexPath]) }
blockOperations.append(op)
NSLog("Delete")
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
collectionView?.performBatchUpdates({
self.blockOperations.forEach { $0.start() }
}, completion: { finished in
self.blockOperations.removeAll(keepCapacity: false)
})
}
The delegate for the nsfetchedresults controller is set to self
_fetchedResultsController?.delegate = self
i know the
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
is being called but nothing is matching. I've printed the type variable which is giving me 'NSFetchedResultsChangeType'