Skip to content

Separation of metadata from object data at save time? #79

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

Open
aranasaurus opened this issue Jan 26, 2016 · 9 comments
Open

Separation of metadata from object data at save time? #79

aranasaurus opened this issue Jan 26, 2016 · 9 comments
Labels

Comments

@aranasaurus
Copy link
Contributor

This is definitely a question, not a bug report, so read accordingly :)

I've recently been re-structuring my data a bit so that I have the fields that I sort on in the Metadata and everything else in the object data. Reading https://github.com/yapstudios/YapDatabase/wiki/Views#initialization-tips suggests that is the recommended way to avoid unnecessary recalculation of the sortings. The problem I'm running into is that because YapDatabaseExtensions writes the metadata every time it writes an object, even if that metadata hasn't changed, every time I write an object the database my sort blocks are being run, even though I haven't updated their sort-able properties.

My question is, am I missing something? Is there a way that I can accomplish this with YapDatabaseExtensions or is this an approach that it's just not designed for?

@danthorpe
Copy link
Owner

Hi @aranasaurus - this is a really good question.

Short answer is, I'm not sure - I'll confess to not doing this myself - I typically have sorting keys inside the object. However, we can try to figure out what is happening.

My first question / thought is.... can you share the general structure of your model and its metadata model. I'm most interested in knowing if your metadata is a value type or a regular NSCoding compliant class.

My second question / thought is... is it possible for you to organize your model so that the metadata property is only set on the model when the sorting properties change? It's the presence of this property (being .Some) which causes the metadata to be written. If the metadata is .None (I think) we just write the value for the key, which would leave any existing metadata in place & untouched.

@aranasaurus
Copy link
Contributor Author

For your first question: My data is structured such that my Objects are all the information that is needed to display said objects, and their metadata contains just the properties that can be sorted on. Both Object and Metadata are value types.

For the second question: It is not currently set up so that I could do that, but I think I could refactor it a little bit so that I could. In fact this line of thinking is what lead me here to ask the question, because I went looking into the YapDBExtensions code that writes the data and as far as I could tell it looks like it writes the metadata no matter what, so setting it to .None when I didn't want to trigger a save would clear my metadata out. ... But now I just went and double checked and it looks like you're right, if this is the method that everything boils down to, at least: https://github.com/danthorpe/YapDatabaseExtensions/blob/development/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift#L437

So that probably gives me what I need :) I'll try to do that refactor and let you know. Thanks!

@danthorpe
Copy link
Owner

Yes. That's what it boils down to.

There is perhaps some smarts that can be added here if your MetadataType conformed to Equatable...

@aranasaurus
Copy link
Contributor Author

It does. That's probably what I'm going to do, in my write transaction, check if the metadata of the already cached version is the same as the updated object, and if so set it to .None before actually writing it.

@danthorpe
Copy link
Owner

Yeah :). And this framework could do that automatically for you I think...

@aranasaurus
Copy link
Contributor Author

It'd be nice if the framework checked for equality on the metadata before writing it... especially if it meant that when I write an object with a .None metadata property it would do what I thought it already did (clear the metadata for that key).

@danthorpe
Copy link
Owner

Yeah, totally agree with you. It's a really subtle behavior that it might as well be considered a bug. Will look to address this at the same time.

@danthorpe
Copy link
Owner

Okay, made a quick start at this on my way to work this morning. See #80.

@aranasaurus
Copy link
Contributor Author

Awesome, looking forward to it!

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