|
Hi all, I have some more fun ObservableObject issues that I'm trying to work around, and I feel like I'm very close to an answer. Basically, I have an app that tracks some plants. I have a class called RealmSyncModel for these plants which first accesses an external MongoDB database to populate the plants array for that user. These plants are retrieved through another Plant class, which has all the info required to initiate and structure the individual objects in the array. For each plant, a user can also add Reminders. This is handled through a separate ObservableObject class called RemindersArray, which has a Published property that references a RemindersClassModel. I was able to get it to the point where users can add multiple unique reminders, and each reminder could be edited/deleted. I was able to get some help on that part through this previous thread here. Now my issue is: When a reminder is added, it adds that reminder for every single plant. What should happen, is that reminders should be unique for every individual plant object. For example, if you add a reminder to water your Cilantro, you shouldn't see that same reminder if you go back and choose your Tomato plant. I feel like I've hit a wall trying to think through how to make this happen and I'm spinning in circles. Part of me thinks it involves a function tied to the RealmSyncModel/Plant classes when a Reminders is called. Here's some example code, I can provide more context if needed! Plants
Reminders Section, which contains a list of reminders and calls plantdata for a specific chosen plant
Reminders Models
|
|
hi, first, let me say that i have zero experience and zero knowledge of MongoDB. so whatever follows may or not make any sense. bottom line: each Plant has to maintain a connection to its Reminders. i don't see such a connection in your code above. if you were using CoreData, there would be a one-to-many relationship from Plant to Reminder. i think that if you were using CloudKit directly (not a comfortable subject for me right now), each Reminder might contain the id of the Plant it belongs to. even if Plant were a simple class or struct, one of its variables would be a Set or Array of Reminders associated with the Plant. i'd not get caught up in whether something should be passed as an @Environment variable or not, for the moment. i would think more directly that you "pass a Plant" to a PlantReminders view; that View accesses the Plant's Reminders to form a List. if you then navigate from that List using a NavigationLink to edit a specific Reminder, it would be natural to pass along the Reminder being displayed to be edited. tracking any edit of a Reminder back to a specific Plant that owns that Reminder might follow the path referenced in your previous post. if you have some code out on GitHub, some folks might take a look at it; otherwise, we may need to see more code here. (and for naming, i'd suggest renaming hope that helps, DMG |
|
Hey @delawaremathguy - thanks again for taking the time to take a look here, I seriously appreciate it. You're definitely right in that I'm scratching my head in figuring out the best way to pass the Plant ID/connection onto the Reminders Array. As it stands, the Plant class is tied strictly to MongoDB Realm, while the Reminder class (which I'll definitely rename, good point) is tied to creating a new local reminder. I guess it's worth rethinking some basic questions from an architecture and setup standpoint, which I'm hoping you can help me with:
For the third point, would it be something as simple as including the Reminder tie-in with the Plant class?
I also am not sure how to tie in the Plant object within the Reminder class, is there some initialization involved with it as well? |
|
hi, Number 3, i think! it appears MongoDB Realm easily supports a to-many relationship. so your
and now, you would define a new class that's as much as i can help on this, i think. MongoDB and Core Data both persist data and give you cloud support for working with multiple devices. hope that's useful for you, DMG |
|
@delawaremathguy, thanks so much as always for taking the time to look into my question! I'm super close to figuring this out - your hunches are correct in that it involves a mix of linking to another Realm class object, as well as using LinkingObjects(). I found a simple example here that I'm trying to work off of: https://github.com/realm/realm-cocoa/blob/master/examples/ios/swift/ListSwiftUI/Models/Ingredient.swift Will keep you posted as soon as I have it working flawlessly. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.