Swift/Collection.swift:722: Fatal error: Index out of bounds

Hello,

I have a simple SwiftUI app for macOS. It uses CoreData as data storage. I have a @FetchRequest and use the resulting array to populate a Table. I have added .searchable to have a search field. I usually can search without problems. Yet, some valid search strings result in a crash repeatedly giving:

Swift/Collection.swift:722: Fatal error: Index out of bounds

The crash happens in the main thread and the call stack is buried within refresh code for the table. It happens after [NSTableView _delegate_isGroupRow:] is called .

This is a big show stopper and I have no possibility to interfere since it happens in code out of my control. ´

My table code is without sections and simple like this:

var table: some View {
        Table(selection: $selection, sortOrder: $items.sortDescriptors) {
            TableColumn("Authors", value: \.authorsForDisplay!).width(min:30, ideal:50, max:200)
            TableColumn("Title", value: \.title!).width(min:100, ideal:300, max:500)
            TableColumn("Year", value: \.year) { article in
                Text(String(article.year))
            }.width(min:50, ideal:50, max:50)
            TableColumn("Journal") { article in
                Text(article.journal?.name ?? "---")
            }.width(min:30, ideal:50, max:200)
            TableColumn("Publisher") { article in
                Text(article.publishedBy ?? "---")
            }.width(min:30, ideal:50, max:200)
        }
    rows: {
        ForEach(items) { article in
            TableRow(article)
        }
    }}

Any ideas?

Thx, Volker

Please give more information.

  • Where do you use search string ?
  • how do you populate array
  • Give an example of failing search string and successful one
  • where does the crash occur (show refresh code for the table)
  • or at least the value of index out of bounds and the number of items in Table

.

it happens in code out of my control

What do you mean ? Out of your own code ?

Thx!

I get the string entered in a search field (created using .searchable() )

Upon submission of the search (by pressing enter) I call a function:

private func searchPredicate(query: String) {
        if self.searchControl.originalPredicate == nil {
            self.searchControl.originalPredicate = items.nsPredicate
            if items.nsPredicate == nil {
                self.searchControl.originalPredicate = NSPredicate(format: "TRUEPREDICATE")
            }
        }
        var predicate: NSPredicate?
        if self.searchScope == .title {
            predicate = NSPredicate(format: "%K CONTAINS[cd] %@", #keyPath(Article.title), self.searchText)
        } else if searchScope == .authors {
            predicate = NSPredicate(format: "%K CONTAINS[cd] %@", #keyPath(Article.authorsForDisplay), self.searchText)
        }
        DispatchQueue.main.async {
            self.items.nsPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [self.searchControl.originalPredicate ?? NSPredicate(format: "TRUEPREDICATE"), predicate!])
        }
    }

When calling this line: self.items.nsPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [self.searchControl.originalPredicate ?? NSPredicate(format: "TRUEPREDICATE"), predicate!])

The crash will occur - or as often - not.

This works for most search string. Only some strings repeatedly cause the mentions crash. for example if I enter wasser it crashes. If I enter bat or evident or turbine or curtail or... it doesn't.

All valid and normal strings. Searches with zero results work as well, so entering xcojfoesjif will not give a crash. So far I can reproduce the crash using washer or human - I have not tried more words yet.

When I look at the call stack, it happens within private functions handling the Table display. There is no refresh code for the table since the the following fetch request is used - and it does automatically pre-render the view (as a @State would do):

@FetchRequest(
        entity: Article.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Article.year, ascending: false)], predicate: NSPredicate(format: "TRUEPREDICATE"), animation: .default)
    private var items: FetchedResults<Article>

I do not have a single line where I rely on an index for the data array. All done magically by SwiftUI. Otherwise it would be easy to solve ;)

The code is on Github: https://github.com/vrunkel/Peru

I can also supply my test data which needs to be imported from xml first.

To me it looks like a bug in SwiftUI's table implementation. Since the call stack contains _delegate_isGroupRow: before the crash, I wonder if for some reason the table wants to create sections? I don' have sections as seen in the code sample above regarding table display.

Hope that clarifies!

Just to check, try to replace

DispatchQueue.main.async

by a

asyncAfter

to delay 1 second

And tell what you get

After updating to Ventura 13.3 the error is gone. So as expected, internal bug. Happy.

Swift/Collection.swift:722: Fatal error: Index out of bounds
 
 
Q