I have implemented drag & drop between two lists in SwiftUI. Regular drag and drops work fine when dropping it in between list items, but when I do some more advanced drag like dragging it onto the cell itself, my app crashes.
This crash happens even before I drop it (so while I am still dragging it, without releasing drop)
Code inside dropDestination does not even get called.
Is this a SwiftUI bug or am I doing something wrong ?
Crash log:
2023-01-06 11:25:05.995303+0100 SimplifyStayAdmin[78761:2158775] *** Assertion failure in -[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:], _UICollectionViewDragAndDropController.m:620
2023-01-06 11:25:06.074407+0100 SimplifyStayAdmin[78761:2158775] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not get the cell at indexPath <NSIndexPath: 0x8846e25efd6dfbf1> {length = 2, path = 0 - 0} to start the reording portion of the Drag-and-drop'
*** First throw call stack:
(
0 CoreFoundation 0x000000018040e7c8 __exceptionPreprocess + 172
1 libobjc.A.dylib 0x0000000180051144 objc_exception_throw + 56
2 Foundation 0x0000000180b13b98 _userInfoForFileAndLine + 0
3 UIKitCore 0x0000000109a546ec -[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:] + 540
4 UIKitCore 0x0000000109a53064 -[_UICollectionViewDragAndDropController beginReorderingForItemAtIndexPath:cell:] + 204
5 UIKitCore 0x0000000109a1bad0 -[UICollectionView _beginInteractiveMovementForItemAtIndexPath:] + 196
6 UIKit 0x000000011803040c -[UICollectionViewAccessibility beginInteractiveMovementForItemAtIndexPath:] + 80
7 UIKitCore 0x0000000109a5c55c -[_UICollectionViewDragDestinationController _reorderingDisplayLinkDidTick] + 912
8 QuartzCore 0x0000000187dd04f8 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 808
9 QuartzCore 0x0000000187ec89a8 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 336
10 CoreFoundation 0x000000018033ee94 __CFMachPortPerform + 172
11 CoreFoundation 0x000000018037387c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
12 CoreFoundation 0x0000000180372e9c __CFRunLoopDoSource1 + 496
13 CoreFoundation 0x000000018036d43c __CFRunLoopRun + 2176
14 CoreFoundation 0x000000018036c7a4 CFRunLoopRunSpecific + 584
15 GraphicsServices 0x0000000188ff7c98 GSEventRunModal + 160
16 UIKitCore 0x000000010a1f237c -[UIApplication _run] + 868
17 UIKitCore 0x000000010a1f6374 UIApplicationMain + 124
18 SwiftUI 0x000000010e6150d4 OUTLINED_FUNCTION_51 + 496
19 SwiftUI 0x000000010e614f7c OUTLINED_FUNCTION_51 + 152
20 SwiftUI 0x000000010dd7ab60 OUTLINED_FUNCTION_10 + 88
21 SimplifyStayAdmin 0x000000010044d0c4 $s17SimplifyStayAdmin0abC3AppV5$mainyyFZ + 40
22 SimplifyStayAdmin 0x000000010044d378 main + 12
23 dyld 0x00000001080c1fa0 start_sim + 20
24 ??? 0x00000001081c9e50 0x0 + 4431060560
25 ??? 0x0869800000000000 0x0 + 606156362346397696
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not get the cell at indexPath <NSIndexPath: 0x8846e25efd6dfbf1> {length = 2, path = 0 - 0} to start the reording portion of the Drag-and-drop'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPad (10th generation) (D79A0AD8-4970-4F84-A5F9-078DAFB697A3) - Runtime: iOS 16.2 (20C52) - DeviceType: iPad (10th generation)
SwiftUI body:
var body: some View {
HStack {
VStack {
Text("title-pending")
List {
ForEach(viewModel.pendingOrders) { order in
RestaurantOrderCardView(order: order) {
.background(cardBackground)
.draggable(order)
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.padding(.top)
}
.listStyle(.plain)
.background(listBackground)
}
VStack {
Text("title-in-preparation")
List {
ForEach(viewModel.inPreparationOrders) { item in
RestaurantOrderCardView(order: item)
.background(cardBackground)
}
.dropDestination(for: RestaurantOrder.self) { items, offset in
guard let firstItem = items.first else { return }
withAnimation {
viewModel.dropItem(firstItem, offset: offset)
}
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.padding(.top)
}
.listStyle(.plain)
.background {
if !viewModel.inPreparationOrders.isEmpty {
listBackground
}
}
.overlay {
if viewModel.inPreparationOrders.isEmpty {
RoundedRectangle(cornerRadius: 8)
.foregroundColor(!inDropArea ? .grayBackgroundColor : .grayBackgroundColor.opacity(0.25))
.ignoresSafeArea()
.dropDestination(for: RestaurantOrder.self) { items, location in
guard let firstItem = items.first else { return false }
withAnimation {
viewModel.dropItem(firstItem, offset: 0)
}
return true
} isTargeted: { inDropArea in
withAnimation {
self.inDropArea = inDropArea
}
}
}
}
}
}
}
Note: This crash occurs both on my device iPhone 11 with iOS 16.2, and on iPad (10th gen) iOS 16.2 simulator.