How to import array attribute with MagicalRecord into CoreData

476 views Asked by At

I am importing a json where the objects have many array attributes such as images:

"images": [
    "model1.jpg",
    "model2.jpg"
],
"models": []
    "one model",
    "another model",
    "third model"
]

Currently I just do:

[ExampleObject MR_importFromArray:objectArrayFromJson];

but these arrays break this auto import since it can't auto fit NSArray to NSData (the binary from when setting the model up in Xcode).

Is there anyway to modify the Model class files (like custom setters/getters) so the MagicalRecord can import my array and store it in the entitys´ attribute and when I retrieve it I get an NSArray in return?

1

There are 1 answers

0
Sebastian L On BEST ANSWER

I solved this myself after some research and I want to share it to whoever might get stuck with same problem.

My problem was that I wanted to save an NSArray into an entity attribute of type NSData. To be able to do this with MagicalRecord I needed to implement a method in my NSManagedObject m-file like this:

- (BOOL) importImages: (id) array {
    NSData *imagesData = [NSKeyedArchiver archivedDataWithRootObject:array];
    self.images = imagesData;
    return YES;
}

so import<;attribute-name without ;> the method must be called.

EDIT: According to this page you return YES if your code process the data. Return NO if you want MagicalImport to continue processing the attribute and use the default import routines.