How to store an array of arrays in Core Data

HI


I am writing an application in which I need to store in Core Data an array of arrays (a bidimensional array) in the type attribute of the entities there is no such type of array.


My approach is storing an array of NSmanagedObjects


var mySavedArrayOfArrays = [ [NSManagedObject ] ]( )


First Im trying to convert the original string array to NSmanagedObject

and then to queue the arrays making a bidimensional array to be strored.


Any clues?


Please Help!


Thanks for your attention.


Juan.

Replies

Without knowing any detail about your question, I'd create a table with all the possible x-axis values and another with all the y-axis values. Then I'd create a third table that 'has one' of each of the x and y values as well as whatever values you want to store.

Juan,


I've done this in the case of [[Int]] by defining its CoreData entity as Transformable -- in my case, the attribute is called "rawTeamsEachRound," perhaps within a entity called "Tournament." a typical instance might be [ [0,1,2,3], [0,2,1,3], [0,3,1,2] ] (think permutations of integer indices for a round-robin event involving 4 teams).


I then define the following fronting instance variable in code to fill out the rest of the "Tournament" class, letting me move instances back and forth with CoreData.


   var teamsEachRound: [[Int]] {

        get { return rawTeamsEachRound as! [[Int]] }
        set { rawTeamsEachRound = newValue as NSObject }
    }


i think this works because NSArray and Int are both NSObjects that know how to transform back and forth to Data without the need to supply an explicit NSValueTransformer.


i'm not sure, however, whether the same will work for an NSManagedObject as the inner-most type.


hope that helps,

DMG/J.

You can't serialize NSManagedObject, so you can't serialize types composed of it. All of the next-best-thing's for trying to do it all end up replicating parts of the Core Data functionality (you end up having to store the managed object ID's in the serialization and fetch the managed objects from the ID's in deserialization) without being able to replicate all of the functionality, because of how much CoreData does in type translation (and you lose the ability to ensure referential integrity). And, honestly, this is one of the places where you have to accept that CoreData is something built on top of database and you need to treat NSManagedObjects as things that live in databases.


How you do a "multidimensional array" in a database is a table; or a set of tables with one of the tables being the index table. That approach maps to CoreData as a CollectionIndex entity with the attributes for your indices and a to-one relationship to your destination entity. Then whatever you need to have reference that just needs a to-many reference to the CollectionIndex.


If you're implementing a traditional multi-dimensional array, it's going to be helpful that CoreData will let you specify that all of the index attributes are unique index constraints.