NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: Having trouble copying a Core Data object and its relationships

Forums > Swift

The app allows a user to create and complete a checklist. All data is saved on the device.

However, if a number of items fail, or the user doesn’t have time to complete the checklist, I need to lock down the checklist to “archive” the work completed. When it’s time to complete or redo the items, the user would choose a new, modified version of the initial checklist that includes just the failed and not executed items.

And that’s where I’m stuck. My thought was to create a copy of the initial, now locked checklist and all of the to-many relationships. I think this may be a “deep copy” and I can’t figure out how to do this. Or is there a better alternative?


I would create new checklist entity, copy the attributes from the "locked" version and do the same for the unfinished items.

So you would loop over the unfinished items, create new entity for each one, fill it with existing values and connect to the new checklist.


Thanks. Starting to go down that path.

But I'm confused based on some intial results...I create the modChecklist and try to set the name to be the same as the locked checklist with an additional string:

let modChecklist = Checklist(context: self.moc) = String(self.origChecklist.wrappedName + "-1")

The result isn't what I appears the original locked checklist is renamed and the new modChecklist keeps the original name. Before I start flowing down with lots and lots and lots of forEachs for all the relationships, what am I missing here?


What's the return of wrappedName and why don't you just use name for your origChecklist?


Name is an optional string, so .wrappedName just reduces the necessity of ?? "unknown name"


Can you share more Core Data code related to this issue? Seems strange that different object would get changed..


Thanks for the time.

Incredibly bizarre...out of frustration I just quit Xcode, restarted the computer, deleted the app from the sim and started clean. The issue went away. But that being said, and putting aside concerns about it popping up again, I'm struggling with the nested relationships...

Checklist<->>TestGroup<->>ActionItem Looking at the sql file, all of the ActionItems are incorrectly being assigned to the last TestGroup. Per one of Paul's training videos, I've created an array of the relationship and because I couldn't figure out a forEach for a NSSet, used that instead (ie to-many relationship from TestGroup<-->>ActionItem is called hasActionItems and the TestGroup class has a casted array called actionItemsArray. The inverse in the ActionItem class is called belongsToTestGroup.)

let modChecklist = Checklist(context: self.moc)
        var newTestGroups = Set<TestGroup>()
        var newActionItems = Set<ActionItem>() = String(self.origChecklist.wrappedName + "-1")

        self.origChecklist.testGroupsArray.forEach{testGroup in
          let newTestGroup = TestGroup(context: self.moc)

          testGroup.actionItemsArray.forEach{actionItem in
            let newActionItem = ActionItem(context: self.moc)

          newTestGroup.hasActionItems = newActionItems as NSSet
        modChecklist.hasTestGroups = newTestGroups as NSSet



As I understand your ActionItems are part of your TestGroups. So shouldn't be the declaration of the var newActionsItems inside the scope of the forEach? You never empty the newActionsItems and therefore for every TestGroup you just add ongoing ActionsItems. I don't think this is intended.


Hatsushira, That was it!!! I'm still new to Swift, and didn't realize I would have to clear the set.

@nemecek-filip Thanks for explaining the overall process and your time!

Thank you both so much!


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.