Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing an array of "int"s in core data
    primarykey
    data
    text
    <p>I am trying to design my data-structures for a new App that I am working on, and I am using core data (for the first time).</p> <p>I've been struggling a bit with working around the lack of arrays in core data, but I have mostly worked things out.</p> <p>The one issue that I have left is that one of my object types has an array of "int"s. I may have LOTS of these ints, and so I want them to be space efficient. I also want to be able to access them quickly.</p> <p>In my particular case, my array of ints will only have two operations performed on them. One is to iterate over them, and the second is to append new ints. The most common operation (and the one that I need to keep most efficient) is just being able to read all of the values in the array.</p> <p>For storage, I figure that I need to use a "binary data" attribute on my object. Now I'm trying to figure out the best way to pack the "int"s into an array of bytes.</p> <p>My app is going to create data that is only used on iOS devices for now, but may be used on other machines in the future, so I want to make sure that my solution is architecture independent, but at the same time fastest on iOS devices with slower processors and concern with battery life.</p> <p>I'm currently thinking of designing my binary data so that the ints are stored in the "little endian" format. My object would be a sub-class of NSManagedObject, and it would have a side field (not part of the core data storage) which is an "int *" pointer which I would use to iterate over the values.</p> <p>When my object is faulted into memory, I would initialize the c array. If the endianness is "little endian" (which I would determine using CFByteOrderGetCurrent()), then I can read the array of ints very efficiently with code like this:</p> <pre><code>@property (retain) NSData *data; // Core Data binary attribute ... int *cArray = (int *) [self.data bytes]; </code></pre> <p>If the endianness is not little-endian, I would need to allocate local storage, unpack the int values properly, and make keep both the binary attribute and the c-array up to date with changes.</p> <p>I would add code to my sub-class to manage faults (i.e. nil-ing out the cArray when faulted out, and setting it properly when faulted back in).</p> <p>So, I think I have a solution that should work for me, although I don't really like it.</p> <p>If anyone can think of a better way to do what I am trying to do, I would really appreciate it.</p> <p>Also, if anyone can think of reasons why interactions between the c-array and NSData or core data might cause me problems, I would really appreciate it.</p> <p>Thanks, Ron</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload