iphone - How to add annotations to MKMapView asynchronously? -
i have lot of annotations added mkmapview. when add annotations, app freezes short period of time. have understand main thread thread allowed add ui view, if true, how make operation not freeze app?
// in viewdidload (nsmanagedobject *object in requestresults) { customannotation *customannotation = [[customannotation alloc] init]; customannotation.title = object.title; customannotation.coordinate = cllocationcoordinate2dmake([object.latitude doublevalue], [object.longitude doublevalue]); [_mapview addannotation:customannotation]; } } // end viewdidload - (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { // shows annotation custom image mkpinannotationview *custompinview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"mapannotation"]; customannotation *customannotation = (id) annotation; custompinview.image = [uiimage imagenamed:@"green"]; return custompinview; }
you can use grand central dispatch - gcd doing this.
try with:
- (void)viewdidload { dispatch_async(dispatch_get_global_queue(0, 0), ^{ (nsmanagedobject *object in requestresults) { customannotation *customannotation = [[customannotation alloc] init]; customannotation.title = object.title; customannotation.coordinate = cllocationcoordinate2dmake([object.latitude doublevalue], [object.longitude doublevalue]); dispatch_async(dispatch_get_main_queue(), ^{ [_mapview addannotation:customannotation]; }); } }); } this nice tutorial: gcd , threading
Comments
Post a Comment