2

I get nil error since I changed my project to use 'objectWillChange.send()'. Because I needed to update the view manually since it doesn't update itself for custom carousel view(because of core data?).

If I write "1" and tap the next button(NavigationLink)

Console log
page.name: 1
catch error: nilError

Here is the codes.

AddPage Class

class AddPage: ObservableObject {
@Environment(\.presentationMode) var presentation
@Environment(\.managedObjectContext) var moc

    func addPage(item: String) {
    
        if item != "" {
        
            let page = Page(context: self.moc)
            page.name = item
        
            do {
                print("page.name: \(page.name ?? "Unknown")")
                try self.moc.save()
            } catch {
            print("catch: \(error)")
            }
        
            objectWillChange.send()
            presentation.wrappedValue.dismiss()
        }
    }
}

AddPageView

@ObservedObject var add = AddPage()  

NavigationLink(destination: ContentView()){
Text("Next")
    .background(
        Rectangle()
            .frame(width: 330, height: 60, alignment: .center)
    )
    .onTapGesture {
        add.addPage(item: pageName)
    }
}

SceneDelegate Class

added only

var addpage = AddPage()
let contentView = ContentView()
            .environment(\.managedObjectContext, context)
            .environmentObject(addpage)

Can someone please help me..?

Taeeun Kim
  • 964
  • 1
  • 8
  • 20
  • You create the AddPage object before doing `. environment(\.managedObjectContext, context)`, not sure if this works. I would not use `@Environment(\.managedObjectContext)` inside AddPage but instead inject the context as an argument to the init method – Joakim Danielson May 01 '21 at 13:52
  • @JoakimDanielson Thank you for your reply! I couldn't try your way, cause I don't know how to inject "@Environment(\.managedObjectContext)" to the init method.. But I could save the value in page.name which means it works with "@Env..."? – Taeeun Kim May 01 '21 at 14:14
  • I meant to avoid @Environment here, what you should inject is the `context` object – Joakim Danielson May 01 '21 at 14:26
  • @JoakimDanielson thank you for your reply again! I will try to this. – Taeeun Kim May 01 '21 at 15:08

1 Answers1

5

Change

@Environment(\.managedObjectContext) var moc 

To something like (If you are using the sample code that Xcode generates) if not manually get your moc however you set it up

var moc = PersistentController.shared.container.viewContext

@Environment wrappers are inconsistent in a class they should only be used in a SwiftUI View.

If you don't have a PersistentController get the code from a brand new project with SwiftUI lifecycle and CoreData. Be CAREFUL and keep the name from the original stack it is usually the app name

Comment out (DONT delete until you are sure this works) the stack code from the SceneDelegate and

make a variable

let context = PersistentController.shared.container.viewContext

So you can keep the context in your View

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • Thank you for your help!! But if I put this "var moc = PersistentController.shared.container.viewContext" in AddPage Class, I get an error, can you tell me where should I put it..? Error Message: "Cannot find 'PersistentController' in scope" – Taeeun Kim May 01 '21 at 17:25
  • `PersistentController` is a `class` it comes from the standard code that Xcode provides when you create a new Project with CoreData. replace that line with the line that provides the `context` for this line `.environment(\.managedObjectContext, context)` you have to get the context from the stack directly. – lorem ipsum May 01 '21 at 17:29
  • Aha..! I found that line from SwiftUI Life Cycle App! Currently, I am using UIKit Life Cycle, is there also a way to use this methode for UIKit Life Cycle? If there is a no way, of course, I will change the life cycle to SwiftUI! – Taeeun Kim May 01 '21 at 17:34
  • You have to access the stack somehow or create a `PersistenceController`. I added some info in the answer since it is more than fits here – lorem ipsum May 01 '21 at 17:44
  • Thank you for your reply! I will try to test both ways which you wrote! – Taeeun Kim May 01 '21 at 18:24
  • with your advice to add "var moc = PersistenceController.shared.container.viewContext" works perfect! thank you so much! – Taeeun Kim May 02 '21 at 21:06