开发者

CoreData Guidance

开发者 https://www.devze.com 2023-01-23 18:16 出处:网络
So I have this problem I am trying to solve - I wonder if anyone can comment on/help me with the approach. The thing is, I have it partly solved, but with the rest I\'m not quite sure.

So I have this problem I am trying to solve - I wonder if anyone can comment on/help me with the approach. The thing is, I have it partly solved, but with the rest I'm not quite sure.

Here's the deal:

I have a fairly large DB online which I want to load on first start of the App. After that I am only going to load it if new versions exist.

I use an xml parser to parse the data and enter all the data to my data model. The database consists of thousands of products, all described by various attributes.

Anyway, it's easy for me to save thousands of products in a database, then retrieving the data on demand.

I have a problem of how to categorize them and how to save the category data. There is a main category i.e. Hi-Fi which has several subcategories- let's say 'stereo', 'tuner', 'phone' and so on....

How to best save this info, that category a has 15 subcategories and each of these categories in turn has 30 product开发者_如何学编程s while securing performance and keeping process-time at a minimum. I don't want to check all 2000 Products whether I need to show them in a certain table view each time I open a new table view.

Any hints on the apporach are appreciated.


You'll need two entities: Product and Category.

Category has a to-many relationship called subcategories with a target entity of Category. The inverse relationship can be called parentCategory. Category also has a to-many relationship called products. Product would have an inverse relationship called category (or categories if a product can belong to multiple categories)

Now, you can get all the products for a given category by checking its products property. If you want to include all the products in the subcategories, you can do a fetch request with a predicate like this:

[NSPredicate predicateWithFormat:@"category == %@ OR category IN %@", category, category.subcategories];


I think you can solve it by having a Core Data modal consisting of three entities: Product, Category and SubCategory.

Product has a relationship category with destination Category and a relationship subcategory with destination SubCategory.

Category has a to-many relationship products with destination Product and a to-many relationship subcategories with destination SubCategory.

SubCategory has a to-many relationship products with destination Product and a relationship category with destination Category.

When defining these relationships remember to assign the Inverse relationships as well.

Now you get a list of all products belonging to a specific category by just loading the Category in question and accessing the products property. It should also be possible to use NSFetchRequest for Product with a predicate specifying which category you want. Which is best regarding performance and memory requirements I can't say so you just have to test which approach works best.

0

精彩评论

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

关注公众号