2015-09-13 42 views
1

我升级我的应用程序雨燕2.0,从那以后,NSFetchedResultsController不正确的行为在iOS 8.4(iOS中9它能正常工作)NSFetchedResultsController没有更新表

情景: - 一个新的实体是加入 - 行出现在tableview中 - 实体的属性发生变化,因此不应与fetchedtesultscontroller 的谓语 - 行不从tableview中消失......

我可以看到beginUpdates ,使用Delete类型调用didChangeObject,并调用endUpdates()。我的缓存是零。

这是xCode 7中iOS 8.4的一个已知错误吗? (有趣的是,有时它工作,但大部分时间它不,它对我的​​应用程序至关重要...)

在此先感谢!

p.s.我想他们在网上说,如果(!indexPath = newIndexPath)的东西,但同样的结果...

回答

3

对于具有相同问题的人,这里是解决方案:

在你:

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

确保更新的情况是前插入的情况下...所以不是这样的:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
      switch type { 
      case .Insert: 
       self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade); 
      case .Move: 
       if(!indexPath!.isEqual(newIndexPath!)) { 
        self.tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!); 
       } 
      case .Update: 
       let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! UITableViewCell; 
       self.configureCell(cell, atIndexPath: indexPath!); 
       self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade); 
      case .Delete: 
       self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade) 
      } 
     } 

你应该有这样的:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
     switch type { 
     case .Update: 
      let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! UITableViewCell; 
      self.configureCell(cell, atIndexPath: indexPath!); 
      self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade); 
     case .Insert: 
      self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade); 
     case .Move: 
      if(!indexPath!.isEqual(newIndexPath!)) { 
       self.tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!); 
      } 
     case .Delete: 
      self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade) 
     } 
    } 

这让我很头疼,但终于找到了解决办法...

+1

你需要休息,否则case语句都将落空,并都将被执行。不能相信这是被接受的答案。 – Matthijs

+1

Swift(3)不需要'break'。这些案件从未出现过。顺序并不重要。 – Matt

1

它看起来像iOS的8臭虫,你可以在这里找到更多的信息:https://forums.developer.apple.com/thread/11662#65178

如果你要修复它,试试这个代码:

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

    switch type { 
    case NSFetchedResultsChangeType(rawValue: 0)!: 
     break // iOS 8 bug - Do nothing if we get an invalid change type. 
    case .Insert: 
     // Insert here 
     break 
    case .Delete: 
     // Delete here 
     break 
    case .Move: 
     // Move here 
     break 
    case .Update: 
     // Update here 
     break 
    } 
} 
0

对于我而言,我不得不这样做,而不是:

case .Update: 

case NSFetchedResultsChangeType(rawValue: 3)!: 

对于didChangeObject:

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

    switch type { 
    case .Insert: 
     print("didChangeObject.Insert") 
     tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
    case .Delete: 
     print("didChangeObject.Delete") 
     tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
    case NSFetchedResultsChangeType(rawValue: 3)!: 
     print("didChangeObject.Update") 
     self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, withObject: anObject as! NSManagedObject) 
    case .Move: 
     tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!) 
    default: 
     return 
    } 
} 
相关问题