Realm cancellation during update cancellation

I have a permanent crash in Realm when I try to delete a database while I am still executing update events in another thread.

Crash:

2017-08-14 18:07:56.289 App Staging[28264:7828070] *** Terminating app due to uncaught exception 'RLMException', reason: 'Can only add, remove, or create objects in a Realm in a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c67fb0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010c0e4141 objc_exception_throw + 48
    2   Realm                               0x0000000108095f96 _ZL27RLMVerifyInWriteTransactionP8RLMRealm + 86
    3   Realm                               0x000000010809710a RLMCreateObjectInRealmWithValue + 138
    4   Realm                               0x00000001080820af +[RLMObject createOrUpdateInRealm:withValue:] + 607
    5   App Staging                         0x0000000107497ae0 +[RealmRoundable createOrUpdateInRealm:withMemberResponse:] + 400
    6   App Staging                         0x0000000107497916 +[RealmRoundable createOrUpdateWithMemberResponse:] + 118
    7   App Staging                         0x000000010742a0a0 +[RealmStaff createOrUpdateInRealm:withResponse:inCareProvider:] + 352
    8   App Staging                         0x000000010742ab92 +[RealmStaff createOrUpdateInRealm:withStaff:inCareProvider:] + 514
    9   App Staging                         0x000000010742a94b +[RealmStaff createOrUpdateStaff:inCareProvider:] + 139
    10  App Staging                         0x000000010733b803 -[StaffRoundableTableViewController updateRoundables:fromDataLoader:inCareProvider:] + 131
    11  App Staging                         0x000000010747ae3a __54-[RoundableTableViewController dataLoaderDidLoadData:]_block_invoke.324 + 122
    12  Realm                               0x00000001081d01a6 -[RLMRealm transactionWithBlock:error:] + 86
    13  Realm                               0x00000001081d010e -[RLMRealm transactionWithBlock:] + 62
    14  App Staging                         0x000000010747ab0d __54-[RoundableTableViewController dataLoaderDidLoadData:]_block_invoke + 765
    15  libdispatch.dylib                   0x000000010e15c4a6 _dispatch_call_block_and_release + 12
    16  libdispatch.dylib                   0x000000010e18505c _dispatch_client_callout + 8
    17  libdispatch.dylib                   0x000000010e164dcd _dispatch_queue_override_invoke + 1321
    18  libdispatch.dylib                   0x000000010e166ec4 _dispatch_root_queue_drain + 634
    19  libdispatch.dylib                   0x000000010e166bef _dispatch_worker_thread3 + 123
    20  libsystem_pthread.dylib             0x000000010e51c5a2 _pthread_wqthread + 1299
    21  libsystem_pthread.dylib             0x000000010e51c07d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

This happens when I call:

[RealmManager deleteRealm];

What is implemented as:

+ (void)deleteRealm
{
    @autoreleasepool {
        [[RLMRealm defaultRealm] invalidate];
        // Hack to force Realm to clear cache because config is cached and crashes eventually because it detects encryption key has changed
        SUPPRESS_UNDECLARED_SELECTOR_WARNING([[RLMRealm class] performSelector:@selector(resetRealmState)]);
    }

    NSFileManager *manager = [NSFileManager defaultManager];
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    NSArray<NSURL *> *realmFileURLs = @[
                                        config.fileURL,
                                        [config.fileURL URLByAppendingPathExtension:@"lock"],
                                        [config.fileURL URLByAppendingPathExtension:@"log_a"],
                                        [config.fileURL URLByAppendingPathExtension:@"log_b"],
                                        [config.fileURL URLByAppendingPathExtension:@"note"],
                                        [[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.realm.management", [[config.fileURL URLByDeletingPathExtension] lastPathComponent]]]
                                        ];
    for (NSURL *URL in realmFileURLs) {
        NSError *error = nil;
        [manager removeItemAtURL:URL error:&error];
        if (error) {
            // handle error
            DDLogError(@"Error deleting realm file - %@", error);
        }
    }
}

My question is: is there a way to stop all Realm operations before running this code.

+4
source share
1 answer

From the Realm documentation on Removing Realm Files :

Realm , , , , Realm, , . , ( ) , RLMArray, RLMResults RLMThreadSafeReference RLMRealm.

, Realm , Realm, Realm , , Realm .

. Realm, , +[RLMRealm resetRealmState], . .

-, , Realm . , .

, , Realm . , Realm , .

Realm Realm. Realm, , , ( , , ). , . , , Realm .

+4

Source: https://habr.com/ru/post/1683710/


All Articles