2

I am trying to implement CoreData into my iOS application. I want to add objects to a list and to save them. Unfortunately, my app crashes and does not work.

My application keeps crashing after implementing CoreData in my SwiftUI Application.

Here is my main code :

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())
    }
}

Here is my 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
    }
}

Crash Report:

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

It keeps crashing and I have no idea why it's crashing.

What I posted isn't the full crash report because the file has too many characters. I posted what I think is the central part of the report.

5
  • 2
    What is the console error message and have you added an exception breakpoint? Also what action are you attempting that causes the crash? Commented Jan 3, 2020 at 4:28
  • There is no error message in the console and I am just trying to load the canvas and it is not working, it keeps crashing with no errors Commented Jan 3, 2020 at 4:32
  • There will be an exception messsge. Try adding an exception Breakpoint. Commented Jan 3, 2020 at 4:33
  • Well weirdly after a restart, it has started working again. Thank you for your help Commented Jan 3, 2020 at 4:38
  • Ahh, classic Apple: Have you tried turning it off and on again? Commented Jan 3, 2020 at 4:39

1 Answer 1

1

Everything works on my end. The problem I sometimes face with CoreData is that the ENTITIES information is incorrect.

Mine looks like this:

enter image description here

Mainly it's the Module, Name, Codegen properties that have to be correct.

Sign up to request clarification or add additional context in comments.

3 Comments

I just had to restart my Mac and it started working. Thank you for your help
Any progress, still yet cannot fix. Keep crashing on FetchRequest.wrappedValue.getter
@RomanSafin try what I’ve answered here: stackoverflow.com/questions/59166513/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.