The following is the code.
struct DeleteButton: View {
@ObservedRealmObject var animal : AnimalModel
@ObservedRealmObject var animalGroup : AnimalGroup
var body: some View {
Button(action: {
let _ = print(animal)
$animalGroup.animals.remove(at: 0)
}) { Image(systemName: "minus") }
}}
Question 1 : How do i get the index of the item that i want to delete? -> currently (for testing) im just delecting the first item in the list of animalGroup.animal
Question 2 : When deleting this item, the user is updated with the linked object: see below code
class AnimalGroup: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var animals = RealmSwift.List<AnimalModel>()
@Persisted var ownerId : String
}
class AnimalModel: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted(originProperty: "animals") var group: LinkingObjects<AnimalGroup>
@Persisted var animalId: String = "(randomName())"
@Persisted var ownerId : String = ""
@Persisted var status : String = "Alive"
@Persisted var animalType : String = "Sheep"
}
The user is a basic user on MongoB.
The problem is when i delete an animal Item, it get removed from the group, but the actual item is not removed from the DB.
SO basically only the link between the user and the item is removed.
Only the link is deleted in the group, but not the actual item in the Mongo DB