NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: CoreData: read only pre-populated db

Forums > Swift

Hi,

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?

Thanks

   

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?

Thanks

   

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. Objc.io has great Core Data book - https://www.objc.io/books/core-data/ 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 - https://nemecek.be/blog/41/my-approach-to-setting-up-core-data-stack

SwiftLee also has quite a lot of posts on Core Data - https://www.avanderlee.com/category/core-data

   

thanks

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.