I’m having the same problem in Swift Playgrounds for iPad (and I’m not even using CoreData). I’ve put in a bunch of debugging to make sure no indexes are out of bounds, but I’m guessing something is being cached related to the ForEach loop where it’s either referencing the deleted item or using a cached version of the array and not noticing the array has been modified.
Note that the debug print statement does not seem to execute before using the index and the list is being edited elsewhere so I think there could be some sort of observer/synchronization issue going on…
struct HeartsScoreBox: View {
@ObservedObject var round: HeartsRound
@State var playerIndex: Int
var body: some View {
VStack {
let _ = print("HSB# scores: \(round.playerScores.count), index: \(playerIndex)")
let score = round.playerScores[playerIndex]
Text(String(describing:score))
}
}
}
And the crash report:
Application Specific Information:
dyld: dyld4 config: DYLD_INSERT_LIBRARIES=/System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection
libswiftCore.dylib: Swift/ContiguousArrayBuffer.swift:575: Fatal error: Index out of range
Thread:
0 libswiftCore.dylib closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:)
1 libswiftCore.dylib closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:)
2 libswiftCore.dylib closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:)
3 libswiftCore.dylib closure #1 in _assertionFailure(_:_:file:line:flags:)
4 libswiftCore.dylib _assertionFailure(_:_:file:line:flags:)
5 libswiftCore.dylib _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:)
6 libswiftCore.dylib Array.subscript.getter
7 Previews_HeartsGameView_Thunk_1.dylib closure #1 in HeartsScoreBox.__preview__body.getter /private/var/mobile/Library/Mobile Documents/iCloud~com~apple~Playgrounds/Documents/Score!.swiftpm/Games/Hearts/HeartsGameView.swift:11
8 SwiftUI VStack.init(alignment:spacing:content:)
9 Previews_HeartsGameView_Thunk_1.dylib HeartsScoreBox.__preview__body.getter /private/var/mobile/Library/Mobile Documents/iCloud~com~apple~Playgrounds/Documents/Score!.swiftpm/Games/Hearts/HeartsGameView.swift:9
10 Score! protocol witness for View.body.getter in conformance HeartsScoreBox /<compiler-generated>:0
11 SwiftUI partial apply for closure #1 in ViewBodyAccessor.updateBody(of:changed:)
12 SwiftUI closure #1 in BodyAccessor.setBody(_:)
13 SwiftUI ViewBodyAccessor.updateBody(of:changed:)
14 SwiftUI DynamicBody.updateValue()
15 SwiftUI partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:)
16 AttributeGraph AG::Graph::UpdateStack::update()
17 AttributeGraph AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int)
18 AttributeGraph AG::Graph::value_ref(AG::AttributeID, AGSwiftMetadata const*, unsigned char&)
19 AttributeGraph AGGraphGetValue
20 SwiftUI GraphHost.updatePreferences()
21 SwiftUI ViewGraph.updateOutputs()
22 SwiftUI closure #1 in ViewRendererHost.render(interval:updateDisplayList:)
23 SwiftUI ViewRendererHost.render(interval:updateDisplayList:)
24 SwiftUI _UIHostingView.layoutSubviews()
25 SwiftUI @objc _UIHostingView.layoutSubviews()
26 UIKitCore -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
27 QuartzCore CA::Layer::layout_if_needed(CA::Transaction*)
28 UIKitCore -[UIView(Hierarchy) layoutBelowIfNeeded]
29 SwiftUI closure #3 in performUpdates #1 <A, B>() in UITableViewListCoordinator.updateUITableView(_:to:transaction:)
30 SwiftUI thunk for @callee_guaranteed () -> ()
31 SwiftUI thunk for @escaping @callee_guaranteed () -> ()
32 UIKitCore +[UIView(Animation) performWithoutAnimation:]
33 SwiftUI performUpdates #1 <A, B>() in UITableViewListCoordinator.updateUITableView(_:to:transaction:)
34 SwiftUI thunk for @callee_guaranteed () -> ()
35 SwiftUI thunk for @escaping @callee_guaranteed () -> ()
36 UIKitCore +[UIView(Animation) performWithoutAnimation:]
37 SwiftUI closure #2 in UITableViewListCoordinator.updateUITableView(_:to:transaction:)
38 SwiftUI thunk for @escaping @callee_guaranteed () -> ()
39 SwiftUI static Update.end()
40 SwiftUI static NSRunLoop.flushObservers()
41 SwiftUI closure #1 in closure #1 in static NSRunLoop.addObserver(_:)
42 SwiftUI specialized thunk for @callee_guaranteed () -> (@error @owned Error)
43 libswiftObjectiveC.dylib autoreleasepool<A>(invoking:)
44 SwiftUI closure #1 in static NSRunLoop.addObserver(_:)
45 SwiftUI @objc closure #1 in static NSRunLoop.addObserver(_:)
46 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
…