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

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

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

Core DataはAppleのOS X and iOSのためのオブジェクトモデリングと持続性を持ったフレームワークです。Xcodeはエンティティー、属性そして関係性を特定するためのオブジェクトモデルの編集機能を提供します。

Q&A

0回答

477閲覧

SwiftUIとCoreData使用中にFatal error: Environment<NSManagedObjectContext> has no key-pathエラーが発生

maron1220

総合スコア18

Core Data

Core DataはAppleのOS X and iOSのためのオブジェクトモデリングと持続性を持ったフレームワークです。Xcodeはエンティティー、属性そして関係性を特定するためのオブジェクトモデルの編集機能を提供します。

0グッド

0クリップ

投稿2020/06/23 12:57

SwiftUIにてCoreDataを使用しています。
なんの問題もなく使用できていたのですが、突然タイトルに有るエラーが発生しました。
FetchRequestでKeyPathは指定しています。
エラーはAppDelegateにて発生しています。

可能性として考えられる原因をご教授いただければと思います。
よろしくお願いいたします。

swift

1import UIKit 2import CoreData 3 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { //ここにエラーが引いてあります 7 8 var window: UIWindow? 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 11 12 13 return true 14 } 15 16 17 18 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 19 20 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 21 } 22 23 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 24 application:didFinishLaunchingWithOptions. 25 26 } 27 28 29 30 lazy var persistentContainer: NSPersistentContainer = { 31 32 let container = NSPersistentContainer(name: "coredata") 33 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 34 if let error = error as NSError? { 35 36 37 38 fatalError("Unresolved error (error), (error.userInfo)") 39 } 40 }) 41 return container 42 }() 43 44 45 46 func saveContext () { 47 let context = persistentContainer.viewContext 48 if context.hasChanges { 49 do { 50 try context.save() 51 } catch { 52 function in a shipping application, although it may be useful during development. 53 let nserror = error as NSError 54 fatalError("Unresolved error (nserror), (nserror.userInfo)") 55 } 56 } 57 } 58 59 60 61} 62 63

アプリ操作上、実際にエラーが出るのは以下のViewです。

swift

1import SwiftUI 2import CoreData 3 4struct BtNameView:View { 5 6 @State var nameOfBt:String 7 8 var body: some View{ 9 HStack{ 10 Text("(nameOfBt)").frame(width: UIScreen.screenWidth/5) 11 Divider() 12 } 13 } 14} 15 16 17struct DayDetailView: View { 18 19 @Environment(.managedObjectContext) var managedObjectContext 20 21 @FetchRequest( 22 entity: BtList.entity(), 23 sortDescriptors: [NSSortDescriptor(keyPath:\BtList.saveDate,ascending:true)], 24 predicate: NSPredicate(format:"isComplete == %@ " ,NSNumber(value:false)) 25 ) 26 var DayDetailLists:FetchedResults<BtList> 27 28 @Binding var doneBtDate:String 29 30 var dateFormatter:DateFormatter{ 31 let formatter = DateFormatter() 32 formatter.setLocalizedDateFormatFromTemplate("yMMMMdEEEE") 33 34 return formatter 35 } 36 37 var saveDateFormatter:DateFormatter{ 38 let saveFormatter = DateFormatter() 39 saveFormatter.setLocalizedDateFormatFromTemplate("yMMMMd") 40 41 return saveFormatter 42 } 43 44 @State var btDictionary:[String:[String]] = [ 45 "BUN":[], 46 // 一部略 47 ] 48 49 50 @State var btListBool = false 51 @State var showBtListButtonBool = true 52 53 @State var photoImage:Data = UIImage(imageLiteralResourceName: "grey-icon").pngData()! 54 @State var photoTitle:String = "" 55 56 var imageWidth = UIScreen.main.bounds.width*4/10 57 var imageheight = UIScreen.main.bounds.width*4/10*4/3 58 59 @State var zoomAppear = false 60 61 62 var body: some View { 63 64 VStack{ 65 padding() 66 Text("(self.doneBtDate)").font(.caption).foregroundColor(.gGreen) 67 Divider() 68// Text("画像一覧").font(.title) 69 ScrollView(.horizontal,showsIndicators: false){ 70 HStack{ 71 ForEach(DayDetailLists){photolist in 72 if (self.dateFormatter.string(from:photolist.saveDate ?? Date()) == self.doneBtDate && photolist.imageData != nil){ 73 ZStack{ 74 75 Image(uiImage:UIImage(data:photolist.imageData ?? self.photoImage)!) 76 .resizable() 77 .frame(width:self.imageWidth,height:self.imageheight ) 78 .cornerRadius(6) 79 .onTapGesture { 80 self.photoImage = photolist.imageData ?? self.photoImage 81 self.photoTitle = photolist.phototitle ?? "" 82 self.zoomAppear.toggle() 83 84 } 85 .sheet(isPresented: self.$zoomAppear){ PhotoZoomView(zoomPhotoImage:self.$photoImage,zoomPhotoTitle: self.$photoTitle) 86 } 87 88 Text("(photolist.phototitle ?? "")").foregroundColor(.white) 89 90 }//ZStack 91 }//if 92 }//ForEach 93 }//HStack 94 }//ScrolView 95 96 if(self.btListBool == false && self.showBtListButtonBool == false){ 97 Divider() 98 Button(action:{ 99 self.btListBool = true 100 }){ 101 Text("血液検査項目を開く") 102 } 103 Spacer() 104 } 105 106 107 List{ 108 Section(header:Text("メモ")){ 109 ForEach(DayDetailLists){messagelist in 110 if(self.dateFormatter.string(from:messagelist.saveDate ?? Date()) == self.doneBtDate && messagelist.message != nil){ 111 Text("(messagelist.message ?? "error")") 112 } 113 } 114 }//Section 115 }//List 116 117 118 // Divider() 119 120 121 122 if(self.showBtListButtonBool){ 123 Divider() 124 Button(action:{ 125 for list in self.DayDetailLists{ 126 print("(self.dateFormatter.string(from:list.saveDate ?? Date()))") 127 print("(self.doneBtDate)") 128 if self.dateFormatter.string(from:list.saveDate ?? Date()) == self.doneBtDate{ 129 print("date success") 130 if let bun = list.bun{ 131 self.btDictionary["BUN"]?.append("(bun)") 132 }else{ 133 print("bun is Empty") 134 } 135 //一部略 136 137 138 self.btListBool = true 139 self.showBtListButtonBool = false 140 }//if 141 else{ 142 print("date error") 143 } 144 }//for in 145 }){ 146 Text("血液検査結果を見る") 147 } 148 Spacer() 149 }//if 150 151 152 153 if(self.btListBool){ 154 VStack{ 155 Divider() 156 Button(action:{ 157 self.btListBool = false 158 }){Text("血液検査項目を閉じる")} 159 Form{ 160 161 Group{ 162 163 if self.btDictionary["BUN"]?.isEmpty ?? false{} 164 else{ 165 BtDayListView(btDayListDictionary: self.btDictionary["BUN"] ?? ["error"], btDayListSection: "BUN", btDayListUnits: BtListView().kidneyUnits["BUN"] ?? "error") 166 }//else 167 168 //一部略 169 170 }//Group 171 172 }//form 173 174 175 176 }//VStack 177 178 }//if 179 180 }//VStack 181 }//body 182}//View 183 184struct BtDayListView:View{ 185 186 //略 187} 188 189 190 191 192コード

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問