Reputation: 83
because I do not know how to use relationships in core data And I tried many times without success.
I want to create an array as a attribute of an entity
What type of attribute should be?
And how to save the array in the core data
And how do I get it?
I would be happy Sample Code
Upvotes: 0
Views: 675
Reputation: 46728
You really do not want to create an array and store it in Core Data. It will not be searchable and the performance will suffer.
You are far better off building a test project and learning how to use relationships. You treat a relationship object as any other object but it returns a set rather than a single object.
I would suggest reading some of the primer material on Core Data and master relationships otherwise you will not use Core Data at anywhere near its full potential.
Upvotes: 2
Reputation: 309
You can store an NSArray
as a Transformable
attribute. Then you can use the NSCoding
to serialize the array. The advantage of this approach is that it's easy but you can't query into the array because it's stored as a BLOB in the data store. If the array is large, you may have to save or load a lot of data to/from the data store just to read or modify a small part of the collection. So you should work with small Array data.
this is my simple sample. CoreDataWithArraySample
Upvotes: 1
Reputation: 371
For that you can use the transformable attribute type in CoreData. In simple terms that attribute type can be thought of as a pointer to any objective-c object that supports NSCoding. NSArray does, so you can assign one of those to that attribute but then for things to work with the persistence store also all the objects you store in the NSArray also need to be NSCoding compliant.
Upvotes: 0