Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-925

Unexpected iOS Crash: "Cannot register notification blocks from within write transactions."

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      <!---

      Questions: If you have questions about HOW TO USE Realm, ask on
      StackOverflow,
      or in our Swift Forum or Obj-C Forum.

      Feature Request: Just fill in the first two sections below.

      Bugs: To help you as fast as possible with an issue please describe your issue
      and the steps you have taken to reproduce it in as many details as possible.

      -->

      Goals

      <!--- What do you want to achieve? -->
      I want to help to fix the crash.

      Expected Results

      <!--- What did you expect to happen? -->
      No crash.

      Actual Results

      <!--- What happened instead?
      e.g. the stack trace of a crash
      -->
      Crash.

      Steps for others to Reproduce

      <!--- What are steps OTHERS can follow to reproduce this issue? -->
      Just run the demo project.

      Code Sample

      <!---
      Provide a code sample or test case that highlights the issue.
      If relevant, include your model definitions.
      For larger code samples, links to external gists/repositories are preferred.
      Alternatively share confidentially via mail to help@realm.io.
      Full Xcode projects that we can compile ourselves are ideal!
      -->
      realm-sample.zip

      Version of Realm and Tooling

      <!---
      In the CONTRIBUTING guidelines, you will find a script,
      which will help determining some of these versions.
      -->

      ProductName:	Mac OS X
      ProductVersion:	10.15.6
      BuildVersion:	19G2021
      
      /Applications/Xcode.app/Contents/Developer
      Xcode 12.0
      Build version 12A7209
      
      /usr/local/bin/pod
      1.9.3
      Realm (5.5.0)
      RealmSwift (5.5.0)
      Realm (from `https//github.com/realm/realm-cocoa.git`, tag `v5.5.0`)
      RealmSwift (from `https//github.com/realm/realm-cocoa.git`, tag `v5.5.0`)
      
      /bin/bash
      GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
      
      carthage not found
      (not in use here)
      
      /usr/bin/git
      git version 2.24.3 (Apple Git-128)
      

      Description

      It looks like that if a Realm.write { }}} is executed on the same thread as the {{Results.observe { }}}, the write transaction is not fully closed before the observe callback gets invoked. In the provided code example, the observe callback contains a {{ViewController2.beginAppearanceTransition() call which leads to the ViewController2.viewWillAppear() being called during the write transaction. The Problem is that the ViewController2.viewWillAppear() contains a {{Results.observe { }}} as well which leads to the error described in the stack trace below.

      Stack trace

      2020-10-14 15:17:12.918034+0200 realm-sample[46718:1573132] *** Terminating app due to uncaught exception 'RLMException', reason: 'Cannot register notification blocks from within write transactions.'
      *** First throw call stack:
      (
      	0   CoreFoundation                      0x0000000108414126 __exceptionPreprocess + 242
      	1   libobjc.A.dylib                     0x00000001057b3f78 objc_exception_throw + 48
      	2   Realm                               0x00000001067981db -[RLMRealm verifyNotificationsAreSupported:] + 363
      	3   Realm                               0x00000001066736f2 _Z23RLMAddNotificationBlockI10RLMResultsEP20RLMNotificationTokenPT_U13block_pointerFvP11objc_objectP19RLMCollectionChangeP7NSErrorEPU28objcproto17OS_dispatch_queue8NSObject + 370
      	4   Realm                               0x00000001067c6b4e -[RLMResults addNotificationBlock:queue:] + 78
      	5   RealmSwift                          0x0000000104e63099 $s10RealmSwift7ResultsV7observe2on_So20RLMNotificationTokenCSo17OS_dispatch_queueCSg_yAA0A16CollectionChangeOyACyxGGctF + 489
      	6   realm-sample                        0x0000000104ab2e32 $s12realm_sample15ViewController2C14viewWillAppearyySbF + 354
      	7   realm-sample                        0x0000000104ab3048 $s12realm_sample15ViewController2C14viewWillAppearyySbFTo + 56
      	8   UIKitCore                           0x00000001116aefb2 -[UIViewController _setViewAppearState:isAnimating:] + 654
      	9   UIKitCore                           0x00000001116af7db -[UIViewController __viewWillAppear:] + 106
      	10  realm-sample                        0x0000000104ab2005 $s12realm_sample14ViewControllerC13viewDidAppearyySbFy10RealmSwift0H16CollectionChangeOyAE7ResultsVyAA0H6ObjectCGGcfU_ + 85
      	11  RealmSwift                          0x0000000104e63114 $s10RealmSwift0A16CollectionChangeOyAA7ResultsVyxGGIegg_AGIegn_AA0aC5ValueRzlTR + 52
      	12  RealmSwift                          0x0000000104e53b09 $s10RealmSwift20ObservableCollectionPAAE16wrapObserveBlockyy011BackingObjcD0QzSg_So19RLMCollectionChangeCSgs5Error_pSgtcyAA0adK0OyxGcFyAG_AjLtcfU_ + 1625
      	13  RealmSwift                          0x0000000104e541aa $s10RealmSwift20ObservableCollectionPAAE16wrapObserveBlockyy011BackingObjcD0QzSg_So19RLMCollectionChangeCSgs5Error_pSgtcyAA0adK0OyxGcFyAG_AjLtcfU_TA + 106
      	14  RealmSwift                          0x0000000104e235b4 $sSo10RLMResultsCyyXlGSgSo19RLMCollectionChangeCSgs5Error_pSgIegngg_AdgIIegggg_TR + 84
      	15  RealmSwift                          0x0000000104e236c1 $sSo10RLMResultsCyyXlGSgSo19RLMCollectionChangeCSgs5Error_pSgIegggg_AdGSo7NSErrorCSgIeyByyy_TR + 161
      	16  Realm                               0x000000010667b684 _ZN12_GLOBAL__N_125CollectionCallbackWrapperclERKN5realm19CollectionChangeSetESt13exception_ptr + 388
      	17  Realm                               0x000000010667b315 _ZN5realm24CollectionChangeCallback4ImplIN12_GLOBAL__N_125CollectionCallbackWrapperEE5afterERKNS_19CollectionChangeSetE + 69
      	18  Realm                               0x0000000106531436 _ZN5realm24CollectionChangeCallback5afterERKNS_19CollectionChangeSetE + 38
      	19  Realm                               0x0000000106531399 _ZZN5realm5_impl18CollectionNotifier13after_advanceEvENK4$_10clINS_4util17CheckedUniqueLockENS1_8CallbackEEEDaRT_RT0_ + 169
      	20  Realm                               0x0000000106510ae4 _ZN5realm5_impl18CollectionNotifier17for_each_callbackIZNS1_13after_advanceEvE4$_10EEvOT_ + 148
      	21  Realm                               0x0000000106510a49 _ZN5realm5_impl18CollectionNotifier13after_advanceEv + 25
      	22  Realm                               0x0000000106511ce0 _ZN5realm5_impl15NotifierPackage13after_advanceEv + 336
      	23  Realm                               0x00000001068e3548 _ZN12_GLOBAL__N_126advance_with_notificationsIZN5realm5_impl11transaction5beginERKNSt3__110shared_ptrINS1_11TransactionEEEPNS1_14BindingContextERNS2_15NotifierPackageEE3$_2EEvSB_S9_OT_SD_ + 1208
      	24  Realm                               0x00000001068e3081 _ZN5realm5_impl11transaction5beginERKNSt3__110shared_ptrINS_11TransactionEEEPNS_14BindingContextERNS0_15NotifierPackageE + 49
      	25  Realm                               0x00000001065e146c _ZN5realm5_impl16RealmCoordinator16promote_to_writeERNS_5RealmE + 316
      	26  Realm                               0x0000000106852271 _ZN5realm5Realm17begin_transactionEv + 545
      	27  Realm                               0x0000000106798cac -[RLMRealm beginWriteTransactionWithError:] + 44
      	28  Realm                               0x0000000106798c78 -[RLMRealm beginWriteTransaction] + 40
      	29  RealmSwift                          0x0000000104e458b5 $s10RealmSwift0A0V10beginWriteyyF + 53
      	30  RealmSwift                          0x0000000104e4552f $s10RealmSwift0A0V5write16withoutNotifying_xSaySo20RLMNotificationTokenCG_xyKXEtKlF + 223
      	31  realm-sample                        0x0000000104ab1e55 $s12realm_sample14ViewControllerC13viewDidAppearyySbF + 645
      	32  realm-sample                        0x0000000104ab2218 $s12realm_sample14ViewControllerC13viewDidAppearyySbFTo + 56
      	33  UIKitCore                           0x00000001116af0bc -[UIViewController _setViewAppearState:isAnimating:] + 920
      	34  UIKitCore                           0x00000001116afa93 -[UIViewController __viewDidAppear:] + 146
      	35  UIKitCore                           0x00000001116afd6c -[UIViewController _endAppearanceTransition:] + 232
      	36  UIKitCore                           0x0000000111583b66 __48-[UIPresentationController transitionDidFinish:]_block_invoke + 138
      	37  UIKitCore                           0x00000001122ce97a -[_UIAfterCACommitBlock run] + 54
      	38  UIKitCore                           0x0000000111de785d _runAfterCACommitDeferredBlocks + 333
      	39  UIKitCore                           0x0000000111dd7790 _cleanUpAfterCAFlushAndRunDeferredBlocks + 221
      	40  UIKitCore                           0x0000000111e09154 _afterCACommitHandler + 85
      	41  CoreFoundation                      0x00000001083816b3 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
      	42  CoreFoundation                      0x000000010837bf3f __CFRunLoopDoObservers + 547
      	43  CoreFoundation                      0x000000010837c4e2 __CFRunLoopRun + 1113
      	44  CoreFoundation                      0x000000010837bb9e CFRunLoopRunSpecific + 567
      	45  GraphicsServices                    0x00000001102f7db3 GSEventRunModal + 139
      	46  UIKitCore                           0x0000000111dd8af3 -[UIApplication _run] + 912
      	47  UIKitCore                           0x0000000111ddda04 UIApplicationMain + 101
      	48  libswiftUIKit.dylib                 0x0000000107c9b7b2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
      	49  realm-sample                        0x0000000104ab43aa $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
      	50  realm-sample                        0x0000000104ab431e $s12realm_sample11AppDelegateC5$mainyyFZ + 46
      	51  realm-sample                        0x0000000104ab43f9 main + 41
      	52  libdyld.dylib                       0x0000000107ebd415 start + 1
      )
      libc++abi.dylib: terminating with uncaught exception of type NSException
      terminating with uncaught exception of type NSException
      CoreSimulator 732.17 - Device: iPhone 6s (875C55C5-F7E8-4881-AC23-C82BD2503BE2) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone 6s
      *** Terminating app due to uncaught exception 'RLMException', reason: 'Cannot register notification blocks from within write transactions.'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: