GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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

3      

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.

3      

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.

3      

@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

3      

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

3      

thanks

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

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.