Re: Query core data store based on a transient calculated value
Re: Query core data store based on a transient calculated value
- Subject: Re: Query core data store based on a transient calculated value
- From: Quincey Morris <email@hidden>
- Date: Fri, 28 Oct 2011 15:45:22 -0700
On Oct 26, 2011, at 14:17 , Darren Wheatley wrote:
> I need to display a subset of those rows in a table view filtered on a
> calculated search criteria, and for each row displayed add a value that I
> calculate in real time but don't store in the entity.
>
> The calculation needs to use a couple of values supplied by the user.
>
> A hypothetical example:
>
> Entity: contains fields "id", "first", and "second"
>
> User inputs: 10 and 20
>
> Search Criteria: only display records where the entity field "id" is a prime
> number between the two supplied numbers. (I need to build some sort of
> complex predicate method here I assume?)
You can use a Core Data fetch for this. Assuming you have an array controller in the nib file to which the table view content is bound, then the array controller has a fetch predicate property you can set. (I think you'll also need "Automatically prepares content" to get it to actually fetch for you.)
Or, you can use a filter predicate. The difference in this case is that all the objects would be fetched and the unwanted ones filtered out at display time. Given the way Core Data works, this isn't as inefficient as it might sound, but fetching just the objects you need is probably a better choice.
> Display: all fields of all records that meet the criteria, along with a
> derived field (not in the the core data entity) that is the sum of the "id"
> field and a random number, so each row in the tableview would contain 4
> fields:
>
> "id", "first", "second", -calculated value-
>
>> From my reading / Googling it seems that a transient property might be the
> way to go, but I can't work out how to do this given that the search
> criteria and the resultant property need to calculate based on user input.
In this case, I think you're better off with a derived property. That is, create your own readonly property whose getter calculates the derived value. Just remember to make this derived property KVO compliant. This means writing a +keyPathsForValuesAffectingCalculatedValue method on the class that has the derived property, and returning a set of keys or key paths of properties that affect the calculation.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden