0
votes

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

First i have this CoreData Structure.

Core Data

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

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

}

My files are here as a NSSet.

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")
......

// insert files

 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 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?

Edit:

i changed now the line:

@NSManaged var files: NSSet

to

@NSManaged var files: [Files]

And using in the loop:

newFormObject.files.append(newFileObject)

And this brings me the following error:

*** 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.

1

1 Answers

0
votes

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;

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

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

with:

newFormObject.uuid = uuid

Question:

How do i connect this 2 Objects?

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

Question:

how do i read all File Objects from a certain Form

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)
    }