Coredata toMany关系映射问题 - Swift
Posted
技术标签:
【中文标题】Coredata toMany关系映射问题 - Swift【英文标题】:Coredata toMany relationship mapping issue - Swift 【发布时间】:2015-03-11 11:57:21 【问题描述】:我有两个表 Category 和 Sub Category。
Category 与 SubCategory 有 toMany 关系。 SubCategory 与 SubCategory 有 toMany 关系。 (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”函数中执行
在这一行之后“subcategory!.setSubCategories(nestedSubCategory!)”
在上图中,您可以看到当嵌套子类别(“Squares”没有进一步的子类别)添加到父子类别时,父子类别(模式)作为子类别添加到 Square。这会导致递归(父 -> 子 -> 父 -> 子 ....)。我不确定它是如何递归地将父级添加到子级的。
【问题讨论】:
【参考方案1】:我已通过从子类别表中删除反向关系来解决此问题。
【讨论】:
以上是关于Coredata toMany关系映射问题 - Swift的主要内容,如果未能解决你的问题,请参考以下文章
使用“To Many”关系条目的“.allObjects”时,SwiftUI + Core Data 崩溃
在CoreData的NSOrderedSet中的特定位置插入一个对象?