|
Hi I'm new to core data so appreciate I may be missing the obvious but.... I have created an entity (Licence) which has 4 attributes (licenceID:UUID, startDate:String, endDate:String, licenceName:String). I've been able to create a fetch request using the below, as well as display records in a list, add records, delete records.
What I'm struggling with is... Due to the way my app works there will only ever be one set of data for the entity. How can I refer to this single record within my view?? EG1 - Rather than using a list I'd prefer to have a simple text view. Using something like...
EG2 - Be able to refer to an attribute. Using something like...
I appreciate that the nature of the fetchRequest is to return sets of data but is there a way to filter to one record eg .first?? Apologies if I'm missing the obvious - any pointers?? |
|
You are looking for https://www.hackingwithswift.com/books/ios-swiftui/filtering-fetchrequest-using-nspredicate However, if you are only ever using a single entity then Core Data is not really useful here. If the license needs to be kept somewhere safe, you might want to look into using Keychain Access. If it's not necessary to store securely, then User Defaults would make more sense. |
|
Thanks Marcus Unfortunately I need to store the data within the app. Predicate will work well to retreive the one instance of data. I was hoping that there would be an easy way of referencing the attributes of the single item. Will keep trying... |
|
I agree with Marcus. If you are only going to have a single entity and/or a single instance of that entity (or even a single instance of multiple entities) you don't need CoreData. You don't mention how large this instrance of data is, but if it is license data, it is probably not that large. I would suggest you simplify your life and use another option to persist the data. For example: Using @AppStorage would look like this and is automatically persisted in the App bundle anytime any of the values change.
|
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.