0
votes

In my App i use a Core Data model with only one Entity and some Attributes in it. What I want to achieve:

Attribute A "imagePath" is of type NSString that stores the path to the Document Directory where the corresponding .png image is.

Attribute B "imageCoreData" is of type Binary Data.

I want to move all the images in Document Directory to Core Data.

So I need code that does the following - get the image from Attribute A in Document Directory, convert it to NSData and save it as Binary Data to Attribute B "imageCoreData". And does this for every object i got.

Also it would be great if I could get informed if it´s successfully completed. Maybe also it should work in background to not block the UI.

The same I want for:

Attribute C "date" of type NSDate that stores a date.

Attribute D "dateString" of type NSString.

Get the date from Attribute C and convert the date from NSDate to NSString and save it in Attribute D. I need this also for all objects I got.

UPDATE

I can get a dictionary with all my paths to Document Directory. But how to pull out all images, covert and save to Attribute B ?

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
NSEntityDescription *entityImg = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];


fetchRequest.resultType = NSDictionaryResultType;


fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entityImg propertiesByName] objectForKey:@"imagePath"]];
fetchRequest.returnsDistinctResults = YES;


NSArray *dictionary = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

NSLog(@"%@", dictionary);
1
Unfortunatly nothing really working. The Convertion from one Attribute type to another isn´t that hard, but I fail in doing it for all my objects I got at once. I guess I need to loop through all my objects somehow, but since I am beginner I couldn t achieve this. - Kreuzberg
Look into NSFetchRequest to see how to retrieve all your entities from your Core Data store. Once you have retrieved them, you can iterate over them, update the attributes, and then save your updates. - Mike Taverne
Probably the problem is NSArray *dictionary. Something strange with that line? - Amin Negm-Awad

1 Answers

0
votes

First, it is non-sensical to store the same information twice. Decide which format you want, and store that.

Second, it is not recommended to store large images as binary data in Core Data. You could store small images, such as thumbnails, but otherwise it is a bad idea.

Third, when fetching your entity, you do not have to go into he complications of NSDictionaryResultType. Just use the default (NSManagedObjectResultType) and work with that. Because of the faulting mechanism of Core Data you will not have to pay any significant price in terms of memory or performance.

Guessing what your original problem was, you perhaps want to send the data somewhere else. In this case you should make the necessary transformations ad hoc.

For example, to create a date string from your entity, subclass the NSManagedObject (I would call this class Image) and add a convenience method. (Note that the date format is just an example. Consult the NSDateFormatter documentation for what you need.)

-(NSString*)dateString {
    if (!self.date) { return @""; }
    static NSDateFormatter *formatter = nil;
    if (!formatter) {
         formatter = [[NSDateFormatter alloc] init];
         formatter.dateFormat = @"yyyy-MM-dd hh:mm:ss"; 
    }
    return [formatter stringFromDate:self.date];
}

As for the image, you do not have to do any conversion because the content of your png file is already of type NSData. Just read it from the file URL and use the NSData object.