-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash when deleting objects that are being observed #6536
Comments
'Key not found' indicates that some part of getting the count from the query is trying to look up an object in a table and not finding it, presumably because that object was deleted. There aren't very many places where queries do that, but I tried to check them all and so far haven't found a way to reproduce this (or spotted anything obviously suspicious). If you can figure out what the query is that causes this that'd probably help; it most likely involves either filtering a List or LinkingObjects or doing some sort of query over a link. |
The query that crashes and that we use to observe changes looks something like this:
The interesting thing about this is that when we do delete a bunch of the objects it doesn't crash. It only crashes after second deletion. The system that handles these first deletes the outdated Workaround for this was to not use
|
Have you had any success in finding a fix for this issue? It seems to be growing rapidly in numbers in our app with over 2k crashes now. I suppose the only thing we can do is to stop using |
No, I haven't been able to reproduce it and I haven't been able to figure out why it might be happening. |
I'm having similar issue in my app. I updated to Realm 5.3.3 and now if I leave an observer in place on my Results before deleting an object the app crashes. In both my case and the example pasted above, it's when we try to access the My workaround for this is to remove my observer before I delete an object, and then immediately refetch into my Results list as soon as the delete completes. Accessing count of the results crashes it. Any chance there might be a fix for this at some point? It was working fine before I updated to Realm v5 This is the query I'm using (parentID is a String):
Here's the log from the crash:
|
Wanted to post again to say that I've got two other objects I can delete and leave the observers in place and it seems fine. It's my more complex object that I'm having the observer issues with. With my complex object, I only have to delete one and it crashes with the 'Key not found' error. I was trying to figure out the difference between the two objects. I have a Chart and a Project. The Chart has a lot more data in it. It contains lists of other objects, lists of Int. Because it has so many sub objects I do some specific looping through the lists of objects and delete those first vs letting my cascade delete code handle it. That was a speed optimization I did a long time ago and it sped up the delete a lot. Both Chart and Project are using LinkingObjects so I can find the object that refers to it and delete that as well. In both cases I'm doing all the deleting as part of a single write transaction. I haven't been able to narrow down the issue yet - but am posting in case this helps someone else. |
Hey, I am able to reproduce this; I think the issue has to do with indexedProperties somehow. Following class with following test will crash 100% of the time
|
Thanks for the repro case! That does fail in the expected way for me as well. |
A reduced test case: @objcMembers public class Activity: Object {
dynamic var type: String = ""
override public static func indexedProperties() -> [String] {
return ["type"]
}
}
class RealmTest: XCTestCase {
func testOnFinished() {
var config = Realm.Configuration.defaultConfiguration
config.inMemoryIdentifier = "testrealm"
let realm = try! Realm(configuration: config)
try! realm.write {
for _ in 0..<5 {
realm.add(Activity(value: ["rootSync"]))
}
}
let activities = realm.objects(Activity.self).filter("type == %@", "rootSync")
print(activities.count)
try! realm.write {
realm.delete(Array(activities[0..<4]))
}
print(activities.count)
}
} It appears that the key thing which I hadn't tried is that at least in this case, it only breaks if all but the last item are deleted. |
@tgoyne Thanks for getting this fix done so quickly! Any reason why using SPM to include Realm does include this fix, but using Carthage does not? Both when using the precompiled binaries and also when building from source? |
There should not be any difference based on installation method. |
It looks like the sync release pulled in the wrong core version so the fix isn't present when using the prebuilt library for that. |
Goals
Listen to changes to Results that has a filter included and get notified when the results are changed.
Expected Results
Get notified of changes without crashing.
Actual Results
Realm v5.0.0 crashes when objects are deleted and there is an attached Results listener for them. This seems to happen only after a second round of deletion. This means that first when objects are deleted everything works as expected, but if you then run another round of deletions to those new objects, Realm crashes with the following stack trace:
This crash occurs if you try to read the results either from the
Results
collection directly or from theResults
that are passed as the first argument toResults.observe
's case.update
.The workaround for this is to not use
Results
at all and always refetch the data from Realm explicitly when a change notification is fired.Steps to Reproduce
Listen to changes and delete objects. Second round of deletion always crashes.
Code Sample
No code sample as of yet. I can try to isolate the issue and generate a reproducible sample from our massive code base.
Version of Realm and Tooling
Realm framework version: 5.0.0
Realm Object Server version: N/A
Xcode version: 11.5
iOS/OSX version: 13.5
Dependency manager + version: Cocoapods 1.9.1
The text was updated successfully, but these errors were encountered: