Hello, sometimes if I use NSMetadataQuery to obervse my file changes on macOS, it crash for this reason, its odd and i have no clue for this problem becuse in my code I never get results using index, anyone help? thanks!
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSMetadataQueryResultArray objectAtIndex:]: index (251625) out of bounds (251625)'
terminating with uncaught exception of type NSException
abort() called
Thanks for the latest info.
Based on that it looks like you’re using metadataQuery
from two different contexts:
-
The main thread, where your
getAllResults()
method is accessing theresults
property (this is based on frame 6 of the crash report backtrace I posted earlier). -
The custom serial operation queue (
workerQueue
) which is backed by a Dispatch serial queue (dispatchQueue
).
That’s a concern. AFAICT this is meant to work, but any sort of concurrency bug in NSMetadataQuery
could trigger exactly this crash.
The ‘obvious’ next step would be to do all that work from a single context, and that means your serial queue. I’m imagining code like this:
func getAllResults(…) … {
let results = dispatchQueue.sync {
metadataQuery.results
}
…
}
Now, if you could reproduce this crash then I’d tell you to go ahead and make this change in a branch, test it, and see if it helps. However, it sounds like you’re debugging this based on crash reports coming in from the field, and that makes things trickier. You’d have to incorporate this change into your mainline product, and that engenders some risk. Specifically, any time you do a sync dispatch from the main thread to a secondary thread you run the risk of deadlock. The code running within your sync closure is small, so I think it’s safe, but there’s always a risk.
If your product has a significant beta test programme, you could make this change in a beta release and see if causes problems in practice.
Of course, it’s still not clear whether this change will actually help prevent the problem )-:
Sorry that I don’t have all the answers here. Concurrency is hard™.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"