UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI & CloudKit - CoreData Many to Many Relationships

Forums > SwiftUI

Hi everyone, I am new to Swift UI and working as well for the first time with CoreData and CloudKit. A lot of my questions could already be answered by existing tutorials or similiar already answered questions. But I couldnt find a great resource for Many to Many Relationships in CoreData.

In my project the user can save markers/timestamps by pushing a button, which triggers a function saving the data in CoreData. When the recording, during the user can save timestamps, is finished by a button press a function saves the recording to CoreData. My aim is that the recording saves and includes all created and saved markers/timestamps, so the user can click in i his history on a Project/Recording and gets all markers/timestamps for this recodring.

At that point I can save and create a marker and recording but not link them to each other.

How can I do it the easiest way - with a Many to Many Relationship? How do Many to Many Relationships work in my code?

I would be really thankfull for help and ideas!

  • Robert

PS: On the phtos you can see that I already tried to create Relationships, but I can't create them with a function...

Screenshots

2      

In your case this is a one to many relationship. One Recording can have many UsedMarker but one UsedMarker can only have one Recording. Unless I completely misunderstand your requirement. What you do is, create your UsedMarker entity with all data and add it to the NSSet via the addToUsedMarker function in your Recording. That's it. Core Data does the rest under the hood for you.

3      

Thanks for your answer! How can I use the automatically generated ADD function correctly? I have a video of the app, which may prevent misunderstandings. It is important that if I delete a marker in a saved recording it will not be deleted as such (so the Marker Button can be used during other recordings) and vice versa. https://youtu.be/MXmUI6kTCpo

2      

You need a reference to your Recording in your View/ViewController. Something like var recording: Recording. When you add your UsedMarker in your function replace //ADD to Recording Object with recording.addToUsedMarker(usedMarker: useMarker).

3      

Thanks - hopefully that helps :) But now my Tabbar misses the argument for Recording ("Missing argument for parameter 'recording' in call"). Whats the argument?

2      

That's what I said above. You need a reference to the Recording you want to save the UsedMarkers for. Either you create it in the view itself or you create it beforehand and transfer it in the initializer YourView(recording: YourRecordingInstance).

If your complete new to programming and don't know what I'm talking about then I would recommend you do https://www.hackingwithswift.com/quick-start/swiftui Paul's great tutorials. There is also a CoreData section in this course: https://www.hackingwithswift.com/quick-start/swiftui/introduction-to-using-core-data-with-swiftui

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.