Inderpal Singh Inderpal Singh - 2 months ago 8
iOS Question

How to solve Memory management issue in ios

I have 5000 Object from an array.When i execute the loop then my app crashed due to memory Issue.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableString *stringToWrite = [[NSMutableString alloc] init];
[stringToWrite appendString:[NSString stringWithFormat:@"First Name,Last Name,Full Name,Phone Number, Email, Birthday,Job, organizationName,Note\n\n"]];
for(int i = 0 ;i<[appDelegate.sortedFilterArray count];i++) {
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"firstName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"lastName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"userName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"phoneNumber"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"emailAddress"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"birthday"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"jobTitle"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"organizationName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[appDelegate.sortedFilterArray valueForKey:@"note"] objectAtIndex:i]]];
}
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.csv"];
[stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];

Answer

You need to change your for loop like this

for(int i = 0 ;i<[appDelegate.sortedFilterArray count];i++)
{
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray objectAtIndex:i] objectForKey:@"firstName"]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"lastName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"userName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"phoneNumber"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"emailAddress"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"birthday"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"jobTitle"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"organizationName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[appDelegate.sortedFilterArray valueForKey:@"note"] objectAtIndex:i]]];
}