21

I have a one-to-many relationship that looks like so,

coredata model

I've set up my model classes in a file to match:

import CoreData
import Foundation

class Board: NSManagedObject {
    @NSManaged var boardColor: String
    @NSManaged var boardCustomBackground: AnyObject?
    @NSManaged var boardID: String
    @NSManaged var boardName: String
    @NSManaged var lists: NSSet
}

class List: NSManagedObject {
    @NSManaged var listID: String
    @NSManaged var listName: String
    @NSManaged var board: Board
}

Because I'm fetching data from multiple JSON endpoints, I have to save my lists seperately from my boards. What I want to do is create/update a list for a board with a matching boardID.

Here's where I am after multiple attempts:

func saveList(boardID: String, listName: String, listID: String) {
    let request = NSFetchRequest(entityName: "Board")
    var error: NSError? = nil
    request.predicate = NSPredicate(format: "boardID like %@", boardID)
    let results: NSArray = context.executeFetchRequest(request, error: &error)
    if results.count > 0 {
        for result in results {
            let board = result as Board
            let list = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: context) as List
            println(" want to save \(listName) in \(board.boardName)")
            board.lists.addListObject(lists)
            list.listName = listName
            list.listID = listID
        }
    }
}

Based on Defining CoreData Relationships in Swift and this, I tried to implement @Keenle's answer for define list objects inside a board:

import Foundation

extension Board {
    func addListObject(value:List) {
        var items = self.mutableSetValueForKey("lists");
        items.addObject(value)
    }

    func removeListObject(value:List) {
        var items = self.mutableSetValueForKey("lists");
        items.removeObject(value)
    }
}

However, I ran into the following error at board.lists.addListObject(lists): 'NSSet' does not have a member named 'addListObject'`

Instead of board.lists.addListObject(lists), I also tried board.lists.listName = listName as implied in this Obj-C example, but that sadly didn't work either.

(Also, The println output is correctly specifying the right board and list.)

Thanks in advance!

Community
  • 1
  • 1
Pirijan
  • 3,430
  • 3
  • 19
  • 29
  • Can you please tell me , after saving how you will fetch the result ? i mean the fetch in one to many relation will be different from normal scenarios?? – ArgaPK Dec 19 '17 at 07:00
  • Can any one please tell me , how we will fetch the records from lists of a specific boardId?? – ArgaPK Dec 19 '17 at 07:26

3 Answers3

33

In a one-to-many relationship, it is easier to set the "to-one" direction of the inverse relationships, in your case just

list.board = board

so that the extension methods are actually not needed here.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • In the case of how I have my List to Board relationship set up, it looks like it's already to-one? see: http://imgur.com/uKspCtt – Pirijan Aug 05 '14 at 12:52
  • 3
    @Pirijan: Yes, I had seen that already in the picture in your question. Bord->List is "to-many", and List->Board is "to-one". Since these are *inverse* relationships of each other, setting one automatically updates the other. - In this case setting the List->Board relationship is a bit easier. – Martin R Aug 05 '14 at 12:55
  • when I try `list.board = board`, the program fails with `Error saving context: Could not merge changes`. The conflict error list shows two boards with the same boardID, even if I've removed the db file prior to the build? – Pirijan Aug 10 '14 at 02:38
  • @MartinR Could you check my answer? I've just found out that you can do like that and it works for me and I think it's a good alternative. – FranMowinckel Jan 30 '16 at 16:24
  • @MartinR, Can you please tell me , after saving how you will fetch the result ? i mean the fetch in one to many relation will be different from normal scenarios?? – ArgaPK Dec 19 '17 at 07:00
6

You should invoke addListObject(...) on board object:

board.addListObject(list) // notice that we pass just one object

Additionaly, if you want to be able to add a set of lists to particular board object, you can enhance you Board class extension with methods that accept set of objects:

func addList(values: NSSet) {
    var items = self.mutableSetValueForKey("lists");
    for value in values {
        items.addObject(value)
    }
}

func removeList(values: NSSet) {
    var items = self.mutableSetValueForKey("lists");
    for value in values {
        items.removeObject(value)
    }
}
Keenle
  • 12,010
  • 3
  • 37
  • 46
  • Hmm, looks like an uncaught exception is raised at the `var items = self.mutableSetValueForKey("lists")`. With the following error: `NSManagedObjects of entity 'Board' do not support -mutableSetValueForKey: for the property 'lists'`. Trying to figure it out now.. – Pirijan Aug 10 '14 at 01:48
  • @Pirijan Did you ever figure out why you were getting the 'do not support -mutableSetValueForKey' error? I'm seeing the same thing and can't get around it. – Dedawn Jun 06 '15 at 21:49
  • 1
    I also was getting "do not support -mutableSetValueForKey" error for my project. I am newbie, so it took me for a while to find a cause. In Model Inspector dialog for relationship field there is a field called "Type". By default it reads "To One". I selected "To Many" option there and the error gone. – Vladimir Ignatov Jun 22 '15 at 11:06
  • @Keenle , Can you please tell me , after saving how you will fetch the result ? i mean the fetch in one to many relation will be different from normal scenarios?? – ArgaPK Dec 19 '17 at 07:01
3

If you define:

@NSManaged var lists: Set<List>

Then you can do:

board.lists.insert(list)
FranMowinckel
  • 4,233
  • 1
  • 30
  • 26
  • Can you please tell me , after saving how you will fetch the result ? i mean the fetch in one to many relation will be different from normal scenarios?? – ArgaPK Dec 19 '17 at 07:01