gpt4 book ai didi

ios - 使用 Swift Realm Results<> 或 List<> 实例时,如何避免延迟或调整 coordinator.drop 的时间?

转载 作者:行者123 更新时间:2023-11-29 05:38:15 26 4
gpt4 key购买 nike

我在学习 iOS 开发时使用 Realm Swift 构建待办事项列表。当我尝试使用coordinator.drop(item.dragItem,toItemAt:destinationIndexPath)重新排序项目时,我的collectionView遇到了问题。

当我包含协调器时,放置的项目首先显示最初位于该索引处的单元格的内容,然后在延迟后被拖动的单元格的内容替换。如果我不包括协调器,则拖放将按预期工作,并且项目在放下时会正确交换(但动画是单元格缩小到中心的动画,并且有点刺耳)。

当我重新排序 collectionView 中的项目时:

func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
if let item = coordinator.items.first,
let sourceIndexPath = item.sourceIndexPath {
do {
try self.realm.write {
// standard category is of type Category
// items is of type List<Task>
standardCategory?.items.remove(at: sourceIndexPath.item)
standardCategory?.items.insert(item.dragItem.localObject as! Task, at: destinationIndexPath.item)
}
} catch {
print("error reording new items, \(error)")
}
coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
}
}

在 viewDidLoad() 内部处理更新 collectionView:

 self.notificationToken = tasks!.observe {
(changes: RealmCollectionChange) in
switch changes {
case .initial(_):
self.collectionView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: deletions.map({ IndexPath(item: $0, section: 0) }))
self.collectionView.insertItems(at: insertions.map({ IndexPath(item: $0, section: 0) }))
self.collectionView.reloadItems(at: modifications.map({ IndexPath(item: $0, section: 0) }))
}, completion: { status in
self.collectionView.reloadData()
})
case .error(_):
print("error")
}

最佳答案

我想我现在可以正常工作了。假设边缘情况不会破坏它,答案如下:

我需要在tasks.observe之外处理模型更新和collectionView更新,以便我可以更准确地控制事件的顺序。您可以使用 tryrealm.commitWrite(withoutNotifying: [notificationToken!])

来做到这一点

我认为关键是将删除和更新项目的顺序重新排序为:1.从模型中删除2.从 Collection View 中删除3.插入模型4.使用协调器来删除项目5.插入 Collection View 6. 重新加载部分(我认为是可选的,我需要它来更改 UI)

func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
if let item = coordinator.items.first {
if let localObject = item.dragItem.localObject as? Task {
if let sourceIndexPath = item.sourceIndexPath {
do {
try self.realm.write {
standardCategory?.items.remove(at: sourceIndexPath.item)
try realm.commitWrite(withoutNotifying: [notificationToken!])
}
} catch { print("error reording new items, \(error)") }
collectionView.deleteItems(at: [sourceIndexPath])
}
do {
try self.realm.write {
standardCategory?.items.insert(localObject, at: destinationIndexPath.item)
try realm.commitWrite(withoutNotifying: [notificationToken!])
}
} catch { print("error reording new items, \(error)") }

self.collectionView.performBatchUpdates({
coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
collectionView.insertItems(at: [destinationIndexPath])
}, completion: { status in
let indexSet = IndexSet(integer: destinationIndexPath.section)
collectionView.reloadSections(indexSet)
})
}
}
}

解决方案:事实证明,使用拖放进行排序时不能使用结果(不能 100% 确定原因,但很高兴知道这一点)。这是当前使用 List 的工作解决方案:

func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
if let item = coordinator.items.first {
if let localObject = item.dragItem.localObject as? Task {
if let sourceIndexPath = item.sourceIndexPath {
self.collectionView.performBatchUpdates({
do {
standardCategory!.realm!.beginWrite()

standardCategory!.items.remove(at: sourceIndexPath.item)
collectionView.deleteItems(at: [sourceIndexPath])

self.standardCategory!.items.insert(localObject as Task, at: destinationIndexPath.item)

coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)

collectionView.insertItems(at: [destinationIndexPath])
try standardCategory!.realm!.commitWrite(withoutNotifying: [notificationToken!])

} catch { print("error reording new items, \(error)") }
}, completion: nil)
}
}
}
}

关于ios - 使用 Swift Realm Results<> 或 List<> 实例时,如何避免延迟或调整 coordinator.drop 的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com