title: Realm - Why are notification blocks triggered when a write transaction begins?
body: <p>I noticed today that Realm notification blocks are triggered when a write transaction begins. Intuitively I would have thought that <em>ending</em> a write transaction would trigger collection notifications with the changes that just happened, but I tracked down a crash in my code today resulting from a notification block being called when a write transaction begins.</p>
  
  <p>Relevant stack frames before my notification handling is invoked:</p>
  
  <pre><code>    frame #17: 0x0000000102535b98 Realm`RLMNotificationToken* RLMAddNotificationBlock&lt;realm::Results&gt;(this=0x00000001742974f0, changes=0x000000016fdf9de0, err=&lt;unavailable&gt;) block_pointer, bool)::'lambda'(realm::CollectionChangeSet const&amp;, std::exception_ptr)::operator()(realm::CollectionChangeSet const&amp;, std::exception_ptr) const + 608 at RLMCollection.mm:345
      frame #18: 0x0000000102535700 Realm`realm::CollectionChangeCallback::Impl&lt;RLMNotificationToken* RLMAddNotificationBlock&lt;realm::Results&gt;(objc_object*, realm::Results&amp;, void (objc_object*, RLMCollectionChange*, NSError*) block_pointer, bool)::'lambda'(realm::CollectionChangeSet const&amp;, std::exception_ptr)&gt;::after(this=0x00000001742974e8, change=0x000000016fdf9de0) + 56 at collection_notifications.hpp:157
      frame #19: 0x000000010248723c Realm`realm::CollectionChangeCallback::after(this=0x000000016fdf9dd0, c=0x000000016fdf9de0) + 64 at collection_notifications.hpp:122
      frame #20: 0x0000000102487198 Realm`auto realm::_impl::CollectionNotifier::after_advance(this=0x000000016fdf9f90, lock=0x000000016fdf9ef0, callback=0x0000000103b62520)::$_9::operator()&lt;std::__1::unique_lock&lt;std::__1::mutex&gt;, realm::_impl::CollectionNotifier::Callback&gt;(std::__1::unique_lock&lt;std::__1::mutex&gt;&amp;, realm::_impl::CollectionNotifier::Callback&amp;) const + 156 at collection_notifier.cpp:326
      frame #21: 0x0000000102479780 Realm`void realm::_impl::CollectionNotifier::for_each_callback&lt;realm::_impl::CollectionNotifier::after_advance()::$_9&gt;(this=0x00000001049d3e18, fn=0x000000016fdf9f90)::$_9&amp;&amp;) + 236 at collection_notifier.cpp:367
      frame #22: 0x0000000102479688 Realm`realm::_impl::CollectionNotifier::after_advance(this=0x00000001049d3e18) + 28 at collection_notifier.cpp:315
      frame #23: 0x000000010247ba3c Realm`realm::_impl::NotifierPackage::after_advance(this=0x000000016fdfa5e8) + 352 at collection_notifier.cpp:474
      frame #24: 0x00000001026c8de4 Realm`void (anonymous namespace)::advance_with_notifications&lt;realm::_impl::transaction::begin(context=0x0000000174221480, sg=0x0000000104020200, func=0x000000016fdfa540, notifiers=0x000000016fdfa5e8)::$_1&gt;(realm::BindingContext*, realm::SharedGroup&amp;, realm::_impl::transaction::begin(realm::SharedGroup&amp;, realm::BindingContext*, realm::_impl::NotifierPackage&amp;)::$_1&amp;&amp;, realm::_impl::NotifierPackage&amp;) + 1152 at transact_log_handler.cpp:674
      frame #25: 0x00000001026c8958 Realm`realm::_impl::transaction::begin(sg=0x0000000104020200, context=0x0000000174221480, notifiers=0x000000016fdfa5e8) + 56 at transact_log_handler.cpp:702
      frame #26: 0x00000001024de620 Realm`realm::_impl::RealmCoordinator::promote_to_write(this=0x0000000103b0e108, realm=0x0000000103b0e498) + 328 at realm_coordinator.cpp:741
      frame #27: 0x00000001026766b4 Realm`realm::Realm::begin_transaction(this=0x0000000103b0e498) + 552 at shared_realm.cpp:483
      frame #28: 0x000000010262df3c Realm`::-[RLMRealm beginWriteTransaction](self=0x00000001740a9fc0, _cmd="beginWriteTransaction") + 48 at RLMRealm.mm:437
  </code></pre>
  
  <p>In a particular use case in my code, one of my notification callbacks creates a new set of RLMResults to display in a table and adds a notification block to it. Adding the notification block raises the expected exception in that case: <code>Cannot create asynchronous query while in a write transaction</code>. </p>
  
  <p>That rule is easy to understand, I'm really just curious about why beginning a write transaction would trigger collection notifications, instead of waiting until after the transaction.</p>