2017-11-27 1 views
0

두 섹션이있는 UITableView 있습니다. 각 섹션에는 특정 영역 객체에 대한 쿼리 결과가 표시됩니다.그룹화 된 UITableView 및 영역 알림

var contentRequests: Results<UESocketPostRequest>? { 
    return try? CustomRealm.realm().objects(UESocketPostRequest.self).filter("completed = false AND content != nil").sorted(byKeyPath: "content.createdAt", ascending: false) 
} 

var mediaUploads: Results<UEMediaUploadRequest>? { 
    return try? CustomRealm.realm().objects(UEMediaUploadRequest.self).filter("finished = false").sorted(byKeyPath: "media.content.createdAt", ascending: false) 
} 

UITableViewDelegateUITableViewDataSource 적합성 :

func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch section { 
    case 0: 
     return self.contentRequests?.count ?? 0 
    case 1: 
     return self.mediaUploads?.count ?? 0 
    default: 
     return 0 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "UPLOADCELL") as? UEUploadCell else { 
     return UITableViewCell() 
    } 

    switch indexPath.section { 
    case 0: 
     if let request = self.contentRequests?[indexPath.row] { 
      cell.softBindSocketRequest(request) 
     } 
    case 1: 
     if let request = self.mediaUploads?[indexPath.row] { 
      cell.softBindMediaUploadRequest(request) 
     } 
    default: 
     break 
    } 

    return cell 
} 

그때 내 영역 결과 각각 NotificationToken 년대를 설정하고이 같은 테이블 뷰 업데이트 : 자주

func startContentToken() { 
    self.contentToken = self.contentRequests?.observe({ [weak self] (changes) in 
     switch changes { 
     case .error: 
      break 
     case .initial: 
      let indexSet = IndexSet(integer: 0) 
      self?.tableView.reloadSections(indexSet, with: .automatic) 
     case .update(let content, deletions: let deletions, insertions: let insertions, modifications: let modifications): 
      self?.tableView.beginUpdates() 
      self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }), 
             with: .automatic) 
      self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}), 
             with: .automatic) 
      self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }), 
             with: .automatic) 
      self?.tableView.endUpdates() 
     } 
    }) 
} 

func startMediaToken() { 
    self.mediaToken = self.mediaUploads?.observe({ [weak self] (changes) in 
     switch changes { 
     case .error: 
      break 
     case .initial: 
      let indexSet = IndexSet(integer: 1) 
      self?.tableView.reloadSections(indexSet, with: .automatic) 
     case .update(let media, deletions: let deletions, insertions: let insertions, modifications: let modifications): 
      self?.tableView.beginUpdates() 
      self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 1) }), 
             with: .automatic) 
      self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 1)}), 
             with: .automatic) 
      self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 1) }), 
             with: .automatic) 
      self?.tableView.endUpdates() 
     } 
    }) 
} 

I을 이 로그 메시지 또는 유사하게 캡슐화 된 UITableView 불일치 예외로 인해 충돌이 발생합니다.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

내가 뭔가 잘못하고 있는가 또는 영역이 방법으로 그룹화 UITableView의 함께 사용하도록 설계되지 않는 이유는 무엇입니까?

답변

2

이렇게되는 이유는 2 개의 알림이 동시에 트리거되지 않기 때문입니다. ContentToken에서 업데이트가 끝날 때까지 결과 numberOfRows를 확인한다고 생각합니다 (notification-update가 1 행을 삭제하면 결과 tableView에 업데이트보다 1 행이 적은지 확인). 문제는이 시간까지 mediaToken이 트리거되어 numberOfRowsInSection (1)이 반환하는 내용을 변경하여 오류가 발생한다는 것입니다. 나는 꽤 오랫동안 똑같은 문제를 겪어 왔지만 불행히도 해결책을 찾지 못했습니다.

+0

통찰력을위한 감사합니다 Jeroen. 해결 방법을 찾을 수 있는지 알아보기 위해 조금 놀아 보겠습니다. –

+0

@JonVogel 솔루션을 제안 했습니까? – sirvon

+0

아니요.하지만 여기 제가 Realm 코코아 저장소에 제기 한 문제가 있습니다 : https://github.com/realm/realm-cocoa/issues/5553 –

관련 문제