简体   繁体   English

在核心数据关系中添加子对象-Swift4

[英]Add child object in Core Data relationship - Swift4

I have two Entitys one called Cupboard one Drawer with a one to many Relationship Cupboard<->>Drawer. 我有两个实体,一个称为“橱柜一个抽屉”,一个与一对多的关系橱柜<->>抽屉。 For Cupboards I use a collectionView and I created a segue to a tableView(Drawer). 对于橱柜,我使用collectionView,并为tableView(Drawer)创建了一个序列。 But I don't know how I add new Drawers to Cupboard when I'm in the DrawerViewController 但是当我在DrawerViewController中时,我不知道如何向橱柜添加新抽屉

I save my Cupboard like this: 我这样保存我的橱柜:

func saveCupboard(name: String, type: String){
    let cupboard = NSEntityDescription.insertNewObject(forEntityName: "Cupboard", into: managedContext) as! Cupboard

    cupboard.name = name
    cupboard.type = type

    saveContext()

}

lazy var managedContext: NSManagedObjectContext = {

    var context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

    context.persistentStoreCoordinator = self.storeCoordinator

    return context
}()

func saveContext() {
    if managedContext.hasChanges {
        do {
            try managedContext.save()
        } catch {
            print(error.localizedDescription)
        }
    }
}

My segue: 我的话题:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showDrawer" {
        let dst = segue.destination as! DrawerViewController

        guard let indexPath = collectionView.indexPathsForSelectedItems else{
            return
        }
        dst.cupboard = ??
    }
}

Maybe someone can help me there. 也许有人可以在那帮我。 Thank you in advance 先感谢您

Edit: 编辑:

 lazy var fetchedResultsCtrl: NSFetchedResultsController<Cupboard> = {
    let request: NSFetchRequest<Cupboard> = Cupboard.fetchRequest()

    let sort = NSSortDescriptor(key: "name", ascending: true)

    request.sortDescriptors = [sort]

    let fetchedCtrl = NSFetchedResultsController(fetchRequest: request, managedObjectContext: appDelegate.coreDataResource.managedContext, sectionNameKeyPath: nil, cacheName: nil)

    fetchedCtrl.delegate = self

    return fetchedCtrl
}()

But I don't know how I add new Drawers to Cupboard when I'm in the DrawerViewController 但是当我在DrawerViewController中时,我不知道如何向橱柜添加新抽屉

Make sure that you have enabled class generation for your managed object model. 确保已为托管对象模型启用了类生成。 Then you have classes Drawer and Cupboard. 然后,您将获得“抽屉”和“橱柜”类。 So you would say: 所以你会说:

let drawer = Drawer(context:context)
drawer.cupboard = // ... the cupboard we already saved  
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showDrawer" {
        let dst = segue.destination as! DrawerViewController

        guard let indexPath = collectionView.indexPathsForSelectedItems else{
            return
        }
        dst.cupboard = cupboards[indexPath.row] as! Cupboard //Pass the cupboard to DrawerViewController.
    }
}

You can pass the cupboard to DrawerViewController like above. 您可以像上面那样将橱柜传递给DrawerViewController。 Then at the time of adding new drawer in DrawerViewController, you have to just set 然后,在DrawerViewController中添加新抽屉时,您只需设置

let newDrawer = Drawer(context:moc) 
newDrawer.cupboard = cupboard

As there is one to many relationships between cupboard and drawer. 由于橱柜和抽屉之间存在一对多的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM