CoreData如何向与实体相关的NSSet添加项目 [英] CoreData how to add items to a NSSet which is related to an entity
问题描述
在我的项目中,我创建了两个实体: Container
和 Items
.两者之间存在一对多的关系.在 Container
中,我将关系项目称为关联项目",并将其设置为"To Many".因为我想在每个容器中放很多东西.在 Items
中,我调用了关系容器,并将其设置为"To One".因为每一项必须与一个且仅一个容器相关联.
In my project I created two entities: Container
and Items
. There's a one to many relationship between the two. In Container
I called the relationship item and I set it to "To Many" because I want to have many items in each container. In Items
I called the relationship container and I set it to "To One" because each item must be associated to one and only one container.
此外,在我的项目中,我有一个表格视图,可以在其中添加新的容器,当我按一个单元格时,我可以看到它的项目.创建容器的代码很好.当我尝试创建项目时就会出现问题:它们创建成功,但是出现在每个容器中.
This aside, in my project I have a tableview where I can add new containers and when I press on a cell I can see its items. The code to create the containers is fine. The problem comes when I try to create the items: they are created successfully but they appear in each container.
在以下部分中,我将发布对象,如何保存容器以及如何保存项目:
In the following part I'll post the objects, how I save containers and how I save items:
- 对象:
extension Container {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Container> {
return NSFetchRequest<Container>(entityName: "Container")
}
@NSManaged public var name: String?
@NSManaged public var index: Int64
@NSManaged public var items: NSSet?
}
extension Items {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Items> {
return NSFetchRequest<Items>(entityName: "Items")
}
@NSManaged public var text: String?
@NSManaged public var image: UIImage?
@NSManaged public var index: Int64
@NSManaged public var container: Container?
}
- 创建容器:
let containerEntity = NSEntityDescription.entity(forEntityName: "Container", in: managedContext)!
let items: NSSet = []
let container = NSManagedObject(entity: containerEntity, insertInto: managedContext) as! Container
///Count is just an int variable
container.setValue("Name", forKeyPath: "name")
container.setValue(count, forKeyPath: "index")
container.setValue(items, forKey: "items")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
- 创建项目:
let itemEntity = NSEntityDescription.entity(forEntityName: "Items", in: managedContext)!
let items = NSManagedObject(entity: itemEntity, insertInto: managedContext) as! Items
items.setValue("item", forKeyPath: "text")
items.setValue(count, forKeyPath: "index")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
无论如何,我认为与其创建项实体,不如编辑容器实体,向其添加项.为此,我尝试了此操作,但是我觉得我丢失了一些东西,因为这东西崩溃了.
Anyway I think that instead of creating items entities I should edit the container entity adding to it items. To do that I tried this but I feel like I'm missing something because this thing crashes.
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "container")
do {
let test = try managedContext.fetch(fetchRequest)
let objectUpdate = test[containerIndex] as! NSManagedObject
objectUpdate.setValue("item", forKey: "text")
objectUpdate.setValue(count, forKey: "index")
do{
try managedContext.save()
} catch {
print(error)
}
}
catch {
print(error)
}
那么我如何将项目添加到特定的容器中?
So how can I add items to a specific container?
另外,可以将我的两个实体属性称为索引吗?
Plus, is it ok if two of my entities attributes are called index?
推荐答案
如果设置正确,应该有一个 addToItems()
If set up correctly a there should be an addToItems()
anyContainerObject.addToItems(value: Items)
这篇关于CoreData如何向与实体相关的NSSet添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!