0

I have been trying to copy a dictionary to an NSMutableDictionary in a block.

I have tried:

myMutableDictionary = [immutableDictionary copy]
     "       "      = [immutableDictionary mutableCopy]
     "       "      = [NSMutableDictionary alloc [initWithDictionary: immutableDictionary]

However, each time I try this, I get the EXC_BAD_ACCESS (code=2…)

I suspect this has something to do with iterations within the completionHandler, but cannot figure another way to structure this.

Any idea what I am doing wrong?

Thanks for your help.

- (void) getCoordinatesForTheseLocations:(NSArray*) meetRecords
{
NSInteger countOfAnnotations = [meetRecords count];

if (debug==1) NSLog(@"countOfAnnotations equals %d", countOfAnnotations);
int __block iCount = 0;
for (__block NSDictionary* meet in meetRecords) {

    if ([[meet objectForKey:MEET_STATE_ABBREV] isEqualToString:@"OR"] || [[meet objectForKey:MEET_STATE_ABBREV] isEqualToString:@"WA"]) {
        NSString *location = [NSString stringWithFormat:@"%@, %@, %@",
                              [[meet objectForKey:MEET_VENUE] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                              [[meet objectForKey:MEET_CITY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                              [[meet objectForKey:MEET_STATE_ABBREV] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location completionHandler:^(NSArray *placemarks, NSError *error) {
            CLLocationCoordinate2D coordinates;
            CLPlacemark* bestGuess;

            if (error)
            {
                NSString *message;
                switch ([error code])
                {
                    case kCLErrorGeocodeFoundNoResult:
                        message = @"kCLErrorGeocodeFoundNoResult";
                        break;
                    case kCLErrorGeocodeCanceled:
                        message = @"kCLErrorGeocodeCanceled";
                        break;
                    case kCLErrorGeocodeFoundPartialResult:
                        message = @"kCLErrorGeocodeFoundNoResult";
                        break;
                    default:
                        message = [error description];
                        break;
                }

                NSLog(@"Error of %@ for %@", message, location);
            } else if(placemarks && placemarks.count > 0) {

               NSMutableDictionary* meetInfoForAnnotation = [[NSMutableDictionary alloc] init];

                meetInfoForAnnotation = [meet copy];

                if (debug==1) NSLog(@"Received placemarks: %@", placemarks);

                bestGuess = [placemarks objectAtIndex:0];
                coordinates.latitude = bestGuess.location.coordinate.latitude;
                coordinates.longitude = bestGuess.location.coordinate.longitude;

                [meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.longitude] forKey:MEET_LONGITUDE];
                [meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.latitude] forKey:MEET_LATITUDE];

                [self mapAnnotations:meet];

            }
            if (debug==1) NSLog(@"iCount equals %d", iCount);

            if (iCount == countOfAnnotations) {

                dispatch_async(dispatch_get_main_queue(), ^{

                    //            NSLog(@"%@",meetsWithCoordinates);
                    myPinColor = MKPinAnnotationColorPurple;

                    [self setAnnotations:self.meetAnnotations];
                });
            }

        }];
        iCount ++;
    }
}

}

Here is part of the error log.

libsystem_platform.dylib`OSAtomicCompareAndSwap32Barrier:
0x38973a28:  ldrex  r9, [r2]

libsystem_platform.dylib`OSAtomicCompareAndSwapPtrBarrier + 4:
0x38973a2c:  movs   r3, #0
0x38973a2e:  cmp    r9, r0
0x38973a30:  bne    0x38973a5         ; OSAtomicCompareAndSwapPtrBarrier + 40
0x38973a32:  dmb    ishst
0x38973a36:  strex  r3, r1, [r2]        Thread 1: EXC_BAD_ACCESS (code = address=0x2dc078ac) 
0x38973a3a:  cmp    r3, #0
0x38973a3c:  beq    0x38973a4a        ; OSAtomicCompareAndSwapPtrBarrier + 34
0x38973a3e:  ldrex  r9, [r2]
0x38973a42:  movs   r3, #0
0x38973a44:  cmp    r9, r0
0x38973a46:  bne    0x38973a50        ; OSAtomicCompareAndSwapPtrBarrier + 40
0x38973a48:  b      0x38973a36        ; OSAtomicCompareAndSwapPtrBarrier + 14
0x38973a4a:  movs   r3, #1
0x38973a4c:  dmb    ish
0x38973a50:  mov    r0, r3
0x38973a52:  bx     lr
PhillipOReilly
  • 609
  • 12
  • 28
  • 1
    Why do you have `__block` in your for loop? You don't need that. – Aaron Brager Feb 22 '14 at 01:13
  • Also, your `iCount` logic assumes that tasks are completed in the order they were started, which I believe is wrong. (You should increment `iCount` when a task *finishes*, not when it *starts*.) – Aaron Brager Feb 22 '14 at 01:14
  • Aaron, the __block is the problem! As you may have discerned I am struggling with this completionHandler component. I now have MKAnnotations being passed into my MapViewController in proper format but cannot get mapView to update, but I digress! If you put your comment in form of an answer, I'll check it. Thanks for your help. – PhillipOReilly Feb 22 '14 at 01:28
  • Yeah, your only block-altered variable is myPinColor. – AMayes Feb 22 '14 at 02:04

0 Answers0