1

I have an array of UIImages that I want to store for easy access later. From what I've read I should save the references in Core Data and the images to disk. I have created a userImages attribute on my entity and set it to Transformable, and my image array is called userImages.

The next step is to do something like this:

NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"itemId=%@",3]];

item = [[context executeFetchRequest:request error:&error] lastObject];

if (error) {
    NSLog(@"error with request");
}

//Update the object
item.userImages = someArrayOfImageReferences;

error = nil;
if (![context save:&error]) {
    NSLog(@"error");
}

But how do I construct someArrayOfImageReferences? Do I have to create another array of arbitrary image names like image1.png, image2.png, etc? Seems like there should be a better way.

1
  • Surely if you are saving them to disk you already have their names somewhere? If not, then when you save the images, you should store the paths in an array so that you can find them again later when you need to read them from disk (after the file names are loaded from core data). Commented Sep 7, 2012 at 4:19

1 Answer 1

0

The set of images is actually not an Array in Core Data, but rather it should by an NSSet. Just copy over your object pointers from your array into a new NSSet and you'll be fine.

Even the images all have file names, dont't use those. A better way would be to generate names on the fly, at least for internal use. You can, of course, keep the user facing names around if necessary. For your internal names, use something based off of a GUID, which can be generated on the fly, or perhaps something based off of the current system time. (Be sure to check for existing files and adjust the name accordingly, in case the user changes their system's time.)

Sign up to request clarification or add additional context in comments.

1 Comment

Core Data doesn't have NSArray support. Instead it's native datatypes are NSSet and as of iOS 5 NSOrderedSet. Not quite sure what the difference would be here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.