markohlebar / BIND

Data Binding and MVVM for iOS
MIT License
354 stars 48 forks source link

Binding a view model to a Core Data managed model object #19

Closed davidolesch closed 9 years ago

davidolesch commented 9 years ago

Great job on this library. I've only used it for a week but I could see it being a library I can reuse over and over on different projects.

I've got a question regarding model to view model bindings where the model is a Core Data managed object. The issue I'm seeing happens when the managed objects gets faulted, causing the binding to output a null value. I don't want the view model to change a property to null when its model object gets faulted.

This isn't a defect in the library but I'm wondering if you have an answer for how to correctly handle object faulting with view to view model binding.

markohlebar commented 9 years ago

Hey @davidolesch, thanks! Do you want to stop the propagation of the property which is currently null, or just transform the null to some human readable value?

davidolesch commented 9 years ago

I want null not to propagate because the underlying value on the model isn't null when a value gets faulted in Core Data. My solution is to initialize the view model with the model instead of binding it to the model.

markohlebar commented 9 years ago

Hey @davidolesch, would !~> operator help? This doesn't initialize the values but just waits for updates.

markohlebar commented 9 years ago

@davidolesch I was also thinking about adding a filter function, that would help not propagating stuff for sure.

davidolesch commented 9 years ago

@markohlebar I think that filtering would make binding to Core Data models work better. I'm interested in helping add that feature.

markohlebar commented 9 years ago

@davidolesch great stuff. OK I think the method signature should be along the lines of

typedef BOOL(^BNDBindingFilterBlock)(id object, id value, BNDBinding *context);
-(BNDBinding *)filter:(BNDBindingFilterBlock)filterBlock; 

Tell me what you think, I would be grateful if you implemented this. Cheers!

davidolesch commented 9 years ago

I've now gotten to the bottom of the defect I was seeing. The problem wasn't the binding not being able to access the faulted relationship; instead it was Core Data not being able to access the faulted relationship.

The model object I was binding to was created from a network request on the background thread and so in Core Data it was created in a background context. Later when I do the binding on the main thread Core Data can no longer fill the relationship fault. Core Data doesn't give out any errors when it can't access the relationship so it took a long time for me to figure out that that was the root cause. I fixed the defect by fetching the model object from the main context. The binding works exactly how it should now.

I looked at adding the filter method but it looks like a bigger task than I have time for right now. Thanks for the help @markohlebar.

markohlebar commented 9 years ago

Hey @davidolesch, I took a stab at filter last weekend, it's progressing well but can't promise a release date. Glad you have a workaround though!