swift 正在通过NSDiffableDataSourceSnapshot获取服务

n3ipq98p  于 2023-09-30  发布在  Swift
关注(0)|答案(2)|浏览(96)

快照有问题,情况是:我有一个3节使用合成布局收集,也有3个不同的提取服务,所以目标是填补每一节3个不同的相册,但当我尝试做图片下载到第一或第二节或第二和第三,我不明白如何修复它
首先,我试着不使用DispatchGroup -根本不起作用,只下载到第一部分,试图为每个部分制作3个不同的快照-也不起作用

var snapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, Int>()
        snapshot.appendSections([.one, .two, .three])

        let group = DispatchGroup()

        group.enter()
        firstService.fetchFirst { [weak self] result in
            defer {
                group.leave()
            }
            switch result {
            case .success(let fetchedFirstPhotos):
                self?.firstPhotos = fetchedFirstPhotos
                snapshot.appendItems(Array(0..<fetchedFirstPhotos.count), toSection: .one)
                print(fetchedFirstPhotos)
            case .failure(let error):
                print(error)
            }
        }

        group.enter()
        secondService.fetchSecond { [weak self] result in
            defer {
                group.leave()
            }
            switch result {
            case .success(let fetchedSecondPhotos):
                self?.secondPhotos = fetchedSecondPhotos
                snapshot.appendItems(Array(0..<fetchedSecondPhotos.count), toSection: .two)
                print(fetchedSecondPhotos)
            case .failure(let error):
                print(error)
            }
        }

        group.enter()
        thirdService.fetchThird { [weak self] result in
            defer {
                group.leave()
            }
            switch result {
            case .success(let fetchedThirdPhotos):
                self?.thirdPhotos = fetchedThirdPhotos
                snapshot.appendItems(Array(0..<fetchedThirdPhotos.count), toSection: .three)
                print(fetchedThirdPhotos)
            case .failure(let error):
                print(error)
            }
        }

        group.notify(queue: .main) {
            self.dataSource.apply(snapshot, animatingDifferences: false)
        }
gt0wga4j

gt0wga4j1#

发生这种情况是因为组立即在主队列上通知。
你需要了解这个API是如何工作的。它匹配进入计数和离开计数,当计数匹配时,它继续通知。
在您的情况下,服务响应更快,块被调用,因此它只有1个进入计数,它与1个离开计数匹配,因此它调用通知。
我的建议是,在打电话给服务部门之前,你先打3次回车。

let group = DispatchGroup()

group.enter()
group.enter()
group.enter()

firstService.fetchFirst { .... }
secondService.fetchSecond { ... }
thirdService.fetchThird { ... }

group.notify(queue: .main) { [weak self] in
    self?.dataSource.apply(snapshot, animatingDifferences: false)
}
fdbelqdn

fdbelqdn2#

无论数据来源如何,每次数据更新时都应重新创建并应用快照。
有一个单独的函数来执行以下操作:

func updateSnapshot() {
    var snapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, Int>()
    snapshot.appendSections([.one, .two, .three])
    if firstPhotos.isEmpty == false {
        snapshot.appendItems(Array(0..<firstPhotos.count), toSection: .one)
    }
    if secondPhotos.isEmpty == false {
        snapshot.appendItems(Array(0..<secondPhotos.count), toSection: .two)
    }
    if thirdPhotos.isEmpty == false {
        snapshot.appendItems(Array(0..<thirdPhotos.count), toSection: .three)
    }
    dataSource.apply(snapshot, animatingDifferences: false)
}

然后,可以按照您喜欢的任何顺序/时间完成获取数据,并且UI将逐步更新:

firstService.fetchFirst { [weak self] result in
    switch result {
    case .success(let fetchedFirstPhotos):
        self?.firstPhotos = fetchedFirstPhotos
        self?.updateSnapshot()
    case .failure(let error):
        print(error)
    }
}

secondService.fetchSecond { [weak self] result in
    switch result {
    case .success(let fetchedSecondPhotos):
        self?.secondPhotos = fetchedSecondPhotos
        self?.updateSnapshot()
    case .failure(let error):
        print(error)
    }
}

thirdService.fetchThird { [weak self] result in
    switch result {
    case .success(let fetchedThirdPhotos):
        self?.thirdPhotos = fetchedThirdPhotos
        self?.updateSnapshot()
    case .failure(let error):
        print(error)
    }
}

相关问题