RealmSwiftを使ってアプリを作成しています。
リレーションのあるデータを削除する際に、下記の実装だとクラッシュしてしまいました。
エラーは下記です。
Thread 1: Exception: "The RLMArray has been invalidated or the object containing it has been deleted."
UnderlayerItemのみの削除であれば削除できました。
どうすれば、クラッシュせずに削除できるでしょうか?
swift
1struct ListView: View { 2 @ObservedObject private var fetcher = Fetcher() 3 @State private var title = "" 4 5 var body: some View { 6 NavigationView { 7 VStack { 8 TextField("タイトルを入力", text: $title) { 9 let item = Item() 10 item.title = self.title 11 let realm = try! Realm() 12 try! realm.write { 13 realm.add(item) 14 } 15 self.title = "" 16 } 17 18 ForEach(self.fetcher.items) { (item: Item) in 19 NavigationLink(destination: DetailView(item: item, id: item.id)) { 20 Text(item.title) 21 } 22 } 23 } 24 } 25 } 26}
swift
1struct DetailView: View { 2 var item: Item 3 var id: String 4 @State private var title = "" 5 @ObservedObject private var fetcher = Fetcher() 6 @Environment(.presentationMode) private var presentationMode: Binding<PresentationMode> 7 8 var body: some View { 9 VStack { 10 TextField("タイトルを入力", text: $title) { 11 let realm = try! Realm() 12 13 if let item = realm.objects(Item.self).filter("id == '(self.id)'").first { 14 try! realm.write() { 15 let underlayerItem = UnderlayerItem() 16 underlayerItem.title = self.title 17 item.underlayerItems.append(underlayerItem) 18 } 19 } 20 self.title = "" 21 } 22 23 ForEach(self.item.underlayerItems) { (underlayerItems: UnderlayerItem) in 24 Text(underlayerItems.title) 25 } 26 27 Button(action: { 28 self.presentationMode.wrappedValue.dismiss() 29 self.fetcher.delete(id: self.id) 30 }) { 31 Text("Delete") 32 } 33 } 34 } 35} 36
swift
1class Fetcher: ObservableObject { 2 var realm = try! Realm() 3 var objectWillChange: ObservableObjectPublisher = .init() 4 private(set) var items: Results<Item> 5 private var notificationTokens: [NotificationToken] = [] 6 7 init() { 8 items = realm.objects(Item.self) 9 10 notificationTokens.append(items.observe { _ in 11 self.objectWillChange.send() 12 }) 13 } 14 15 func delete(id: String) { 16 guard let item = realm.objects(Item.self).filter("id == '(id)'").first else { return } 17 try! realm.write() { 18 realm.delete(item.underlayerItems) 19 realm.delete(item) 20 } 21 } 22}
Swift
1 2class Item: Object, Identifiable { 3 @objc dynamic var id = NSUUID().uuidString 4 @objc dynamic var title = "" 5 @objc dynamic var createdAt = NSDate() 6 7 let underlayerItems: List<UnderlayerItem> = List<UnderlayerItem>() 8 9 override static func primaryKey() -> String? { 10 return "id" 11 } 12} 13 14class UnderlayerItem: Object, Identifiable { 15 @objc dynamic var id = NSUUID().uuidString 16 @objc dynamic var title = "" 17 @objc dynamic var createdAt = NSDate() 18 19 override static func primaryKey() -> String? { 20 return "id" 21 } 22}
回答1件
あなたの回答
tips
プレビュー