Narek  Simonyan
Narek Simonyan

Reputation: 612

High memory usage when calling to core data

I have a problem.I have saved images in the core data.I have a TableView and in every cell of that table I have an image,which loads from the core data.

Here are codes.

In this part of my code,I get data for images for every cell

   if let img = CoreDataFunctions.loadImageFromCache(imgURL){
                cell.PersonImage?.image = UIImage(data:img)
            }

It is CoreDataFunctions.loadImageFromCache method

class func loadImageFromCache(selfiePath:String?) -> NSData? {
        let managedObjectContext: NSManagedObjectContext = AppDelegate().managedObjectContext
        let fetchRequest = NSFetchRequest(entityName: "Images")
        fetchRequest.predicate = NSPredicate(format: "selfie == %@", selfiePath!)

        if let fetchResults = try! managedObjectContext.executeFetchRequest(fetchRequest) as? [Images] {
            if fetchResults.count == 1 {
                return fetchResults[0].imageData
            }
        }
        return nil
    }

When I finish the loading of the images, the Memory Usage becomes too high and stays on that position.Can anyone tell me the reason ???

Upvotes: 1

Views: 344

Answers (1)

Mundi
Mundi

Reputation: 80265

1) Use a NSFetchedResultsController to display the images in a table view. You will then have a significantly lower memory footprint. You can also eliminate your expensive fetch method.

2) Check "External Storage" for your data attribute in the model editor to make sure the images are not actually stored in the database. Alternatively, devise your own storage/naming scheme and store the images in the applications documents directory.

Upvotes: 3

Related Questions