2017-04-25 14 views
1

여러 개의 코어 데이터 엔티티가 서버에 모두 동기화되어 있습니다. 1. 문제는 NSFetchedResultsController 내 UI를 제어하는 ​​것이 모든 데이터NSFetchedResultsController 삭제를 인식하지 못함

 // Next delete data in all entities (but not user) 
    for entity in CoreDataStack.sharedInstance.allEntities() { 
     if (entity != "User") { 
      if (DataAccess.deleteExisting(entityName: entity, inMoc: self.operationMOC)) { 
       print("Deleted OK: \(entity)") 
      } else { 
       print("ERROR deleting \(entity)") 
      } 
     } 
    } 

    .... 

    // Save updates 
    self.operationMOC.performAndWait { 
     do { 
      try self.operationMOC.save() 
     } catch { 
      fatalError("ERROR: Failed to save operation moc: \(error)") 
     } 
    } 
    self.mainThreadMOC.performAndWait { 
     do { 
      try self.mainThreadMOC.save() 
     } catch { 
      fatalError("ERROR: Failed to save main moc: \(error)") 
     } 
    } 

를 새로 핵심 데이터 엔티티의 모든 2. 통화 API를 삭제하지를 인식하는 것 같다 : 테스트를 위해 내가하는 기능을 구축 나는 두 부분으로 새로 고침 기능을 분할 한

error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (14) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

업데이트에 대한 "didChange"대의원 방법 화재 그래서 난 삭제가 인식되지 않습니다 것을 볼 수 있습니다 때 오류를주고, 삭제, 따라서 너무 많은 jQuery과 행이 업데이트가 시작되면 tableView.insertRows (at : ...)로 시도합니다. 코어 데이터, 그래서 그것의하고있다,

NotificationCenter.default.addObserver(self, selector: #selector(MessagesTab.dataChanged), name: NSNotification.Name.NSManagedObjectContextObjectsDidChange, object: self.mainThreadMOC) 

완벽에 대한 삭제 및 업데이트 화재 :

가 나는 또한 관찰자에 넣어 가지고 행운 - 나는 또한 나의 주 스레드에서 MOC에 직접 업데이트를 시도했다 일.

결과적으로 NSFetchResults 컨트롤러를 덤프하고 내 자신의 알림을 사용하기 전에 내 질문에 아무런 생각이 없습니까? (코드 또는 기대 중 하나임) 나는 지난 하루 동안이 문제에 곤란을 겪었으므로 충고를 기꺼이 받아 들였습니다.

내 NSFetchedResultsControllerDelegate didChange 방법 :

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 
    print(">>>> FRC Change Fired") 
    switch type { 
    case .insert: 
     tableView.insertRows(at: [newIndexPath!], with: .automatic) 
    case .delete: 
     tableView.deleteRows(at: [indexPath!], with: .automatic) 
    case .update: 
     let rowMessage = self.fetchedResultsController.object(at: indexPath!) 
     if let cell = self.tableView.cellForRow(at: indexPath!) as? InterestsListCell { 
      cell.configureWithData(rowMessage) 
     } 
    default: 
     print("Was not a update, delete or insert") 
    } 
} 
+0

fetchResultsDelegate didChangeObject 메서드를 게시해야합니다. 여기에 문제가 있습니다. – SeanLintern88

+0

@ SeanLintern88, 빠른 응답에 감사드립니다. 대리모 메서드 게시, 비록 거기에 많은 실수로 생각하지 않는다 – redPanda

+0

numberOfItemForSection tableView에, 당신은 FRC.fetchedObjects.count 또는 자기 저장 배열을 참조합니까? – SeanLintern88

답변

1

많은 덕분에이 코드는

// Delete all records for the entity 
    func deleteExisting(entityName: String, inMoc moc: NSManagedObjectContext) -> Bool { 
    do { 
     let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName) 
     let deleteRequest = NSBatchDeleteRequest(fetchRequest: request) 
     try moc.execute(deleteRequest) 
     return true 
    } catch { 
     print("ERROR: deleting existing records - \(entityName)") 
     return false 
    } 
} 

을 발사하는 FRC가 발생하지 않습니다 @JonRose 에는 그 후 나는이 문제를 연구하고 다른 사람들을 도울 수 있기를 바랍니다있다 -이 슬라이드가 표시된 WWDC 2015 session on Core Data을 참조하십시오. enter image description here .refreshAll() 및 .mergeChages를 사용해 보았습니다. 그러나 저를위한 simples은 내 FRC 및 U를 파손 한 것처럼 보였습니다. se NSManagedObjectContextDidSave 알림

관련 문제