我有两个表类别和子类别。
类别与SubCategory有很多关系。 SubCategory与SubCategory有很多关系。 (SubCategory可能有子类别)
数据库模型:
Json回复
{
"response": {
"status": true,
"message": "Category listed",
"code": 200
},
"data": [
{
"description": "Memory Questions",
"id": "QT05",
"title": "Memory",
"subcategory": [
{
"title": "Patterns",
"id": "QT05P",
"subcategory": [
{
"title": "Squares",
"id": "QT05PS",
"demo": {
"type": "image",
"value": "Memory/images/demo_QT05PS.png"
}
},
{
"title": "Circles",
"id": "QT05PC",
"demo": {
"type": "image",
"value": "Memory/images/demo_QT05PC.png"
}
},
{
"title": "Triangles",
"id": "QT05PT",
"demo": {
"type": "image",
"value": "Memory/images/demo_QT05PT.png"
}
},
{
"title": "Rectangles",
"id": "QT05PR",
"demo": {
"type": "image",
"value": "Memory/images/demo_QT05PR.png"
}
}
],
"demo": {
"type": "image",
"value": "Memory/images/demo_QT05P.png"
}
}
]
}
]
}
子类别模型类(CoreData)
extension SubCategory{
func addSubCategory(subcategory:SubCategory) {
var sub = self.mutableSetValueForKey("subcategories")
sub.addObject(subcategory)
}
func setSubCategories(subCategories:NSArray){
var sub = self.mutableSetValueForKey("subcategories")
for subcategory in subCategories {
sub.addObject(subcategory)
}
}
}
映射JSON
func mapCategory(dict:NSDictionary?) -> LUBaseResponse? {
if (dict == nil) {
return nil
}
var response = self.mapStatus(dict!)
if (response != nil){
if (response!.status == 1){
let data:NSArray = dict!["data"] as NSArray
var categoryArray = NSMutableArray()
for categoryDict in data {
if (self.fetchCategory(categoryDict["id"] as? String) == nil){
var category = NSEntityDescription.insertNewObjectForEntityForName("Category", inManagedObjectContext: self.managedObjectContext!) as? Category
category?.categoryDescription = categoryDict["description"] as String
category?.categoryId = categoryDict["id"] as String
category?.categoryTitle = categoryDict["title"] as String
var subcategory = self.mapSubcategory(categoryDict["subcategory"] as? NSArray,category: category!)
if (subcategory != nil && subcategory?.count > 0){
category?.setSubCategories(subcategory!)
}
categoryArray.addObject(category!)
}
LUCoreData.sharedInstance.saveContext()
}
response?.data = categoryArray
}
}
return response
}
//Issue in this method
private func mapSubcategory(array:NSArray?,category:Category) -> NSMutableArray? {
var subcategoriesObjects = NSMutableArray()
if (array?.count > 0){
var subcategories = array!
NSEntityDescription.insertNewObjectForEntityForName("SubCategory", inManagedObjectContext: self.managedObjectContext!) as? SubCategory
for subcategoryDict in subcategories {
var title = subcategoryDict["title"]
if (self.fetchSubCategory(subcategoryDict["id"] as? String) == nil) {
var subcategory = NSEntityDescription.insertNewObjectForEntityForName("SubCategory", inManagedObjectContext: self.managedObjectContext!) as? SubCategory
subcategory?.title = subcategoryDict["title"] as String
subcategory?.id = subcategoryDict["id"] as String
subcategory?.categoryOf = category
var arraySubCategory = subcategoryDict.objectForKey("subcategory") as? NSArray
if (arraySubCategory?.count > 0){
var nestedSubCategory = self.mapSubcategory(arraySubCategory,category: category)
if (nestedSubCategory != nil){
var sub = nestedSubCategory!.objectAtIndex(0) as SubCategory
println("Sub is \(sub.subcategories)") //Returns Empty
subcategory!.setSubCategories(nestedSubCategory!)
//Issue Here
println("Sub is \(sub.subcategories)")
}
}
subcategoriesObjects.addObject(subcategory!)
}
LUCoreData.sharedInstance.saveContext()
}
}
return subcategoriesObjects
}
问题:
在JSON Response中,你可以看到Memory(Category)有子类别" Patterns",它有嵌套的SubCategories" Square"," Circles"等
这个循环在" mapSubcategory"中执行。功能
在这一行"子类别之后!.setSubCategories(nestedSubCategory!)"
在上面的图像中,您可以看到当嵌套子类别(" Squares"没有其他子类别)添加到父子类别时,父子类别(模式)被添加到Square作为子类别。这导致递归(Parent - > Child - > Parent - > Child ....)。我不确定如何以递归方式将父母添加到孩子身上。
答案 0 :(得分:0)
我已经通过从子类别表中删除反向关系来解决这个问题。