Hello,
I have an app which is enabled to synchronize the data with iCloud.
The data inherit from UIDocument class and is overridden the necessary methods.
In my ViewController, I observe the NSMetadataQueryDidUpdate and when I receive the notification, I iterate the results and collect all the changed URLs then reload the data of each URL to update the UI.
In the loading method, I open the document of relevant each URL to get the contents as follows.
for url in urlList {
autoreleasepool {
let coordinator = NSFileCoordinator(filePresenter: nil)
coordinator.coordinate(readingItemAt: url, options: .withoutChanges, error: nil, byAccessor: { (url) in
let doc = MyDocument(fileURL: url)
doc.open { (success) in
:
let title = doc.title
let image = doc.image
:
doc.close(comeletionHandler: { (success) in
:
}
}
}
}
}
Then I update the UI with the updated contents.
These process has been worked well until I updated to iOS11.
Since I updated to iOS11, NSMetadataQueryDidUpdate notification is sent again after a few seconds of the loading process.
I don't know why and I am not sure but I think the open and close of UIDocument evoke the notification because when I commented out from the open to close part, the notification never came until I actually update some contents.
Anyway, my app is stacked in the repetition of process as follows.
- Launch the app and receive the NSMetadataQueryDidUpdate notification first after starting the observation.
- Collect the URL list to reload the data from the notification.
- Open the documents to get the contents from each URL then close
- Update the UI
- Again receive the NSMetadataQueryDidUpdate about few seconds after the data loading and repeat the process(back to 2.)
As a result, my app repeats from 2. to 5. infinitely
Am I something wrong? How should I handle this situation?