开发者

Core Data modeling issues (how to update attribute based on a relation's relation?)

开发者 https://www.devze.com 2023-02-13 04:46 出处:网络
I have an entity (Order) that has 开发者_如何学编程a to-many relationship with an entity Item, which has a to-many relationship with an entity Note. If the price changes for a Note, or a Note is added

I have an entity (Order) that has 开发者_如何学编程a to-many relationship with an entity Item, which has a to-many relationship with an entity Note. If the price changes for a Note, or a Note is added, the 'price' attribute for the associated Order must update.

Right now, my solution is to have all Order objects sign up for NSManagedObjectContextDidChange notifications, and check all the inserted/changed objects to see if any of those objects is one of the Order's Item's Notes. However, this is very inefficient and hacky, and is leading to a few more performance issues that can be optimized away, but I'm starting to realize that my solution is what's faulty, not necessarily the issues.

So, what's the best way to do this?

EDIT: To answer the questions brought up by Rog: I'm looking to propagate the changes to model data, which are observed by view controllers via KVO. The problem I'm noticing is, if the price of a Note related to an Item is adjusted, there's no facility to account for this in Core Data. If I use keyPathsForAffectingPrice on Item, and return "notes", that only accounts for if notes are inserted/deleted, not if the Note price is adjusted.

If this wasn't Core Data, I'd write my own accessor for note price to just say [self.item willChangeValueForKey:@"price"], self.price = x, [self.item didChangeValueForKey:@"price"]; but that isn't possible since I can't do custom accessors in Core Data, right?


We problably need more details about your code to be able to help - i.e. are you looking at ways to propagate changes to your model data or to the UI?

Are you using a fetchedResultsController at the moment?

The way I see it, if your Order contains Items and the Items contain Notes, any updates to your "child" attributes will be effective immediately (provided you have your reverse relationships setup properly).

Then if you're looking at ways to updating your UI accordingly, then we need to know how you are currently fetching and populating your views with your Coredata entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消