NSMetaDataQuery and MD-Spotlight Backed APIs Suddenly Stop Working

I'm testing code that uses NSMetadataQuery and I noticed some odd behavior. I run a query where the search scope is set to my Desktop directory. The predicate is set to search for a file name that contains 'we' (display name to be precise: kMDItemDisplayName). And I get no results.

So I stop the app from Xcode then I create a new folder on the Desktop and name it we exactly. Then I rerun my app and start the query again - still no results.

Unfortunately none of the Spotlight based APIs I'm using report errors. There is no delegate method -queryDidFailWithError: or anything like that.

So in another place my app uses the lower level MD prefixed APIs in Core Services to read file metadata and I notice that those APIs are failing as well.

Specifically I try to load metadata for a PNG image. I create an MDItemRef via MDItemCreateWithURL (and I do get an MDItemRef). Then I call MDItemCopyAttributes with the MDItemRef and an array of metadata attribute names:

CFDictionaryRef fetchedData = MDItemCopyAttributes(mdItem, attributeNames);

I step through it in the debugger and fetchedData is NULL. Since there is no error handling APIs available for any of this my app just silently fails.

I have Console.app open when I step through MDItemCopyAttributes call in the debugger and this logs out every time:

com.apple.spotlightserver Bad checksum on fetch attributes reply from store, <private> 0x123ea456....<private>

FWIW I do not have Spotlight indexing turned off on my system.

Also jumping to Finder I'm experiencing the same failures (metadata isn't populating and the Desktop search for we produces no results).

Actually on further inspection it looks like all searches are producing no results. Spotlight appears to be in a broken state system wide.

I'm pretty sure a system restart will 'fix' it - until it starts occurring again. But for obvious reasons I'd like to avoid releasing a feature in my app that results in this kind of experience.

Is there anything I can do on my end to workaround or avoid this issue? And tips or advice would be greatly appreciated.

NSMetaDataQuery and MD-Spotlight Backed APIs Suddenly Stop Working
 
 
Q