質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

0回答

541閲覧

SwiftUIでCoreDataの更新

momiji0210

総合スコア60

0グッド

0クリップ

投稿2021/04/02 10:09

SwiftUIを勉強しており、CoreDataにてデータの保存などを管理したいです。

新規データ追加や、データ削除などはいろいろなソースを見てなんとか実装できたのですが、
既存データの更新についてどのようにすればよいかわかりませんでした。

下記のソースコードのように一覧画面(登録機能含む)と編集画面を分けているのですが
その場合、どのように更新すればよいのでしょうか。

値を次の画面へ渡すまでは実装できたのですが、
この編集した値をどのように更新すればよいか理解できておりません。

ContentView

SwiftUI

1struct ContentView: View { 2 3 @Environment(.managedObjectContext) private var viewContext 4 @FetchRequest( 5 sortDescriptors: [NSSortDescriptor(keyPath: \Item.id, ascending: true)], 6 animation: .default) 7 private var items: FetchedResults<Item> 8 9 var body: some View { 10 List { 11 12 // ~ 略 ~ 13 14 NavigationLink(destination: SecondView(contents: "sample message 2")) { 15 Text("test") 16 } 17 18 // 編集ページへ遷移 19 ForEach(items) { item in 20 NavigationLink(destination: AddView(title: .constant("(item.title ?? "Sample title")"), contents: .constant("(item.contents ?? "Sample contents")"))) { 21 //NavigationLink(destination: AddView(item: item)) { 22 TopRowView( 23 id: item.id, 24 title: "(item.title ?? defaultTitle)", 25 contents: "(item.contents ?? defaultContents)", 26 timesstamp: "(item.timestamp!)" 27 ) 28 } 29 } 30 .onDelete(perform: deleteItems) 31 .onMove(perform: moveItems) 32 33 } 34 } 35 36 // ~ 略 ~ 37 38 // 追加 39 private func addItem() { 40 41 withAnimation { 42 43 let random = Int.random(in: 100..<500); 44 let i = random; 45 46 let newItem = Item(context: viewContext) 47 newItem.timestamp = Date() 48 newItem.id = Int16(i+1+random) 49 newItem.title = "aaaa " + String(i) 50 newItem.contents = "bbbb " + String(i) 51 52 do { 53 try viewContext.save() 54 } catch { 55 // Replace this implementation with code to handle the error appropriately. 56 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 57 let nsError = error as NSError 58 fatalError("Unresolved error (nsError), (nsError.userInfo)") 59 } 60 } 61 62 } 63} 64 65 66struct AddView: View { 67 68 @Binding var title : String 69 @Binding var contents : String 70 71 @State private var textTitle: String = "" 72 @State private var textContents: String = "" 73 74 var body: some View { 75 76 VStack { 77 78 // タイトル 79 TextField("Title", text: $title) 80 .autocapitalization(.none) // 自動大文字機能を切る 81 .textFieldStyle(RoundedBorderTextFieldStyle()) // 入力域のまわりを枠で囲む 82 .padding() // 余白を追加 83 .onAppear { 84 // 初期値を代入 85 //self.title = self.item.title 86 } 87 } 88 } 89}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問