7/04/2014

Equivalence of CLRegion

To see how the management of CLRegion by CLLocationManager,
I wrote below codes.
I found that CLRegion(s) that have same identifier isEqual.

*1
{
    // different parameters, same identifiers
    CLCircularRegion *region1 = [[CLCircularRegion allocinitWithCenter:CLLocationCoordinate2DMake(1020)
                                                                  radius:30
                                                              identifier:@"test"];
    CLCircularRegion *region2 = [[CLCircularRegion allocinitWithCenter:CLLocationCoordinate2DMake(4050)
                                                                  radius:60
                                                              identifier:@"test"];
    NSLog(@"regions are equal = %d", [region1 isEqual:region2]);
    // regions are equal = 1
}
{
    // same parameters, different identifiers
    CLCircularRegion *region1 = [[CLCircularRegion allocinitWithCenter:CLLocationCoordinate2DMake(1020)
                                                                  radius:30
                                                              identifier:@"test"];
    CLCircularRegion *region2 = [[CLCircularRegion allocinitWithCenter:CLLocationCoordinate2DMake(1020)
                                                                  radius:30
                                                              identifier:@"tes"];
    NSLog(@"regions are equal = %d", [region1 isEqual:region2]);
    // regions are equal = 0
}


So codes that remove a region has same identifier from mutable array or set are below. See simple way.

*2
    // remove region with same identifier from array
    NSArray *regions;
    CLRegion *regionToRemove;

    NSMutableArray *mArray = regions.mutableCopy;
    // normal way
    for (CLRegion *region in regions) {
        if ([region.identifier isEqualToString:regionToRemove.identifier]) {
            [mArray removeObject:region];
        }
    }
    
    // simple way
    [mArray removeObject:regionToRemove];

Conversely you may need to be careful about this behavior in certain situation.


CLLocationManager による CLRegion の管理の仕方を見てもしやと思いテストコードを書いてみると、同じ identifier の CLRegion は isEqual という事が分かった。
*1

という事で、Mutableな配列やセットから同じ identifier の region を削除するには単にこう書けばいい。simple wayの方を参照。
*2

逆に言うと、Mutableな配列から region を削除する時はこの挙動を気を付けなければならないケースがあるかも知れない。

No comments:

Post a Comment