UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.