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

SOLVED: Project 12: Is there an easy way to clear the Core Data storage?

Forums > 100 Days of SwiftUI

Is there an easy way to clear what is currently saved in CoreData?

It is hard to follow along with project 12 without knowing how to do that.

He has us change our data model entities around so many different times in that project, and now my app runs into problems when I try to compile it.

I had a button in my View that added 3 sample instances of my entity to my data model, and clicked it multiple times. So now there are probably 4 copies of each instance of the Entity.

Then I tried to add constraints to my Entity, add the NSMergeByPropertyObjectTrumpMergePolicy to my code, and run the app again, but it is running into errors. It says Build Succeded, but then the simulator crashes and I have a super long error in the output log. But the part of the error that seems to make sense to me says this...

NSUnderlyingException=Constraint unique violation, reason=constraint violation during attempted migration

I assume it is because it is trying to load the data with multiple copies of entities already stored in it, but I have added constraints that would not allow that data to exist in my data model. So I am wondering if there is a way that I can just clear the data from the data model without having to restart the whole project from scratch.

2      

func deleteall(){
        for c in trueuser{
            moc.delete(c)
        }
        try? self.moc.save()
    }

"trueuser" is your fetch result,"c" is a variable,it can go through all your items in for loop.

2      

I can see how this would work if your entities still existed in the data model, but what if you have already deleted those entities from your data model?

For example, lets say I created a Book entity earlier in the project, and created multiple instances of Books in the data model.

Then, later in the project, Paul tells us to create a Singer entity instead. So, I delete the Book entity from my data model and it no longer exists. Are the instances of Book that I had already saved instanty deleted when I delete the Book Entity? Or are those instances of Book still in storage, but I no longer have a way of writing code to fetch them because now Book does not exist in the current context?

If the instances of Book do still exist in memory even though Book no longer exists as an entity, then lets say you forgot how the Book entity was originally defined and have no way of recreating it. Does that mean that those instances of Book are just permenantly stuck in memory? There is no way to just clear the entire database file in XCode without writing an application that is capable of running a FetchRequest to find the particular data you are looking for?

2      

If this is just a one-time thing, you could take advantage of the 14-day free trial of Core Data Lab. It will alllow you to browse your Core Data stores and you can delete records and a whole lot more.

2      

That would probably help me to see what is going on a little better. But, for now, I'm not trying to add any more subscriptions to my list, and I am hesitant to use my free trial for that now, because it does seem like it might come in handy in the futute. But thanks for the recommendation. For now I guess I'll just chalk it up as "I don't understand Core Data well enough yet, but I'll get by."

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.