Realm object has been deleted or invalidated

后端 未结 12 1104
粉色の甜心
粉色の甜心 2020-12-13 08:20

When I start my app, I perform an API call to see whether there\'s new data available. The data is stored in my local Realm database, and some of it is displayed in the init

相关标签:
12条回答
  • 2020-12-13 08:56

    You can check if an object has been deleted from the Realm by calling object.invalidated -- if it returns true, then it has been deleted or the Realm has manually invalidated.

    0 讨论(0)
  • 2020-12-13 08:58

    I've just place breakpoint inside method:

    // Realm/RLMUtil.mm:193
    static NSException *RLMException(NSString *reason, NSDictionary *additionalUserInfo) {
        // ...
    }
    

    And on left panel you can check stack trace, here you can find where error throws.

    0 讨论(0)
  • 2020-12-13 08:59

    I was unable to place breakpoints within the Realm framework itself as others suggested, but instead placed an exception breakpoint on my entire project:

    This allowed me to catch a proper stack trace when the exception was thrown, and find my bug.

    0 讨论(0)
  • 2020-12-13 09:02

    I got a really nice way to catch a RLMException within Swift.

    Currently Swift doesn't show where a RLMException happened.

    In Realm/RLMUtil.mm:266, there is the definition for RLMException.

    If you change the code to generate a swift error,

    Xcode now can show you where the exception occurred.

    Now it is a part of Swift.

    // Realm/RLMUtil.mm:266
    static NSException *RLMException(NSString *reason, NSDictionary *additionalUserInfo) {
        // add some code to generate a swift error. E.g. division-by-zero.
        int a = 0;
        if (reason == nil) {
            a = 1;
        }
        NSLog(@"calculating 1 / %d = %f", a, 1 / a);
    
        ... remainder of the original code...
    }
    
    0 讨论(0)
  • 2020-12-13 09:03

    In my experience, if you trying to use target object (which you wanna delete) after delete, application will crash.

    If you wanna trigger some code blocks after removing realm object, just trying to trigger that block right before the object removing in the memory. Trying to usage that object after removed from memory, will make some problem and will crash the app.

    For example:

        try! realm.write {
            print("deleted word: \(targetObject.word)")
            realm.delete(targetObject)
    
            //  targetObject was removed, so don't try to access it otherwise you gonna got the 'nil' value instead of object.
        }
    
    0 讨论(0)
  • 2020-12-13 09:03

    Seems like a common issue when using RxDataSources, Here is a work round which prevent the crash caused by adopting to IdentifiableType protocol.

    Check if the Realm object was invalidated. If so, just return a random unique value, like so:

      var identity: String {
            return isInvalidated ? "deleted-object-\(UUID().uuidString)" : objectId
        }
    

    Source: - https://github.com/RxSwiftCommunity/RxDataSources/issues/172#issuecomment-427675816

    0 讨论(0)
提交回复
热议问题