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

Get all of a kind #3

Closed
nicolasalliaume opened this issue Jan 16, 2016 · 3 comments
Closed

Get all of a kind #3

nicolasalliaume opened this issue Jan 16, 2016 · 3 comments
Labels

Comments

@nicolasalliaume
Copy link

Hi there!
How can I get all items stored under a certain Namespace? For example, I have the following namespace:

struct ListsNamespace: Namespace {
    static let id = "lists"
}

and I want to fetch all 'List' items.

Thanks!

@Mazyod
Copy link
Member

Mazyod commented Jan 17, 2016

Hello! That is not possible, by design, since a namespace doesn't restrict the type of items, it simply adds an extra component to the key of the item. Given that, if we try to retrieve all items, it won't be type safe anymore.

Can you please share more about your use case? I can help you figure out a better way to use this library, hopefully, or we can come up with a new feature that accommodates your needs.

(On the other hand, if you want to look at other established key value stores, like redis, you will see they don't allow fetching all items in a namespace, because that is more like a query, which is better accommodated by a database)

@nicolasalliaume
Copy link
Author

Hi! Thanks for your fast response!

I'm storing model instances by their id using the namespace I copied on my question.

I need now to fetch all of them to populate a table view.

It's a small app and there won't be more than 15 items stores under the namespace. I wanted to avoid incurring into the work of using a relational database for it.

I'm thinking a possible workaround for this could be storing an array of all stored item keys under a certain key (for example, lists:all) and update it using the existing hooks on the ListsNamespace.

@Mazyod
Copy link
Member

Mazyod commented Jan 17, 2016

Makes sense. You can definitely do that, or ... Check this out:

struct ListsNamespace: Namespace {

    static let id = "lists"

    static let all: [Key] = {
        return (0..<15).map {
            Key<ListsNamespace, String?>(id: "\($0)", defaultValue: nil)
        }
    }()
}

store.set(ListsNamespace.all[0], value: "1")
store.get(ListsNamespace.all[0]) // "1"

It might be easy to forget that Key is just an instance that can also be instantiated on the fly. In the example above, I have hardcoded the number of Key instances, but you can also make that variable, and do crazy stuff with it.

I hope this helps .. It is also making me wonder if there is value of adding support to KeyArray in library itself to simplify the implementation somehow ...

In all cases, please keep the feedback coming!

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

No branches or pull requests

2 participants