spotlight and iCloud

Hi,


I'm trying to figured out how to utilize core spotlight in context of iCloud. In one app the app's content is stored files handled by a UIDocument subclass and the files usually are stored in iCloud.


One thing I stumbled across was how to detect when an indexed file is deleted from iCloud on a second device. Usually my app doesn't get informed about the file being deleted unless the files is currently in use. So how to keep the spotlight index up to date?


Dirk

From the iCloud Design Guide:


In iOS, actively track a document’s location in the file system. Each instance of your iOS app must be prepared for another instance to move, rename, or delete iCloud-based documents. If your app persistently stores a URL or path information to a file or file package, do not assume that the item will still be there the next time you attempt to access it.

In iOS, employ an

NSMetadataQuery
object, along with file coordination, to actively track the locations of your documents. Early in your app’s launch process, instantiate and configure a metadata query object, start it, and register for its
NSMetadataQueryDidUpdateNotification
notification. Implement the
presentedItemDidMoveToURL:
method and the
presentedItemURL
property to allow your app to respond to pushed changes from iCloud. Refresh your app’s model layer and update your app’s user interface elements as needed. For details on using metadata queries, see File Metadata Search Programming Guide.

So using these approaches you should be able to detect when a document is removed and then remove any related CSSearchableItem entries from CoreSpotlight.

Well, yes. This is partly correct. If my app is running I will notice any removal. I'm able to handle that case already.


But If my app is not running while a file is deleted from iCloud NSMetadataQuery returns just the remaining files on next launch. It seems I need to keep track of what files the app has last seen when it was active to be able to compare this list of files against the list of files on next launch. This way I could tell which files have been deleted while the app was closed.


Or is there an easier way?


Dirk

10 years later, there's still no definitive solution to this issue. How did you handle it?

spotlight and iCloud
 
 
Q