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

[Model] Store and retrieve entities by type #68

Open
jsbean opened this issue Aug 28, 2018 · 1 comment
Open

[Model] Store and retrieve entities by type #68

jsbean opened this issue Aug 28, 2018 · 1 comment

Comments

@jsbean
Copy link
Member

jsbean commented Aug 28, 2018

A little bit of a work around needs to be made In order to keep the model type-safe:

extension Model {
    var entitiesByType: [String: [Identifier]] = [:]
    func store <T> (_ value: T, ...) -> Identifier {
        let identifier = // make new identifier
        entitiesByType.safelyAppend(value, forKey: "\(T.self)") 
        return identifier
    }
    func retrieve <T> (_ identifier: Identifier) -> T {
        return entities["\(T.self)"] as! [T]
    }
}
@jsbean
Copy link
Member Author

jsbean commented Sep 4, 2018

The currently solution uses the String key. This is problematic for two reasons: with thousands or millions of hits, the creation of strings for the types is quite expensive. Furthermore, this doesn't actually preserve type information.

Instead, it may be better to implement Model as a protocol. Concrete implementations of Model-conforming types could extend the specific types they can store while retaining type information.

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

No branches or pull requests

1 participant