Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

ios - Saving CoreData to-many relationships in Swift

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...