0

When i am storing larger size images in core data using for loop i am receiving memory warning message by the didReceiveMemoryWarning method when iteration count is 300. Now based on memory warning i can display the user with the alert that "memory is fully please sync your images". But my problem is i am unable to get memory warning greater that 300. i.e i am getting memory warning exactly for 300th iteration.above 300 and below 300 i am not getting memory warning.

this is code which i used

for (int i=0;i<=300;i++)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *persistentStorePath = [documentsDirectory stringByAppendingPathComponent:@"DetailsRegister.sqlite"];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"EMpDetails" inManagedObjectContext:context];

         NSManagedObject *newDevice=[[NSManagedObject alloc]initWithEntity:entity insertIntoManagedObjectContext:context];
         UIImage *image = [UIImage imageNamed:@"image.png"];
        imageview.image=image;
        [self SaveImage:image];
        dataImage = UIImageJPEGRepresentation(image, 0.0);

       [newDevice setValue:dataImage forKey:@"image"]; // obj refers to NSManagedObject

            error = nil;
            // Save the object to persistent store
            if (![context save:&error]) {
                NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
            }

2 Answers2

3

CoreData isn't really an ideal place to store image data.

I tend to just store the imageData (or just actual images if they are not sensitive) in the documents folder and then just store an imageURL against the persisted object.

That way you can just return the Image for a URL that way, much better performance.

enter image description here

Callum Boddy
  • 407
  • 2
  • 5
  • is there any limit to data store in core data?.if there is a limit than what is limit of core data and how to show alert message when core data is full – sanginadham murali Mar 10 '15 at 11:31
  • I don't this there is a limit, I think the limit is the GB left on the device. an alert view will come from the system when the device is running low on space. – Callum Boddy Mar 10 '15 at 11:35
0

Again drawing on Marcus S. Zarra's Core Data: Data Storage and Management for iOS, OS X, and iCloud (2nd edition), one recommendation is as follows:

  • Small Binary Data [...] anything smaller than 100 kilobytes [...] When working with something this small, it is most efficient to store it directly as a property value in its corresponding table.
  • Medium Binary Data [...] anything larger than 100 kilobytes and smaller than 1 megabyte in size [...] Data of this size can also be stored directly in the repository. However, the data should be stored in its own table on the other end of a relationship with the primary tables.
  • Large Binary Data [...] greater than 1 megabyte in size [...] Any binary data of this size should be stored on disk as opposed to in the repository. When working with data of this size, it is best to store its path information directly in the primary entity [...] and store the binary data in a known location on disk (such as in the Application Support subdirectory for your application).

For more details, you should get the book (disclosure: not affiliated).

Community
  • 1
  • 1
Drux
  • 11,992
  • 13
  • 66
  • 116