Skip to content
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

[RLMSchema] Don't iterate through runtime classes unnecessarily #8721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ajjnix
Copy link

@ajjnix ajjnix commented Nov 25, 2024

In my application, there are quite a few crashes with an unclear stack trace (see the screenshot). During my investigation, I found out what is happening:

  1. We add a class to Realm.
  2. We release a version.
  3. We remove the class from the application's source code.
  4. In the migration, we call deleteData(forType:). Yes, I am aware of the crosslink issue; this is not that situation. I will look into that MR later with my experience on how to fix the deletion problem.
  5. deleteData(forType:) tries to determine whether to clear or delete the table.
  6. To do this, it calls -schemaForClassName (RLMSchema).
  7. I have explicitly defined all classes (in objectSchemaByName after +(instancetype)schemaWithObjectClasses), and none of them includes the one I'm deleting, which makes sense.
  8. But it seems excessive that a search is performed in +classForString (RLMSchema), which then goes to RLMRegisterClassLocalNames and iterates through all classes. And I remind you that I have explicitly specified the ones I am using.
  9. We end up in RLMIsObjectSubclass.
  10. And already in class_getSuperclass, something goes wrong.

I understand that it would be good to figure out which specific class is causing this issue, but I don't have that capability. However, it seems that this MR will eliminate the need to call class_getSuperclass unnecessarily.
389705538-fd872a2c-81bf-4bba-bff1-09cd78a838a2

@ajjnix ajjnix changed the title Don't iterate through runtime classes unnecessarily [RLMSchema] Don't iterate through runtime classes unnecessarily Nov 25, 2024
@tgoyne
Copy link
Member

tgoyne commented Dec 2, 2024

This will break initializing unmanaged objects prior to opening a Realm. Creating unmanaged objects using the value: initializer lazily loads the schema for that specific class, so the second object type created will see non-empty _objectSchemaByName but the class's schema won't be present.

The correct fix here is that RLMSchema needs a separate method which checks for the existence of an ObjectSchema without trying to create it if it's missing, which deleteDataForClassName: would use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants