对象之间的核心数据关系 [英] Core Data Relation between Objects

查看:144
本文介绍了对象之间的核心数据关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于如何在Core Data中创建2个NSObject之间的关系的问题。

i have a question about how create a relation between 2 NSObjects in Core Data.

首先我有这个CoreData结构。

First i have this CoreData Structure.

是文件和表单之间的关系。表单可以有多个文件,但一个文件只能有一个表单。

There is a Relationship between files and forms. A form could have multiple files, but one file could only have one form.

我已经创建了NSObjects只有Swift(所以没有桥接头) - 现在有2核心数据NSObject类:

I have created the NSObjects only with Swift (so no Bridging Header) - and have now 2 Core Data NSObject Classes:

class Forms: NSManagedObject {

@NSManaged var uuid: String
@NSManaged var damagenumber: String
@NSManaged var internalnumber: String
@NSManaged var date: NSDate
@NSManaged var name: String
@NSManaged var street: String
@NSManaged var postalcode: String
@NSManaged var city: String
@NSManaged var country: String
@NSManaged var templateJSON: String
@NSManaged var formname: String
@NSManaged var files: NSSet

}

class Files: NSManagedObject {

@NSManaged var url: String
@NSManaged var forms: Forms

}

我的文件在这里作为NSSet。

My files are here as a NSSet.

,我想例如添加一个具有5个文件的表单。所以startet有这样的:

So, i would like for example add a form with 5 files. So startet with something like this:

var request = NSFetchRequest(entityName: "Forms")
request.returnsObjectsAsFaults = false;

let newFormObject = NSEntityDescription.insertNewObjectForEntityForName("Forms", inManagedObjectContext: context) as Forms

newFormObject.setValue(uuid, forKey: "uuid")
......

//插入文件

 for index in 0...4 {

     var request = NSFetchRequest(entityName: "Files")
     request.returnsObjectsAsFaults = false;

     let newFileObject = NSEntityDescription.insertNewObjectForEntityForName("Files", inManagedObjectContext: context) as Files
     newFileObject.setValue(arrFiles[index], forKey: "url")

}

Ok一切正常。但是:我如何连接这2个对象?如果u使用Obj-C NSObjects而不是Swift我有几个方法添加文件到窗体。

Ok everything works fine. BUT: How do i connect this 2 Objects? If u use Obj-C NSObjects instead of Swift i have a few Methods to add a File to a Form. But how can i do this in Swift only?

如果我有一个关系 - 如何从某个表单中读取所有文件对象?

And if i have a relation - how do i read all File Objects from a certain Form?

编辑:

我现在改变了行:

@NSManaged var files: NSSet

/ p>

to

@NSManaged var files: [Files]

并在循环中使用:

newFormObject.files.append(newFileObject)

这会带来以下错误:

***错误:此进程已调用NSArray-taking方法,如initWithArray :,并在一个NSSet对象中传递。

*** ERROR: this process has called an NSArray-taking method, such as initWithArray:, and passed in an NSSet object. This is being worked-around for now, but will soon cause you grief.

推荐答案

减少您的代码:仅当需要检索对象时设置NSFetchRequests。因此,每次创建新对象时,不需要以下行:

Reduce your code: Set NSFetchRequests only when you need to retrieve objects. Thus, you don't need the following lines every time you create new objects:

//Forms
var request = NSFetchRequest(entityName: "Forms")
request.returnsObjectsAsFaults = false;

//Files
var request = NSFetchRequest(entityName: "Files")
request.returnsObjectsAsFaults = false;

一致性:您已设置NSManagedObject子类。所以使用它们,不要使用键值编码。替换:

Be consistent: You have set NSManagedObject subclasses. So use them and don't use Key Value Coding. Replace:

newFormObject.setValue(uuid, forKey: "uuid")

与:

newFormObject.uuid = uuid

问题:

如何连接此2个对象?

一行代码应该足够你的循环:

A single line of code should be enough at the end of your loop:

newFileObject.forms = newFormObject // where forms is the name of your relationship

问题:


如何从某个表单中读取所有文件对象

how do i read all File Objects from a certain Form

当您需要检索 Files对象表单,你可以执行一个NSFetchRequest(允许你设置谓词,排序等等),或者你可以简单地得到一个NSSet的所有相关对象,如下所示:

When you need to retrieve Files objects of a certain Form, you can perform a NSFetchRequest (that allows you to set predicates, sorts and much more) or you can simply get a NSSet of all your related objects like this:

    //Get all Files objects (of a to-many relationship) related to a single Form object
    let filesSet = myForm.files as NSSet //where myForm is a Form managedObject
    let filesArray = filesSet.allObjects as Array

    for obj in filesArray as [Files] {
        println(obj.url)
    }

这篇关于对象之间的核心数据关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆