ios - Correct way to accessing attributes of a class modeled in core data -


let's recipe object has nsset of 1 or more ingredients, , same relationship modeled in core data.

given recipe, id correct way access ingredients?

in example seems natural use recipe.ingredients, equally use nsfetchrequest ingredient entities nspredicate match recipe.

now let's want ingredients 'collected'. less clear cut me - should use fetch request ingredients predicate restricting recipe , collected state? or loop through recipe.ingredients?

at other end of scale, perhaps need ingredients recipe appear in other recipes. now, fetch request seems more appealing.

what correct general approach? or case case scenario? interested in impact on:

  1. consitancy
  2. readability
  3. performance
  4. robustness (for example, easy make error in fetch request compiler cannot catch).

in example seems natural use recipe.ingredients, equally use nsfetchrequest ingredient entities nspredicate match recipe.

why latter when can former? have recipe, , has set of ingredients, there's no need @ all ingredients , filter out related recipe have.

now let's want ingredients 'collected'. less clear cut me - should use fetch request ingredients predicate restricting recipe , collected state? or loop through recipe.ingredients?

apply predicate recipe's ingredients:

nspredicate *iscollected = [nspredicate predicatewithformat:@"collected == yes"]; nsset *collectedingredients = [recipe.ingredients filteredsetusingpredicate:iscollected]; 

at other end of scale, perhaps need ingredients recipe appear in other recipes. now, fetch request seems more appealing.

again, using fetch request here seems wasteful because have easy access set of ingredients in final result, , that's potentially smaller set set of ingredients. use same approach above, change predicate test recipes associated each ingredient. like:

nspredicate *p = [nspredicate predicatewithformat:@"recipes > 1"]; nsset *i = [recipe.ingredients filteredsetusingpredicate:p]; 

what correct general approach?

fetch requests way search through instances of given entity. you're going start fetch request objects work with. when objects want somehow related object have can (and should) use relationships want.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -