te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>swift - iOS Crash - UICollectionViewDiffableDataSource - Fatal: supplied item identifiers are not unique - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - iOS Crash - UICollectionViewDiffableDataSource - Fatal: supplied item identifiers are not unique - Stack Overflow

programmeradmin4浏览0评论

I have a weird crash that has begun to occur that I am unable to replicate and mostly happens on the iPad 10th generation.

The crash occurs when applying a snapshot for a UICollectionViewDiffableDataSource. The content models for the datasource are shown below and they conform to Hashable. I added a custom Hashable conformance in an attempt to fix the crash I am getting but it has not helped. Previously I just used automatic conformance.

struct FormationContent: Hashable {
    let block: Block
    
    func hash(into hasher: inout Hasher) {
        hasherbine(block)
    }
    
    enum Block: Hashable {
        case formation(FormationCellViewModel)
        case add(isSelected: Bool)
        
        func hash(into hasher: inout Hasher) {
            switch self {
            case .formation(let viewModel):
                hasherbine("formation")
                hasherbine(viewModel.id)
            case .add(let isSelected):
                hasherbine("add")
                hasherbine(isSelected)
            }
        }
    }
    
    struct FormationCellViewModel: Hashable {
        let id: UUID
        let title: String
        let isSelected: Bool
    }
}

Below are the crash logs that don't appear to show any duplication. Oddly the add section does not appear but I guess the crash is occurring before the datasource attempts to display that in the next section:

Fatal: supplied item identifiers are not unique. 
Duplicate identifiers: 
{( footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 65BF74DC-FC44-4E2E-8984-DD036543B0AD, title: "5-3-2", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 2397B8DE-DD7C-4CED-B78D-5BDB30F0CB15, title: "4-5-1", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: F0CBB71C-B39B-4674-B950-F4CFF0FF8E72, title: "4-4-2", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 71609229-BCC8-457F-977F-618335BC9E0B, title: "4-3-3", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 5FA5DBE3-87A2-4CE4-9D19-2588CFE5DC28, title: "4-3-2-1", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 631FD123-F4D9-43A2-BFA1-64F6A739B9C3, title: "3-5-2", isSelected: false))) )}

The FormationContent model is for two sections that are handled with the different blocks. The formation id comes from a CoreData model and is unique.

This is how I apply my snapshot:

private func applyFormationSnapshot(_ formationsContent: [FormationLayout.Section : [FormationContent]]) {
    var snapshot = NSDiffableDataSourceSnapshot<FormationLayout.Section, FormationContent>()
    snapshot.appendSections([.main, .add])
    if let formations = formationsContent[.main] {
       snapshot.appendItems(formations, toSection: .main)
    }
    if let addFormation = formationsContent[.add] {
       snapshot.appendItems(addFormation, toSection: .add)
    }
    formationsDataSource.apply(snapshot, animatingDifferences: true)
}

Is there anything obvious that I am doing wrong? I plan to add some logging of the formation content models to help get a better understanding of the issue via Firebase.

Thanks

I have a weird crash that has begun to occur that I am unable to replicate and mostly happens on the iPad 10th generation.

The crash occurs when applying a snapshot for a UICollectionViewDiffableDataSource. The content models for the datasource are shown below and they conform to Hashable. I added a custom Hashable conformance in an attempt to fix the crash I am getting but it has not helped. Previously I just used automatic conformance.

struct FormationContent: Hashable {
    let block: Block
    
    func hash(into hasher: inout Hasher) {
        hasherbine(block)
    }
    
    enum Block: Hashable {
        case formation(FormationCellViewModel)
        case add(isSelected: Bool)
        
        func hash(into hasher: inout Hasher) {
            switch self {
            case .formation(let viewModel):
                hasherbine("formation")
                hasherbine(viewModel.id)
            case .add(let isSelected):
                hasherbine("add")
                hasherbine(isSelected)
            }
        }
    }
    
    struct FormationCellViewModel: Hashable {
        let id: UUID
        let title: String
        let isSelected: Bool
    }
}

Below are the crash logs that don't appear to show any duplication. Oddly the add section does not appear but I guess the crash is occurring before the datasource attempts to display that in the next section:

Fatal: supplied item identifiers are not unique. 
Duplicate identifiers: 
{( footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 65BF74DC-FC44-4E2E-8984-DD036543B0AD, title: "5-3-2", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 2397B8DE-DD7C-4CED-B78D-5BDB30F0CB15, title: "4-5-1", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: F0CBB71C-B39B-4674-B950-F4CFF0FF8E72, title: "4-4-2", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 71609229-BCC8-457F-977F-618335BC9E0B, title: "4-3-3", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 5FA5DBE3-87A2-4CE4-9D19-2588CFE5DC28, title: "4-3-2-1", isSelected: false))), 
footballformation.FormationContent(block: footballformation.FormationContent.Block.formation(footballformation.FormationContent.FormationCellViewModel(id: 631FD123-F4D9-43A2-BFA1-64F6A739B9C3, title: "3-5-2", isSelected: false))) )}

The FormationContent model is for two sections that are handled with the different blocks. The formation id comes from a CoreData model and is unique.

This is how I apply my snapshot:

private func applyFormationSnapshot(_ formationsContent: [FormationLayout.Section : [FormationContent]]) {
    var snapshot = NSDiffableDataSourceSnapshot<FormationLayout.Section, FormationContent>()
    snapshot.appendSections([.main, .add])
    if let formations = formationsContent[.main] {
       snapshot.appendItems(formations, toSection: .main)
    }
    if let addFormation = formationsContent[.add] {
       snapshot.appendItems(addFormation, toSection: .add)
    }
    formationsDataSource.apply(snapshot, animatingDifferences: true)
}

Is there anything obvious that I am doing wrong? I plan to add some logging of the formation content models to help get a better understanding of the issue via Firebase.

Thanks

Share Improve this question asked 2 days ago EdwardEdward 2,9742 gold badges30 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try making everything Identifiable instead of Hashable, such as:

struct FormationContent: Identifiable {
    let id = UUID()  // <--- here
    
    let block: Block

    enum Block: Identifiable {
        case formation(FormationCellViewModel)
        case add(isSelected: Bool)
        
        var id: UUID {  // <--- here
              switch self {
              case .formation(let viewModel): return viewModel.id
              case .add(let isSelected): return UUID()
              }
          }
    }
    
    struct FormationCellViewModel: Identifiable {
        let id = UUID()  // <--- here
        let title: String
        let isSelected: Bool
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论