Dunes Buggy
Dunes Buggy

Reputation: 1819

Core Data deleteObject sets all properties to nil

I'm a little new CoreData.

When I call deleteObject() with object on my NSManagedContext object, its setting all the properties in the object to nil. Is there anyway for me to avoid this? I don't want to be nullified.

My project is in Swift.

Upvotes: 0

Views: 491

Answers (1)

SpacyRicochet
SpacyRicochet

Reputation: 2279

You're misunderstanding the purpose of CoreData. It's a way of managing a persistent store, which means that whatever you tell your context, is absolute. So if you deleteObject(), that object gets prepared for deletion and you're not supposed to touch it anymore.

Instead, you want some kind of mirror object, that allows you to create a new copy of the NSManagedObject for in-memory use. You could do it like this;

struct MirrorManaged {
    var text: NSString
}

class Managed: NSManagedObject {
    @NSManaged var text: NSString

    func copyToMemory() -> MirrorManaged {
        return MirrorManaged(text: self.text)
    }
}

Upvotes: 1

Related Questions