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

NSEntityDescription can only be used in a single MOM #20

Closed
helje5 opened this issue Oct 1, 2023 · 3 comments
Closed

NSEntityDescription can only be used in a single MOM #20

helje5 opened this issue Oct 1, 2023 · 3 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@helje5
Copy link
Contributor

helje5 commented Oct 1, 2023

I guess it is OK, but one entity can only be used in a single MOM, otherwise it throws an internal inconsistency exception:

Can't use an entity in two models.

There is no really nice way around it, but internally MOMs are already uniqued based on the input types, so that is probably not a huge deal in practice.

Also, MOMs do not seem to go away when they are released. Or the entities keep a backref to them:

    try autoreleasepool { // doesn't help
      let schema1 = NSManagedObjectModel(
    }
    // Can't use an entity in two models. (NSInternalInconsistencyException)
    let schema2 = NSManagedObjectModel(
      [ Fixtures.PersonAddressSchema.Person.self ],
      schemaCache: cache
    )

Not sure there is a way around this. Entities could be copied, but they are likely still tied to the classes?

@helje5 helje5 added help wanted Extra attention is needed question Further information is requested labels Oct 1, 2023
@helje5
Copy link
Contributor Author

helje5 commented Oct 2, 2023

Thinking about it, if any class is requested for an entity, that should result in the same MOM. Because the entity will be bound to it.
Currently the code uniques on the set of types requested.

E.g. this yields the same MOM:

.modelContainer(for: Item.self, Group.self)
.modelContainer(for: Item.self, Group.self)

but this won't (w/ automatic dependency discovery):

.modelContainer(for: Item.self, Group.self)
.modelContainer(for: Item.self)

@helje5
Copy link
Contributor Author

helje5 commented Oct 4, 2023

Well, actually that could be made to work if the entity is never cached, but always built from scratch by the builder.

Though I'm still not convinced it has a relevant use case (unlike freestanding classes in CoreData). The closest one would be sharing types between two different schema versions, but I think that's not even supported by SwiftData.

@helje5
Copy link
Contributor Author

helje5 commented Oct 7, 2023

Works now, fixed.

@helje5 helje5 closed this as completed Oct 7, 2023
@helje5 helje5 self-assigned this Oct 7, 2023
@helje5 helje5 added bug Something isn't working and removed question Further information is requested labels Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant