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.

Hm, sometimes search just gets stuck. I get a NSMetadataQueryDidStartGatheringNotification but no NSMetadataQueryDidFinishGatheringNotification.

Seems to happen when testings on a particular volume mount... Finder is able to get results for it. (but another volume mount works fine, both are SMB shares). I'm not doing anything extravagant - applying the same template predicate for the search every time.

And then there are times I get results brilliantly fast. Extremely frustrating. Feels like a magic box that works like 70% of the time.

I filed a bunch of feedbacks against this API:

FB24729304 FB24493001 FB24349840 FB24690162 FB24349840

I probably will file more against NSMetadataQuery soon. kind of disappointing spending so much time on this but may end up having to abandon.

Hm, sometimes search just gets stuck. I get a NSMetadataQueryDidStartGatheringNotification but no NSMetadataQueryDidFinishGatheringNotification.

To follow up - it may have not actually been stuck. I let it ride for around 30 minutes and finally got the 1 search result I was expecting.

NSMetaDataQuery and MD-Spotlight Backed APIs Suddenly Stop Working
 
 
Q