title: UIDocument & NSFileWrapper Architecture and Performance
body: <p>We've recently converted our code to use UIDocument instead of manipulating files on the file system directly, and we've encountered some performance issues as a result. We are wondering whether we are using this class incorrectly, if anyone else had these issues, and what are the common ways to address them.</p>
  
  <h2>Our app</h2>
  
  <p>We have a "shoebox app" that manages a bunch of documents, each consisting of multiple image files that can be quite heavy, a small metadata file and a small preview image. The user may have many documents on her device (1000+ documents). Each document's files are grouped in an directory and we use NSFileWrapper to read and write them.</p>
  
  <p>When our app starts, it needs the metadata of all the documents in order to show a document index, and a subset of the preview images. More preview images are loaded as the user scrolls.</p>
  
  <p>In order to get that information, we open all the documents, read their metadata and preview image if we need to, close them, and then open again on demand.</p>
  
  <h2>Problem #1: Slow loading time</h2>
  
  <p>It takes a lot of time to open all the documents and read their metadata. I think there are several factors contributing to this problem:
   - Each document open action is relatively slow
   - The open document blocks and the completion blocks are executed on the same queue, which makes the operation's latency very bad (my document is open, but the completion block has to wait for X open document blocks before it can run)</p>
  
  <p>We thought about solving this problem using a separate index file, but this approach has the drawback that we will need to manage the metadata in two places and that we will need to keep it synched with the file system in case iCloud changes the files.</p>
  
  <h2>Problem #2: Threading</h2>
  
  <p>Each open document creates its own "File Access Thread". When we open many documents concurrently, the overhead crushes the app.</p>
  
  <p>We solved this issue by synching the open operations using a semaphore. This approach has the drawback that it slows down the loading even more.</p>
  
  <h2>Questions</h2>
  
  <ol>
  <li>Is there some fundamental problem with the way we are using UIDocument? If not:</li>
  <li>Has anyone encountered a similar loading time problem? What is the common way to solve it? Do other apps keep an index file? </li>
  <li>Is there a way to make UI document use a thread pool? If not, how do you control resource usage?</li>
  </ol>
  
  <p>Thanks!</p>