I'm having a memory issue that leads to a crash. I am looping through an array of dictionaries and then inside that I loop through a keys array that I created. I use each key in that keys array to get the value of that key in the dictionary. I then create a string by appending the values. This string will contain a large amount of data.
I am also using ARC so I can't manually release.
The memory spike happens on the stringByAppendingFormat line.
NSString *theString = [[NSString alloc] init];
for (NSMutableDictionary *aDict in collectionArray)
{
for (NSString *key in itemKeys)
{
NSString *valueString = [aDict valueForKey:key];
// Memory spikes here
theString = [theString stringByAppendingFormat:@"%@,", valueString];
}
}
Instead of NSString, you should use an NSMutableString. Try this:
NSMutableString *theString = [[NSMutableString alloc] init];
for (NSMutableDictionary *aDict in collectionArray)
{
for (NSString *key in itemKeys)
{
NSString *valueString = [aDict valueForKey:key];
// Memory spikes here
[theString appendFormat:@"%@,", valueString];
}
}
Edit:
This will probably solve your problems if your dictionary and the lengths of the itemKeys are not particularly large. However, if they are large you'll need to use an autoreleasepool in your loop like they do here: https://stackoverflow.com/a/7804798/211292 Also, consider Tommy's change if all you are doing is separating the values by commas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With