Reputation: 655
Hi so let's assume that I have an API that gets the category and its ID
let category = ["Armchairs", "Sofas", "Office", "Chairs", "Tables", "Lamps", "Spare Parts"]
let categoryId = [24,23,25,26,27,28,42]
and here's a sample sub category of Armchair with id 24 (Query on category table and get all sub category with parent_id = 24)
let subCat = ["New","Hot", "Used"]
let subCatId = [89,90,91]
so for item #1
id:100 , name:Armchair Item, categ_id:89
how can I create the coredata model for this that when I query its parent category which is (24) it will find all the items in its subcategory?
here's my coredata
Additional Question: Is my coredata structure right? Should I put a sub_categ_id field?
Upvotes: 1
Views: 46
Reputation: 1876
You need to have relations built first. Category and SubCategory. to my viewpoint its Many to Many. and you need to have ParentId in Sub Category as well.
So when you query ex: getAllItemsByParentId:24
all the New,Old,Used items will be load.
You can create table like this.
Updated :
So i have relationship built like this.
and you can see mainId
in Category table as well. when you insert item to category table mainId also will be inserted. so you only need to query the id id only. ex: where all the objects has mainId = 24
. i hope you get the idea.
Upvotes: 1