NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: CoreData: read only pre-populated db

Forums > Swift


I am trying to build some app that needs a pre-populated db (read only) that will be able to be completed by users. I haven't been able to find help online, I am fairly beginner in using CD.

This object (A) will have a relationship with an entirely user populated object (B).

A -> one to many -> B B -> one to one -> A

Basically A will have a list of read only default entries but needs to be able to be topped up by user custom entries (deletable/ modifiable).

Is that possible to do?



Sure. Just don't give the user the possibility to edit entries in table A in any way. Usually, you can do this with not providing a user interface to do so.


As @Hatsushira says, if the Entity A will be only pre-populated and user cant add more, then the best option is to not allow editing of any kind.

Another approach (if you wanted user to be able to add new Entity A objects) would be to have attribute like isLocked which would be true for the pre-populated entities and you could check it and after that either deny or allow the user the option to edit.


@nemecek-filip Good idea. This is a easier approach I believe: is it possible to disable onDelete for selected items only? Would you have some kind of guide how to prepopulate, it seems to be not so straightforward. I can't really find any advanced guide on Core Data on hackingwithswift. Is it included in any of the books?



I think you need to do it manually. So ideally you would do delete in a single method and that would check isLocked and if that is true then the method would not delete the entity.

I don't think there is much CD in other books, not 100 % certain. has great Core Data book - it is really in depth and helped me a lot.

As for pre-populating, I would check the entities count and if it is zero, then it means first launch and you can pre-populate on the background to avoid slowing the UI. If you are interested, I have recently shared my Core Data Stack setup via blog post -

SwiftLee also has quite a lot of posts on Core Data -




Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try 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.