NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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)
        modChecklist.name = String(self.origChecklist.wrappedName + "-1")

The result isn't what I expected...it 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>()

        modChecklist.name = String(self.origChecklist.wrappedName + "-1")

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

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

          newTestGroup.hasActionItems = newActionItems as NSSet
          newTestGroups.insert(newTestGroup)
        }
        modChecklist.hasTestGroups = newTestGroups as NSSet

          try? self.moc.save()

   

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 Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.