title: Exporting large amounts of data from coredata to json
body: <p>I'm trying to export some data from core data to JSON. While the record count isn't particularly large (around 5000-15000 records), my data model is complex and there is a large amount of data in each record, so when I export this I exceed the allowable memory and iOS kills my app.</p>
  
  <p>The steps i currently take are:</p>
  
  <ul>1. I have a method that extracts all the data from cordata and stores it an `NSDictionary`</ul>
  
  <ul>2. I then write it to a file using an `NSOutputStream` and `NSJSONSerialization`</ul>
  
  <ul>3. I then zip up the file and send it via email</ul>
  
  <p>I'm pretty sure that steps 2 and 3 are fine from a max memory perspective as I stream the data. But the problem is that it gets killed in step 1 because I'm effectively pulling all the data out of CD and putting it in memory so I can pass it through <code>NSOutputStream</code> to <code>NSJSONSerialization</code>.</p>
  
  <p>Anyone know how to not have to pull everything into memory, but still write to a single tree JSON file?</p>
  
  <p><strong>Update - More Detail</strong><br>
  My data structure (simplified for clarify) looks like this.
  Given its not just a flat set of records but a hierarchal structure of objects with relationships i cant figure out how to pull the data out of core data in batches and fed tot he json streamer rather than all in memory to construct the json. my step one above is actually a collection of recursive methods that pull the data out of the core data entities and construct the 'NSDictionary'.</p>
  
  <pre><code>Folder {
      Folder {
          Word {
              details type 1
              details type 2
          }
          Word {
              details type 1
              details type 2
          }
      }
      Folder {
          Word {
              details type 1
              details type 2
          }
          Word {
              details type 1
              details type 2
          }
      }
      Word {
          details type 1
          details type 2
      }
  }
  </code></pre>