|
I have been trying to figure out CoreData. Swift for that matter! LOL! Anyway, I am starting small to try and understand it. So, here's my simple app "CoreDataTest" I have two entities in my CoreDat Model. Test1, and Test2. Test1 has two attributes, name1 as type String and array1 as type Transformable. Test2 has name1 as type String and dictionary1 as type Transformable. CodeGen is set to Manual/None, and I created the class and extension files as shown in a tutorial via Edit > Create NSManagedObject Subclass. I have a DataHelper class that holds all my methods. The issue I am having is that in my fetchRequest method for either entity, I am having to Type the holding property to be either Test1, or Test2, respectively. However, when the fetchRequest occurs on Test1, I get a failure with no error codes. The fetchRequest for the dictionary is returning what appears to be a string representation of the dictionary data. I want to store an Array & Dictionary, and I want to retrieve an Array & Dictionary. I have created a github repository of the project at https://github.com/tcarroll2/CoreDataTest I appreciate any help. Tom |
|
So I've been working through more of the tutorial I was following, and it appears that plain arrays are the issue. Is there a special way I need to fetch an Array from the store? |
|
There are some ways how to store array in an entity but it looks like a not elegant design. Traditionally in Core Data if you want to have a array of items you would create separate entity and use relationship to connect them. Then one entity would have set of these related entities. If you really want to dive deep into Core Data and dont mind paying then I can highly recommend the book Core Data by objc.io. It is great and I still regret not reading it sooner. |
|
Well crap. Scratch that. I added an array to the entity that is working, and it's returning the data just fine. I'll have to go back and take another refined look at the non-working entity to see if I can discover why its not working... |
|
hi, i would not give up on the array of Int; that's transformable and should work well. especially if the amount of data is small, the order is important, and you'll not be doing a Core Data fetch based on any of the values. it's a lot cleaner than putting lots of Entities into Core Data whose only attribute is an Int (along with the relationship needed to track it), and handling the NSOrderedSet relationship implementation to track them. i think your problem is the Dictionary of type hope that helps, DMG |
|
@delawaremathguy I've steered clear of the struct / entity thing just because I don't understand how to search the store for a particular item within an entity using the struct. It seems logical that I would have something like name: String, value: NSNumber in an entity/struct of Record. I just don't how I would search the store to find "foo" with a value of 72839472102. I'm sure I'll get there. I've been learning a lot lately and when I come back to that part of my app I may have learned enough to figure it out. |
|
hi Tom, i'm not sure why i did not respond to this earlier ... apologies. other than writing your own transformable implementation for a Dictionary of type
finally but you could do this with a generic fetch request in Core Data to find all NameValuePairs where the name is "foo" and the value is "72839472102". however, there could be several different test2Objects that have such an associated name and value pair. whew! ADDED AS AN AFTERTHOUGHT: how about storing your dictionary as simple Data? both String and Int are Codable, so your dictionary would be Codable. you could write it as data with a JSONEncoder and bring it back with a JSONDecoder. you'd use two functions very similar to those above to access and update the data. hope that helps, DMG |
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 February 9th.
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.