Search code examples
ioscore-datansdatansobject

How to save a (custom) NSObject in Core Data


In my application i have an custom NSObject, which contains 2 mutable Arrays. I need to save this custom NSOBject into a core data entity, but i have no real idea how i can accomplish that...

After some searching, i found out, that the best way would be to convert the nsobject to nsdata and save it in an transformable field of the entity... but i m not sure how to do that. can someone help me?

heres to code for my custom object:

MeasureData.h

@interface MeasureData : NSObject{
}

@property (nonatomic, strong) NSMutableArray *questionsData;
@property (nonatomic, strong) NSMutableArray *answersData;

- (id) init;

@end

MeasureData.m

#import "MeasureData.h"

@implementation MeasureData

@synthesize questionsData;
@synthesize answersData;

#pragma mark -
#pragma mark int

- (id)init
{
self = [super init];

// Initalize questions array (width data from plist)
questionsData = self.makeQuestionsArray;
// NSLog(@"loaded questions array: %@",questionsData); // debug


// Initalize answers array
answersData = self.makeAnswersArray;
// NSLog(@"loaded answers array: %@",answersData); // debug


return self;
}

-(NSMutableArray *)makeQuestionsArray
{
// Initalize questions array (width data from plist)
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"questions.list" ofType:@"plist"];
NSMutableArray *questions = [NSMutableArray arrayWithContentsOfFile:path];


/*
 [questionsData insertObject:(NSString *)string atIndex:0];
 */

return questions;
}

-(NSMutableArray *)makeAnswersArray
{
// Initalize answers array
NSMutableArray *answers = [NSMutableArray arrayWithCapacity:0];

return answers;
}

-(id)initWithCoder:(NSCoder *)decoder {
if ((self=[super init])) {
    questionsData = [decoder decodeObjectForKey:@"questionsData"];
    answersData = [decoder decodeObjectForKey:@"answersData"];
}
return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:questionsData forKey:@"questionsData"];
[encoder encodeObject:questionsData forKey:@"questionsData"];
}

@end

According to the first comment, i implemented the encoder/coder functions for my custom class. And tried to archive and encode it (i m new to ios, so it could be completly wrong) - but it dont work... can someone tell me whats wrong?

heres the encoding (which dont work XD):

NSMutableData *dataToSave = (NSMutableData *)self.measureData;
NSKeyedArchiver *archiverForData = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataToSave];

[archiverForData encodeObject:dataToSave forKey:@"dataToSave"];
[archiverForData finishEncoding];
//

//theMeasure is the CoreData Entity
theMeasure.result = dataToSave;

Solution

  • In outline:

    • create a NSMutableData
    • create a NSKeyedArchiver with initForWritingWithMutableData over your data
    • serialize your arrays / objects / whatever you need (that implements NSCoding) with encode... methods of NSCoder
    • create a managed object with a BLOB (binary data) type field
    • write your encoded data from the mutable data to this field of the managed object.

    In my answer to this question you can find some useful links: NSCoding VS Core data