We recently converted our code to use UIDocument, rather than directly manipulating files on the file system, and as a result we ran into some performance issues. We are wondering if we are using this class incorrectly if someone had these problems, and what are the common ways to fix them.
Our app
We have a โshoe appโ that manages a bunch of documents, each of which consists of several image files that can be quite heavy, a small metadata file and a small preview image. A user can have many documents on his device (1000+ documents). Each document file is grouped into a directory, and we use NSFileWrapper for reading and writing.
When our application starts, it needs the metadata of all documents to show the document index and a subset of the preview images. Additional preview images are loaded as the user scrolls.
To get this information, we will open all the documents, read their metadata and the preview image, if necessary, close them, and then open them again on demand.
Problem # 1: Slow boot time
It takes a long time to open all documents and read their metadata. I think there are several factors contributing to this problem: - Each document opening action is relatively slow - Open document blocks and completion blocks are executed in one queue, which makes it very difficult to delay the operation (my document is open, but the completion block should wait for X open document blocks before its launch)
We decided to solve this problem with a separate index file, but this approach has the disadvantage that we will need to manage metadata in two places and that we will need to synchronize it with the file system in case iCloud modifies files.
Problem # 2: Threading
Each open document creates its own "File Access Thread". When we open many documents at the same time, overhead depresses the application.
We solved this problem by synchronizing open operations using a semaphore. This approach has the disadvantage that it slows down loading even more.
Questions
- Is there any fundamental problem with the way we use UIDocument? If not:
- Has anyone encountered a similar boot time issue? What is the general way to solve it? Do other applications have an index file?
- Is there a way to force a UI document to use a thread pool? If not, how do you control the use of resources?
Thanks!
performance multithreading ios uidocument
Hila
source share