-
Type: Task
-
Resolution: Works as Designed
-
Priority: Trivial - P5
-
None
-
Affects Version/s: None
-
Component/s: None
<!---
Questions: If you have questions about HOW TO USE Realm, ask on
StackOverflow,
or in our Swift Forum or Obj-C Forum.
Feature Request: Just fill in the first two sections below.
Bugs: To help you as fast as possible with an issue please describe your issue
and the steps you have taken to reproduce it in as many details as possible.
-->
Goals
<!--- What do you want to achieve? -->
I wish to display a view of a list of objects along with other objects that are linked via a backlink. The example below uses Owners and Dogs where I am trying to display a list of dogs along with the number of owners for each dog.
It is possible to add dogs into this view as well as attach owners to dogs from the same view.
Expected Results
<!--- What did you expect to happen? -->
As owners are added to dogs, the list should be updated to reflect the number of owners each dog has
Actual Results
<!--- What happened instead?
e.g. the stack trace of a crash
-->
The UI is not updated to reflect the latest data
Steps for others to Reproduce
<!--- What are steps OTHERS can follow to reproduce this issue? -->
Given the sample code below
- Tap "Add dog" button
- Tap on the on newly added dog row
- Tap "Add person" button
- The UI will present no changes.
- Restarting the application will then display the correct number of owners.
Code Sample
<!---
Provide a code sample or test case that highlights the issue.
If relevant, include your model definitions.
For larger code samples, links to external gists/repositories are preferred.
Alternatively share confidentially via mail to help@realm.io.
Full Xcode projects that we can compile ourselves are ideal!
-->
Models:
@objcMembers class Owner: Object, ObjectKeyIdentifiable { dynamic var _id = UUID().uuidString dynamic var name = "Owner" var dogs = RealmSwift.List<Dog>() override static func primaryKey() -> String? { return "_id" } } @objcMembers class Dog: Object, ObjectKeyIdentifiable { dynamic var _id = UUID().uuidString dynamic var name = "Fido" var owners = LinkingObjects(fromType: Owner.self, property: "dogs") override static func primaryKey() -> String? { return "_id" } }
View:
struct ContentView: View { @ObservedResults(Dog.self) var dogs @State var selectedDog: Dog? var body: some View { NavigationView { List { ForEach(dogs) { dog in Text("\(dog.name) has \(dog.owners.count) owners") .onTapGesture { selectedDog = dog } } } .navigationTitle("Dogs") .navigationBarItems( leading: Button("Add owner", action: addOwnerToDog).disabled(selectedDog == nil), trailing: Button(action: { $dogs.append(Dog()) }) { Image(systemName: "plus.app.fill") } ) } } func addOwnerToDog() { let realm = try! Realm() let newOwner = Owner() let dog = realm.object(ofType: Dog.self, forPrimaryKey: selectedDog!._id) try! realm.write { if let dog = dog { newOwner.dogs.append(dog) } realm.add(newOwner) } } }
Version of Realm and Tooling
<!---
In the CONTRIBUTING guidelines, you will find a script,
which will help determining some of these versions.
-->
Realm framework version: 10.7.4
Xcode version: 12.5
iOS/OSX version: 14.5
Dependency manager + version: Swift Package Manager