0

I need to create some Core Data objects outside the context, in other words, these are objects that will not need to be saved in Core Data. Based on a couple of threads I read it seems that creating the objects from inside the context without saving them is the best way to accomplish this, everything seems to be working fine but some notes from this thread got me wondering if the objects are in fact added to somewhere and need to somehow be deleted.

Mundi's Comment:

If you want to discard it, just delete the object before saving, and it will never touch the persistent store.

In the following code, I'm creating and returning a Dog object from within context by calling the unmanagedDog method, please note that objects created with this method are not saved.

Is there anything that needs to be done after creating objects this way?

Again, I'm just concerned because of @Mundi's comment, stating that the objects need to be deleted.

class DogServiceModel: ObservableObject{
    let manager: CoreDataManager

    // this creates a Dog and saves it in Core Data
    func addDog(name: String?, vaccines: Double?){
        let dog = Dog(context: manager.context)
        dog.name = name ?? ""
        dog.vaccines = vaccines ?? 0
        // some other expenses
        self.manager.save()
    }
    // this creates a dog object but does NOT save it in Core Data    
    func unmanagedDog(name: String?, vaccines: Double?)->Dog{
        let dog = Dog(context: manager.context)
        dog.name = name ?? ""
        dog.vaccines = vaccines ?? 0
        // some other expenses
        return dog
    }
}

class Calculator{
    func calculateDogPrice(dog:Dog){
        // do some calculations with the data insed the Dog object
    }
}

class OtherClass{
    let dogSM = DogServiceModel()
    let calculator = Calculator()

    func doSomethingTemporarly(){
        let unmanagedDog = dogSM.unmanagedDog(name: "Teddy", vaccines: 1500)
        
        calculator.calculateDogPrice(dog:unmanagedDog)
        // some other stuff
    }
}
fs_tigre
  • 10,650
  • 13
  • 73
  • 146

1 Answers1

1

The likely problem with that code is that unmanagedDog is inserted into the context and, although you don't save changes immediately, you're probably going to save changes at some point in the future. When you do that, unmanagedDog will be saved, because saving changes saves all changes in the context. Unless you're sure that you'll never save changes on manager.context, you need to do something if you don't want to save unmanagedDog.

If you don't want to save it, you can do either of these:

  • Delete it before saving changes. That's fine, there's no reason not to do that if you don't want the object.
  • Create a new managed object context-- maybe a new child context of the one you're using-- and then never save changes on that context. You can still save changes on manager.context without saving objects from this new context.
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Shoot, thank you very much for your explanation, it's helping me a lot to understand how `context` works. Although I haven't been able to find a good source that shows how to create a child context, I'm a little lost here, since I don't fully understand how you can have two `contexts` managing the same Core Data objects. I need to read more about `context`s in general and how I can create a second one. – fs_tigre Aug 17 '23 at 19:13
  • 1
    For a child context you can create a new managed object context and then set its `parentContext` property to one that already exists, like the one you have here. If you're just using it for temporary objects, that's all there is. If you ever want to save changes on the child context there's a little more to it, to make sure that changes get saved to the persistent store file. – Tom Harrington Aug 17 '23 at 20:08
  • So, if I'm understanding you correctly, I could literally create a new file, copy and paste the code from the existing `context` and use this `newContext` for creating temporary objects, and as long as I don't call `newContext.save` the objects created in the `newContext` will never get saved, correct? Thanks. – fs_tigre Aug 17 '23 at 20:28
  • 1
    I'm not sure about the new file part. If you mean a new source code file, it's your call and depends on your project. If you mean a new Core Data persistent store, that's not necessary and would probably make things a lot more complex than necessary. – Tom Harrington Aug 17 '23 at 22:30
  • Yeah, a new source code file (`var newContext: NSManagedObjectContext`) but using the same `NSPersistentContainer`. Thanks. – fs_tigre Aug 17 '23 at 23:13