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

SOLVED: question about managed object context

Forums > SwiftUI

So i've been learning from 100 days. of swiftui about moc and i have some questions. First one: if i understand correctly, moc is like a place where you can save data, but its not written to the hard disk till you save it right? second one: whats the difference between a managed object and managed object context? last one: what exactly does. this code do

class DataController: ObservableObject {
    let container = NSPersistentContainer(name: "Bookworm")

    init() {
        container.loadPersistentStores { description, error in
            if let error = error {
                print("Core Data failed to load: \(error.localizedDescription)")
            }
        }
    }
}

i understand its to initialize core data right? but id like to know what each line does. sorry for bad grammar or punctuation, my mac sometimes skips spaces/letters.

2      

To answer your questions it's best to define what a managed object and a managed object context are.

A managed object represents your entities. A managed object context is best defined by quoting Apple's Core Data Programming Guide:

Think of the managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad[…]. You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered.

And to answer your last question:

class DataController: ObservableObject { // Create a class that conforms to ObservableObject, by doing so we make sure that it stays alive for the whole life cycle of our app.
    let container = NSPersistentContainer(name: "Bookworm") // Uses the name to name the persistent store and to look up the managed object model, we therefore use the same name as in Bookworm.xcdatamodeld

    init() { // Initializes the class
        container.loadPersistentStores { description, error in // Tells the persistent container to load its persistent stores, by doing so it completes the creation of the core data stack. The method returns a description of the stores and can throw an error.
            if let error = error { // Assign error to constant error.
                print("Core Data failed to load: \(error.localizedDescription)") // Print out the error message.
            }
        }
    }
}

Here are some great ressources:

https://developer.apple.com/documentation/coredata/core_data_stack

https://www.objc.io/issues/4-core-data/core-data-models-and-model-objects

https://cocoacasts.com/working-with-managed-objects-in-core-data

2      

thank you!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.