Post

Replies

Boosts

Views

Activity

Why do I get an error when deleting data from SwiftUI List and Realm?
Hi all,I know this is may not be the right forum to post questions about Realm but my issue has to do with the nature of how SwiftUI works.Has anyone been able to successfully integrate Realm with SwiftUI, especially deleting records/rows from a SwiftUI List? I have tried a few different things but no matter what I do I get the same error. After reading some related threads I found out that other people have the same issue but I refuse to think that there is no current solution to integrate Realm with SwiftUI.The following code successfully presents all of the items from Realm in a SwiftUI List, I can create new ones and they show up in the List as expected, my issues is when I try to delete records from the List by either manually pressing a button or by left-swiping to delete the selected row, I get an Index is out of bounds error.Here is my code.Realm Model:class Dog: Object { @objc dynamic var name = "" @objc dynamic var age = 0 @objc dynamic var createdAt = NSDate() @objc dynamic var userID = UUID().uuidString override static func primaryKey() -> String? { return "userID" } }SwiftUI Code:class BindableResults: ObservableObject where Element: RealmSwift.RealmCollectionValue { var results: Results private var token: NotificationToken! init(results: Results) { self.results = results lateInit() } func lateInit() { token = results.observe { [weak self] _ in self?.objectWillChange.send() } } deinit { token.invalidate() } } struct DogRow: View { var dog = Dog() var body: some View { HStack { Text(dog.name) Text("\(dog.age)") } } } struct ContentView : View { @ObservedObject var dogs = BindableResults(results: try! Realm().objects(Dog.self)) var body: some View { VStack{ List{ ForEach(dogs.results, id: \.name) { dog in DogRow(dog: dog) }.onDelete(perform: deleteRow ) } Button(action: { try! realm.write { realm.delete(self.dogs.results[0]) } }){ Text("Delete User") } } } private func deleteRow(with indexSet: IndexSet){ indexSet.forEach ({ index in try! realm.write { realm.delete(self.dogs.results[index]) } }) } }ERRORTerminating app due to uncaught exception ‘RLMException’, reason: ‘Index 23 is out of bounds (must be less than 23).’Of course, the 23 changes depending on how many items are in the Realm database, in this case, I had 24 records when I swiped and tapped the delete button.
4
0
3.7k
Apr ’20
Rename or find where a custom Color Asset has been used in Xcode
Now with Dark Mode I have been creating color assets to manage dark mode colors, but sometimes I think of a more semantic name for a certain color after I have used it in multiple places and I was wondering if there is a way to find where a color has been used even if I have to go and manually changed them, I'm not really looking for an automatic solution.Is there a way to rename or find all of the instances where a custom color asset has been used in Xcode?Thanks!
6
0
6.2k
Oct ’19