0
-(void) objectParsed_ListAllMedia:(NSDictionary *)dictionary
{
@try {

        self.viewLoading.hidden=1;

        [self.arrGaalleryMediaName removeAllObjects];
        [self.arrMediaNames removeAllObjects];


        if(self.arrOnlyServerImages == nil){
            self.arrOnlyServerImages = [[NSMutableArray alloc] init];
        }

        if([self.arrOnlyServerImages count] >0){
            [self.arrOnlyServerImages removeAllObjects];
        }


        if (dictionary==nil) {

            [self.gridCollectionView reloadData];
            return;
        }
        // Filter Array for Audio file

        NSPredicate *predicate = [NSPredicate predicateWithFormat: @"type != 'audio' "];
        self.arrOnlyServerImages = [NSMutableArray arrayWithArray:[[dictionary objectForKey:@"objects"] filteredArrayUsingPredicate:predicate]];



        // Remove duplicate Start  //Read Meta Data and Duplicate from Download, Duplicate from upload   START

        dispatch_queue_t backgroundQueue = dispatch_queue_create("com.memreas.myqueue", 0);

        dispatch_async(backgroundQueue, ^{

            NSMutableArray *arr = [NSMutableArray array];
            NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];

            for (int i=0; self.assetAry.count>i; i++) {

                ALAsset *result =self.assetAry[i];
                ALAssetRepresentation *imageRep = [result defaultRepresentation];
                NSDictionary * customMetaDic = [imageRep metadata][(NSString*)kCGImagePropertyIPTCDictionary];

                if (customMetaDic) {

                    [self.arrMediaNames addObject:customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]?customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]:@""];
                }else{
                    [self.arrMediaNames addObject:@""];
                }

                [self.arrGaalleryMediaName addObject:[self getFileNameWithExtensionFromPath:imageRep.url]];

            }


            for (int i=0; self.arrOnlyServerImages.count>i; i++) {

                NSMutableDictionary* obj =  self.arrOnlyServerImages[i];
                NSMutableDictionary * dic2 = [NSMutableDictionary dictionaryWithDictionary:obj];
                BOOL isArrMedia =[self.arrMediaNames containsObject:dic2[@"media_name"]];
                BOOL isGallery =[self.arrGaalleryMediaName containsObject:dic2[@"media_name"]];

                if (isArrMedia||isGallery) {

                    dic2[@"isDownloaded"] = [NSNumber numberWithBool:YES];
                    [indexSet addIndex: isArrMedia?[self.arrMediaNames indexOfObject:dic2[@"media_name"]] :[self.arrGaalleryMediaName indexOfObject:dic2[@"media_name"]]];

                }else{

                    dic2[@"isDownloaded"] = [NSNumber numberWithBool:NO];
                }

                [arr addObject:dic2];

            }

            dispatch_async(dispatch_get_main_queue(), ^{

                @try {

                    self.arrOnlyServerImages = arr;
                    [self.assetAry removeObjectsAtIndexes:indexSet];
                    [self.gridCollectionView reloadData];

                }
                @catch (NSException *exception) {
                    NSLog(@"%@",exception);
                    [self.gridCollectionView reloadData];
                }

            });


        });

        // Remove duplicate END  //Read Meta Data and Duplicate from Download, Duplicate from upload   END

        [self.gridCollectionView reloadData];
        [self.gridView.collectionView reloadData];
        [self.location performSelector:@selector(stopActivity) withObject:nil afterDelay:2];


    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }

}

I have issue with my code, While I run this code it generates memory pressure issue and crash the app.

Functionality is:

I load all the images from server and local assets and match each other with file name and remove duplicate images from list, So it will visible only once.

Any one have solution so please help.

thanks in advance.

Faizan Refai
  • 833
  • 10
  • 13
  • A quick look at your code - I see lots of `Mutable` variables there. Try to optimise your code and use immutable variable instead. – Kampai Oct 30 '14 at 06:25
  • I need that immutable element because requirement is some thing like that. I Need to remove object at indexes run time. – Faizan Refai Oct 30 '14 at 06:28
  • You can create temporary `mutable` variable and perform your operations. Also look at variable `dic2` - You are adding `mutable` dictionary in array. Instead create `NSDictionary` from `NSMutableDictionary` and remove `NSMutableDictionary` after adding it to array. – Kampai Oct 30 '14 at 06:34
  • NSmutable is occupied more memory then Immutable ? – Faizan Refai Oct 30 '14 at 06:36
  • This code works perfectly in simulator but it does not work in device. – Faizan Refai Oct 30 '14 at 06:39
  • Yes. Read this http://stackoverflow.com/questions/7071096/what-is-difference-between-mutable-and-immutable – Kampai Oct 30 '14 at 06:40
  • @Faizan- Check out my answer, this will explain to overcome from memory issues. http://stackoverflow.com/questions/26921227/table-view-images-never-being-released/26925176#26925176 – Kampai Nov 17 '14 at 14:17

1 Answers1

0

You're accessing memory-intensive things (ALAssetRepresentations) in a tight loop. In these cases a local autoreleasepool can help ARC to keep your memory use down.

Inside the loop where you pass through self.assetAry, wrap everything in an autoreleasepool like so:

@autoreleasepool {
    AlAsset *asset = ...
    ...
    // Rest of your code
}
jrturton
  • 118,105
  • 32
  • 252
  • 268