Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Im getting a rather baffling error when trying to get an object from an
NSDictionary
. I get an
EXC_BAD_ACCESS
when calling
objectForKey
. When I look at the given objects, everything is fine. The dictionary exists. It contains the key I am looking for. The key is also there. Everything is totally fine. So why does
objectForKey
crash on this occasion? Ive taken a screenshot of both the code and of the console - you can see the properties in question in the print out. The key is there, and the dictionary contains that key. But its as though the dictionary doesnt exist or something? Baffled. By the way I 'copy'd the dictionary in an attempt to fix the crash, I wouldnt normally do that.
–
–
–
–
The problem here was todo with threading. As I was correctly reminded in comments, NSMutableDictionary is not thread safe. It was being updated on a background thread, and the code above was being called on the main thread. The simplest solution was to wrap these calls in @synchronised. Thanks to all those who chipped in to help.
if ([update uniqueId]) {
@synchronized (self.downloadProgress) {
if ([self.downloadProgress objectForKey:[update uniqueId]]) {
NSDictionary *progressInfo = [[self.downloadProgress objectForKey:[update uniqueId]] copy];
if ([progressInfo objectForKey:@"progressString"]) {
return [progressInfo objectForKey:@"progressString"];
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.