title: Firebase Realtime Database consuming CPU / memory with excessive "userWrites"
body: <p>I'm using the Firebase Realtime Database on an iOS project. I've had a growing number of reports from users about my app consuming lots of battery, and have recently done a bit of profiling. On my device the app is using 100% CPU and more than 0.5 Gb memory (and growing) for minutes at a time - the Time Profiler suggests its Firebase that's the culprit, with more than 8.77 minutes out of 10.09 minutes spent in the <code>[FRepo restoreWrites]</code> method (3.22 minutes in <code>[FCompoundWrite addWrite:atPath:]</code>). This is with persistence enabled.</p>
  
  <p>I dug in to the code for <code>restoreWrites</code> and it seems that its performance is related to the number of <code>userWrites</code> records. <code>[[[[[[reference repo] persistenceManager] storageEngine] writesDB] allKeys] count]</code> comes out at about 75,000 (and growing every time I launch the app) - is this how many writes have been persisted to disk that haven't yet been written to the server? Or is this the total number of records across all users? I don't know. But either way it's constantly growing and causing massive performance issues.</p>
  
  <p>Is this inevitable and a reason to migrate to FireStore? Or is this something wrong I'm doing that is fixable?</p>
  
  <p>Here's the full profile info to show you what I'm talking about:      </p>
  
  <pre><code>9.41 min   93.2%    0 s      _dispatch_workloop_worker_thread$VARIANT$mp  0x109890
  9.41 min   93.2%    0 s       _pthread_wqthread
  9.41 min   93.2%    0 s        _dispatch_workloop_worker_thread$VARIANT$mp
  9.41 min   93.2%    0 s         _dispatch_root_queue_drain_deferred_wlh$VARIANT$mp
  9.41 min   93.2%    0 s          _dispatch_queue_invoke$VARIANT$mp
  9.37 min   92.9%    0 s           _dispatch_queue_serial_drain$VARIANT$mp
  9.37 min   92.9%    0 s            _dispatch_client_callout
  9.37 min   92.9%    0 s             _dispatch_call_block_and_release
  8.78 min   87.0%    0 s              -[FRepo deferredInit]
  8.77 min   87.0%    0 s               -[FRepo restoreWrites]
  7.30 min   72.3%    0 s                -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
  6.55 min   64.9%    1.00 ms                 __22-[FRepo restoreWrites]_block_invoke
  5.69 min   56.3%    4.00 ms                  -[FSyncTree applyUserMergeAtPath:changedChildren:writeId:]
  5.67 min   56.2%    8.00 ms                   -[FWriteTree addMergeAtPath:changedChildren:writeId:]
  5.63 min   55.8%    5.00 ms                    -[FCompoundWrite addCompoundWrite:atPath:]
  5.63 min   55.8%    0 s                     -[FImmutableTree forEach:]
  5.63 min   55.8%    1.00 ms                      -[FImmutableTree forEachPathSoFar:withBlock:]
  3.43 min   34.0%    1.00 ms                       -[FArraySortedDictionary enumerateKeysAndObjectsReverse:usingBlock:]
  3.42 min   33.9%    3.00 ms                        __45-[FImmutableTree forEachPathSoFar:withBlock:]_block_invoke
  3.32 min   32.9%    7.00 ms                         -[FImmutableTree forEachPathSoFar:withBlock:]
  3.31 min   32.8%    6.00 ms                          __42-[FCompoundWrite addCompoundWrite:atPath:]_block_invoke
  3.22 min   31.9%    9.00 ms                           -[FCompoundWrite addWrite:atPath:]
  </code></pre>