GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

ForEach Causing Thread 1: EXC_BREAKPOINT when accessing core date (CloudKit)

Forums > SwiftUI

Hi All,

I have an app that was created with cloudkit as a capability.

The app uses core data to save items that users add.

When trying to show those items with a ForEach i get a Thread 1 EXC_BREAKPOINT error.

struct HumidorList: View {
    @Environment(\.managedObjectContext) var manObContext
    @FetchRequest(entity: MyHumidor.entity(), sortDescriptors: [])
    var myHumidorRow: FetchedResults<MyHumidor>
    @State var addNewHumidor = false

    var body: some View {
        NavigationView {
            List {
                ForEach(myHumidorRow) { hums in    ***Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c7aa6678)***
                    Text(hums.name)
                }
            }
            .sheet(isPresented: $addNewHumidor) {
                NewHumidor().environment(\.managedObjectContext, self.manObContext)
            }
            .navigationBarTitle("Select Humidor")
            .navigationBarItems(
                trailing: Button (
                    action: {
                        self.addNewHumidor = true
                },
                    label: {
                        Text("Add Humidor")
                }))
        }
    }
}

when i go to app delegate i see the following wirtten in purple after the line

class AppDelegate: UIResponder, UIApplicationDelegate {

Context in environment is not connected to a persistent store coordinator: <NSManagedObjectContext: 0x281328620>

All your help is appreciated.

Kind Regards

Adam

3      

Hi Adam

How is the view presented? Is it through .sheet(). Also is your var myHumidorRow on a separate line below the FetchRequest or is it just overflow from the previous line?

Dave

3      

The var is on a spererate line not overflowed and the view is just a view at this stage. It calls a sheet when a button is clicked but the view i am hoping to show is just a list.

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course.

Save your spot

Sponsor Hacking with Swift and reach the world's largest Swift community!

struct ContentView: View {
    var body: some View {
        TabView{
            HumidorList()
            .tabItem {
                    Image(systemName: "list.dash")
                    Text("My Humidor")
            }
            AppView()
                .tabItem {
                    Image(systemName: "plus")
                    Text("second")
            }
        }
    }
}

This is where the view comes from

3      

Hi Adam

So the variable has to be on the same line as the @FetchRequeat so it should read as follows -

@FetchRequest(entity: MyHumidor.entity(), sortDescriptors: []) var myHumidor: FetchedResults<MyHumidor> 

Also try putting you managed object context variable in your content view as well. See how you go now.

Dave

3      

Hi Dave,

I did both of those things and unfortunately had no luck.

Still got the same error in the same place.

Do you think it has anything to do with me using cloudkit. The persistant container has changed to

lazy var persistentContainer: NSPersistentCloudKitContainer = {

3      

Although, the error on the same line has now changed to:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

3      

If i take the for each out the app works perfectly, obviously not showing the coreData

 var body: some View {
        NavigationView {
            List {
//                ForEach(myHumidorRow) { hums in
//                    Text(hums.name)
//                }
                Text("this")
            }
            .sheet(isPresented: $addNewHumidor) {
                NewHumidor().environment(\.managedObjectContext, self.manObContext)
            }
            .navigationBarTitle("Select Humidor")
            .navigationBarItems(
                trailing: Button (
                    action: {
                        self.addNewHumidor = true
                },
                    label: {
                        Text("Add Humidor")
                }))
        }
    }
}

3      

Try putting the ForEach back but change it to following -

ForEach(myHumidorRow, id: \.self) { hums in
  Text(hums.name)
   }

3      

That gives me:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

On the same line

3      

Hi Adam, adventitious I had to deal with the same Issue today... I could solve the problem by attaching following line of Code to the Views, which are using the Data from CloudKit/CoreData...

.environment(.managedObjectContext, (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext)

Found in the Apple Developer Forum: https://developer.apple.com/forums/thread/121213

Best whishes Robert

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course.

Save your spot

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.