Reputation: 1230
I'm obtaining an image from Parse server, but when I receive it and store it in an NSMutableDictionary
, it fails to stay there when I check it.
NSString* messageSenderId = [receivedMessage objectForKey:@"Sender Id"];
NSLog(@"messageSenderId = %@", messageSenderId);
if([self.avatarDictionary objectForKey:messageSenderId] == nil) { // avatar image for sender not yet stored
PFQuery* userQuery = [PFUser query];
[userQuery whereKey:@"objectId" equalTo:messageSenderId];
PFUser* messageSender = (PFUser *)[[messageSenderQuery findObjects] firstObject];
PFFile *avatarImage = messageSender[@"profilePictureSmall"];
[avatarImage getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error) {
UIImage* storedImage = [UIImage imageWithData:data];
[self.avatarDictionary setObject:storedImage forKey:messageSender.objectId];
NSLog(@"objectForKey: %@", [self.avatarDictionary objectForKey:messageSender.objectId]);
[self.collectionView reloadData];
}];
}
When I check for the storedImage
variable I can see that it is not empty. But, the NSLog
reveals that there is no object for that key after I store it inside.
Upvotes: 0
Views: 94
Reputation: 26385
Sure that you can, but I would not.
Your problem can be due to the fact that you didn't initialize the mutable dictionary.
Images take a lot of memory and if you are storing a lot of images your app can be killed by the system.
A better approach could be save images locally and store the path to them in the mutable dictionary.
Or if you still want to store images in RAM, you can use NSCache. NSCache basically is a mutable dictionary, but it has a better memory management by evicting automatically resources and it is thread safe.
Upvotes: 2