ios - 在 Swift UI 中实现核心数据而不崩溃

标签 ios swift core-data crash swiftui

我正在尝试将 CoreData 实现到我的 iOS 应用程序中。我想将对象添加到列表中并保存它们。不幸的是,我的应用程序崩溃并且无法运行。

在我的 SwiftUI 应用程序中实现 CoreData 后,我的应用程序不断崩溃。

这是我的主要代码:

struct Log: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: ExerciseForWorkout.getAllExerciseForWorkout()) var loggedExercise: FetchedResults<ExerciseForWorkout>

    @State private var newLoggedExercise = ""
    @State private var newReps = 0

    var body: some View {
        NavigationView {
            List{
                Section(header: Text("Add Exercise")) {
                    HStack {
                        TextField("Exercise", text: $newLoggedExercise)
                        Stepper("Reps", value: $newReps, in: 0...24)
                        Button(action: {
                            let loggedExerciseNew = ExerciseForWorkout(context: self.managedObjectContext)
                            loggedExerciseNew.name = self.newLoggedExercise
                            loggedExerciseNew.reps = self.newReps
                            loggedExerciseNew.dateCreated = Date()

                            do {
                                try self.managedObjectContext.save()
                            } catch {
                                print("Error while saving !!!")
                                print(error)
                            }

                            self.newLoggedExercise = ""
                        }) {
                            Image(systemName: "plus.circle.fill")
                                .foregroundColor(Color.green)
                                .imageScale(.large)
                        }
                    }
                }
                .font(.headline)

                Section(header: Text("Today's Workout")) {
                    ForEach(self.loggedExercise) { log in
                        HStack {
                            Text(log.name!)
                            Text("Reps : \(log.reps)")
                        }
                    }
                    Text("test")
                }
            }
        }
        .navigationBarTitle(Text("Log"))
        .navigationBarItems(trailing: EditButton())
    }
}

这是我的ExerciseForWorkout 类(class):

import Foundation
import CoreData

public class ExerciseForWorkout: NSManagedObject, Identifiable {
    @NSManaged public var reps: Int
    @NSManaged public var dateCreated: Date?
    @NSManaged public var name: String?
}

extension ExerciseForWorkout {
    static func getAllExerciseForWorkout() -> NSFetchRequest<ExerciseForWorkout> {
        let request:NSFetchRequest<ExerciseForWorkout> = ExerciseForWorkout.fetchRequest() as! NSFetchRequest<ExerciseForWorkout>

        let sortDescriptor = NSSortDescriptor(key: "dateCreated", ascending: true)

        request.sortDescriptors = [sortDescriptor]

        return request
    }
}

崩溃报告:

Thread 0 Crashed:
0   com.apple.SwiftUI               0x00007fff2c57a3fd FetchRequest.wrappedValue.getter + 269
1   com.yonatanbensimon.project.gymappswiftuivone   0x0000000104f5463c Log.loggedExercise.getter + 300
2   Log.2.preview-thunk.dylib       0x00000001055f8bb4 closure #2 in closure #1 in closure #1 in Log.__preview__body.getter + 260
3   Log.2.preview-thunk.dylib       0x00000001055f8f1d partial apply for closure #2 in closure #1 in closure #1 in Log.__preview__body.getter + 13
4   com.apple.SwiftUI               0x00007fff2c280f9a Section<>.init(header:footer:content:) + 170
5   com.apple.SwiftUI               0x00007fff2c281b24 Section<>.init(header:content:) + 36
6   Log.2.preview-thunk.dylib       0x00000001055f73b6 closure #1 in closure #1 in Log.__preview__body.getter + 1718
7   Log.2.preview-thunk.dylib       0x00000001055f748c partial apply for closure #1 in closure #1 in Log.__preview__body.getter + 12
8   com.apple.SwiftUI               0x00007fff2c27b923 List<>.init(content:) + 83
9   Log.2.preview-thunk.dylib       0x00000001055f65fc closure #1 in Log.__preview__body.getter + 380
10  Log.2.preview-thunk.dylib       0x00000001055f6c5c partial apply for closure #1 in Log.__preview__body.getter + 12
11  com.apple.SwiftUI               0x00007fff2c0be646 NavigationView.init(content:) + 54
12  Log.2.preview-thunk.dylib       0x00000001055f4fc7 Log.__preview__body.getter + 391
13  com.yonatanbensimon.project.gymappswiftuivone   0x0000000104f55e6b Log.body.getter + 91 (Log.swift:19)
14  com.yonatanbensimon.project.gymappswiftuivone   0x0000000104f5a569 protocol witness for View.body.getter in conformance Log + 9
15  com.apple.SwiftUI               0x00007fff2c048067 DynamicPropertyBody.update(context:) + 1671
16  com.apple.SwiftUI               0x00007fff2c048890 protocol witness for static UntypedAttribute._update(_:graph:attribute:) in conformance DynamicPropertyBody<A> + 32
17  ???                             0x00007fff2f8c0c69 0 + 140733991095401
18  ???                             0x00007fff2f8a8ac5 0 + 140733990996677
19  ???                             0x00007fff2f8a8d83 0 + 140733990997379
20  ???                             0x00007fff2f8ac267 0 + 140733991010919
21  com.apple.SwiftUI               0x00007fff2c2e7cff AnyViewList.update(context:) + 671
22  com.apple.SwiftUI               0x00007fff2c2ea5ed partial apply for protocol witness for static UntypedAttribute._update(_:graph:attribute:) in conformance AnyViewList + 29
23  ???                             0x00007fff2f8a8ac5 0 + 140733990996677
24  ???                             0x00007fff2f8a8d83 0 + 140733990997379
25  ???                             0x00007fff2f8abf08 0 + 140733991010056
26  ???                             0x00007fff2f8b9dbb 0 + 140733991067067
27  com.apple.SwiftUI               0x00007fff2bfb6677 closure #1 in View.withChildrenForTest<A>(environment:options:body:) + 279
28  com.apple.SwiftUI               0x00007fff2bfb650d View.withChildrenForTest<A>(environment:options:body:) + 205
29  com.apple.SwiftUI               0x00007fff2c1ff21a View._allPreviews.getter + 90
30  com.apple.SwiftUI               0x00007fff2c1ff18d MakePreviews.visit<A>(type:) + 109
31  com.apple.SwiftUI               0x00007fff2c5fa8b2 _callVisitViewType2 + 34
32  com.apple.SwiftUI               0x00007fff2c1ff0ee static _PreviewProvider._allPreviews.getter + 174
33  com.apple.dt.XCPreviewKit       0x0000000105589307 0x10556e000 + 111367
34  com.apple.dt.XCPreviewKit       0x0000000105574fb0 0x10556e000 + 28592
35  com.apple.dt.XCPreviewKit       0x0000000105585e8b 0x10556e000 + 97931
36  com.apple.dt.XCPreviewKit       0x0000000105585e34 0x10556e000 + 97844
37  com.apple.dt.XCPreviewKit       0x00000001055867c9 0x10556e000 + 100297
38  com.apple.dt.XCPreviewKit       0x0000000105584e31 0x10556e000 + 93745
39  com.apple.dt.XCPreviewKit       0x0000000105585159 0x10556e000 + 94553
40  ???                             0x00007fff5223e848 0 + 140734571472968
41  ???                             0x00007fff5223f7b9 0 + 140734571476921
42  ???                             0x00007fff5224bc9b 0 + 140734571527323
43  com.apple.CoreFoundation        0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
44  com.apple.CoreFoundation        0x00007fff23bceca9 __CFRunLoopRun + 2329
45  com.apple.CoreFoundation        0x00007fff23bce066 CFRunLoopRunSpecific + 438
46  com.apple.GeoServices           0x00007fff384c0bb0 GSEventRunModal + 65
47  com.apple.UIKitCore             0x00007fff48092d4d UIApplicationMain + 1621
48  com.yonatanbensimon.project.gymappswiftuivone   0x0000000104f5346b main + 75 (AppDelegate.swift:13)
49  libdyld.dylib                   0x00007fff5227ec25 start + 1

Thread 1:
0   ???                             0x00007fff52462818 0 + 140734573717528

Thread 2:
0   ???                             0x00007fff52462818 0 + 140734573717528

Thread 3:
0   ???                             0x00007fff52462818 0 + 140734573717528

Thread 4:
0   ???                             0x00007fff52462818 0 + 140734573717528

Thread 5:
0   ???                             0x00007fff52462818 0 + 140734573717528

Thread 6:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib          0x00007fff523b625a mach_msg_trap + 10
1   libsystem_kernel.dylib          0x00007fff523b65d0 mach_msg + 60
2   com.apple.CoreFoundation        0x00007fff23bd3cf5 __CFRunLoopServiceMachPort + 197
3   com.apple.CoreFoundation        0x00007fff23bcea17 __CFRunLoopRun + 1671
4   com.apple.CoreFoundation        0x00007fff23bce066 CFRunLoopRunSpecific + 438
5   com.apple.Foundation            0x00007fff2576b86f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 211
6   com.apple.Foundation            0x00007fff2576bae2 -[NSRunLoop(NSRunLoop) runUntilDate:] + 72
7   com.apple.UIKitCore             0x00007fff48135028 -[UIEventFetcher threadMain] + 138
8   com.apple.Foundation            0x00007fff257817a7 __NSThread__start__ + 1047
9   ???                             0x00007fff52466e65 0 + 140734573735525
10  ???                             0x00007fff5246283b 0 + 140734573717563

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x000000000000001c  rdx: 0x0000000000000303
  rdi: 0x00007fff89e0b600  rsi: 0x0000000000000303  rbp: 0x00007ffeeacaa950  rsp: 0x00007ffeeacaa910
   r8: 0x00007f8085821950   r9: 0x000000000000003f  r10: 0x00007ffeeacae178  r11: 0xffffffffffffff68
  r12: 0x00007fff89e4fff0  r13: 0x000060000190a520  r14: 0x00007ffeeacaab40  r15: 0x0000000000000000
  rip: 0x00007fff2c57a3fd  rfl: 0x0000000000010246  cr2: 0x00007fff2c282090

它一直崩溃,我不知道为什么会崩溃。

我发布的不是完整的崩溃报告,因为该文件包含太多字符。我发布了我认为报告的核心部分。

最佳答案

一切都在我这边进行。我有时在使用 CoreData 时遇到的问题是 ENTITIES 信息不正确。

我的看起来像这样:

enter image description here

主要是 ModuleNameCodegen 属性必须正确。

关于ios - 在 Swift UI 中实现核心数据而不崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573178/

相关文章:

ios - 轻量级迁移/addPersistentStoreWithType 挂起应用程序启动?

iphone - 在 Dropbox 上上传图片时出错

ios - 如何在 SwiftUI 中滚动顶部

ios - 尝试在 Swift 中访问嵌套字典数组中的键

ios - 如何设置导航栏右栏项目按钮的突出显示调整图像属性

ios - 换行时出现弹跳

ios - MagicalRecord iCloud 问题

ios - 将 UIView 保存为具有透明背景的 png 文件

objective-c - 是否允许重置标签栏 Controller ?

ios - 当用户将手机切换为静音时,如何防止我的iOS VoIP应用播放音频?