YapDatabaseExtensions icon indicating copy to clipboard operation
YapDatabaseExtensions copied to clipboard

Separation of metadata from object data at save time?

Open aranasaurus opened this issue 9 years ago • 9 comments

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?

aranasaurus avatar Jan 26 '16 22:01 aranasaurus

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.

danthorpe avatar Jan 26 '16 22:01 danthorpe

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!

aranasaurus avatar Jan 27 '16 18:01 aranasaurus

Yes. That's what it boils down to.

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

danthorpe avatar Jan 27 '16 18:01 danthorpe

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.

aranasaurus avatar Jan 27 '16 19:01 aranasaurus

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

danthorpe avatar Jan 27 '16 19:01 danthorpe

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).

aranasaurus avatar Jan 27 '16 21:01 aranasaurus

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 avatar Jan 27 '16 21:01 danthorpe

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

danthorpe avatar Jan 28 '16 09:01 danthorpe

Awesome, looking forward to it!

aranasaurus avatar Jan 28 '16 18:01 aranasaurus