简体   繁体   中英

The fast way to compere Array with different objects type

I have a one array with fetch results from coredata and it's contain object myObject with property myObjId .

And i have MKAnnotation on my MapView , so I have an array with custom annotation with Id :

self.mapView.annotations return object CustomPinAnnotation with myId property.

What i want to do is sync this two results without reloading existing pins on the map. How to find data in annotations array which is NOT in the results array from CoreData?

I wrote that method to check right before annotation will be added:

-(BOOL)isAlreadyDisplayOnMap:(NSNumber*)pinId {
    for(CustomPinAnnotation *an in self.mapView.annotations) {
        if(![an isKindOfClass:[MKUserLocation class]]) {
            if([an.favourId integerValue] == [pinId integerValue]) {
                return YES;
            }
        }
    }
    return NO;
}

But this is terribly slow and should be a better way to compare arrays.

You may want to try using block:

     __block BOOL alreadyDisplayed;
    [self.mapView.annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       CustomPinAnnotation *an = (CustomPinAnnotation *)obj;
          if(![an isKindOfClass:[MKUserLocation class]]) {
             if([an.favourId integerValue] == [pinId integerValue]) {
                alreadyDisplayed = YES;
                *stop = YES;
             }
          }
    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM