Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Development: Can I store an array of integers in a Core Data object without creating a new table to represent the array?

I'm using Core Data and I'm trying to figure out the simplest way to store an array of integers in one of my Core Data entities. Currently, my entities contain various arrays of objects that are more complex than a single number, so it makes sense to represent those arrays as tables in my DB and attach them using relationships. If I want to store a simple array of integers, do I need to create a new table with a single column and attach it using a one-to-many relationship? Or is there a more simple way?

Thanks in advance for your wisdom!

like image 496
BeachRunnerFred Avatar asked Dec 27 '10 21:12

BeachRunnerFred


2 Answers

Chris Hanson already gave a good answer to this question, still if you really want to store the NSArray as Core Data attribute take a look at transformable attributes

like image 136
Martin Brugger Avatar answered Sep 29 '22 13:09

Martin Brugger


For an array of integers, I would use a Binary property - which is an NSData. NSData has a lot of accessor/modifier methods. If you need/want to keep an mutable copy, then you can use NSMutableData as a cache - and persist that (as an NSData) as makes sense.

I use this for long lists of doubles - which, for me, represent collected data sets. This works for me because I have no need to link a single data point to some other object in my model. At runtime, I unpack these data sets and use them to render graph lines. At that point, I could generate some sort of relatedness - but that relatedness would need to be derived.

like image 35
westsider Avatar answered Sep 29 '22 14:09

westsider