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

[ObjectSchema] Expose objectClass from RLMObjectSchema private interface #6244

Merged
merged 1 commit into from
Sep 23, 2019
Merged

[ObjectSchema] Expose objectClass from RLMObjectSchema private interface #6244

merged 1 commit into from
Sep 23, 2019

Conversation

liamnichols
Copy link
Contributor

Summary

This change proposes the addition of the computed variable public var objectClass: AnyClass { get } on ObjectSchema to expose the underlying RLMObjectSchema's private objectClass.

Motivation

When working with Swift models, realm-cocoa will 'demangle' the original class name when building the shared schema. As a result, no mechanism exists for looking up the associated class type from a Realms schema. This isn't something that most people will need to do, but it can be really beneficial in cases where we might need to store some additional information on model definitions that needs to be validated either at runtime or in unit tests.

Although the objectClass is private on RLMObjectSchema, a similar capability is possible in projects that use realm-cocoa directly since they'd be able to use NSClassFromString(objectSchema.className) however this will return nil in Swift using objectSchema.className since the value has been passed through +[RLMSwiftSupport demangleClassName:]

I hope there is no problem with such change, my inspiration for the change was that I noticed Realm.Configuration is actually doing a similar thing in order to expose the objectTypes getter since they're backed by a custom RLMSchema but I wanted similar functionality when reading object schemas from a Realm directly

RLMObjectSchema and ObjectSchema both expose the 'className' property however since Swift Object subclasses have their class names demangled via RLMSwiftSupport, there is no way to associate an ObjectSchema instance back to its original Swift class. This change exposes a mechanism for such capabilities and adds unit test coverage to validate it
@tgoyne
Copy link
Member

tgoyne commented Sep 6, 2019

I need to think about this for a bit. There isn't any obvious reason why it'd be a problem (even the edge cases like inspecting the schema during migrations are probably fine), but it is the first thing that ties a Swift schema type's public interface to a specific Realm.

@tgoyne tgoyne merged commit 4a73445 into realm:master Sep 23, 2019
@realm-probot
Copy link

realm-probot bot commented Sep 23, 2019

Hey - looks like you forgot to add a T-* label - could you please add one (if you have access to add labels)?

@tgoyne
Copy link
Member

tgoyne commented Sep 23, 2019

I failed to come up with any plausible way that this could be a problem that doesn't involve us making breaking changes anyway, so seems fine.

@liamnichols liamnichols deleted the ln/object-schema/object-class branch September 23, 2019 17:29
@TTLoveQX
Copy link

TTLoveQX commented Oct 4, 2019

Hello there is a crash on realm. It crashed 'libc++abi.dylib: terminating with uncaught exception of type NSException' when I run it on Xcode 11.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants