I want to add my imageArray into coredata as transformable but this is not storing properly.
My save button coding.
- (IBAction)saveButton:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"FoodInfo" inManagedObjectContext:context];
NSAttributeDescription *messageType = [[NSAttributeDescription alloc] init];
[messageType setName:@"photos"];
[messageType setAttributeType:NSTransformableAttributeType];
[imagesForShow addObject:messageType];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unable to save context for class" );
} else {
NSLog(@"saved all records!");
[context save:nil];
}
//[newEntry setValue:imagesForShow forKey:@"images"];
}
- Here 'imagesForShow' is my array of images.
When iam going to fetch this image array , this showing nil
- (void)viewDidLoad {
[super viewDidLoad];
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"FoodInfo"];
[request setReturnsObjectsAsFaults:NO];
arrayForPhotos = [[NSMutableArray alloc]initWithArray:[context executeFetchRequest:request error:nil]];
// Do any additional setup after loading the view.
}
What I am doing wrong with this code. Thanks.
In your save code:
I can't even figure out what this is supposed to do. It's completely wrong. You should never be allocating an instance of
NSAttributeDescriptionunless you are constructing a Core Data model on the fly-- which you are not doing and which almost nobody ever does. Creating the new entry is OK. The rest, I don't know. You said thatimagesForShowis your array of images, so I don't know why you're also adding an attribute description to the array.In a more general sense, if
newEntryhas a transformable attribute namedphotosandimagesForShowis anNSArrayofUIImageobjects, then you could do this:This is similar to a line that you have commented out, though it's not clear why it's commented out.
But whatever you do get rid of the code creating the
NSAttributeDescription.