louisdh / textor

A plain text editor for iOS
MIT License
585 stars 113 forks source link

`FileManager.url(forUbiquityContainerIdentifier:)` should not be called from main thread #12

Open aamct2 opened 6 years ago

aamct2 commented 6 years ago

Summary

FileManager.url(forUbiquityContainerIdentifier:) is currently being called on the main thread, against Apple's guidance.

Details

At the moment you are instantiating the shared DocumentManager in the DocumentBrowserViewController.viewDidLoad (), which is happening on the main thread.

https://github.com/louisdh/textor/blob/f97306e5e97d8f204512a345a6f397d781784aec/Textor/Controller/DocumentBrowserViewController.swift#L11-L17

When you step through the initialisation of the DocumentManager, you eventually get to a call for the cloudDocumentsURL property which is where the call to FileManager.url(forUbiquityContainerIdentifier:) happens.

https://github.com/louisdh/textor/blob/f97306e5e97d8f204512a345a6f397d781784aec/Textor/Util/DocumentManager.swift#L60-L66

This goes against Apple's guidance for FileManager.url(forUbiquityContainerIdentifier:) that it should not be called from the main thread.

Important

Do not call this method from your app’s main thread. Because this method might take a nontrivial amount of time to set up iCloud and return the requested URL, you should always call it from a secondary thread. To determine if iCloud is available, especially at launch time, check the value of the ubiquityIdentityToken property instead.

Link to full Apple documentation: https://developer.apple.com/documentation/foundation/filemanager/1411653-url

louisdh commented 6 years ago

Thanks for pointing this out, I'll take a look.