Domanda

I have this method that is not working the way I need. It create a MKPolyline "animated": let's say I have 85 points. Each polyline that connects these points is created 0.1 second after the previous. For this, it call itself through performSelector:withObject:afterDelay:.

-(void)addOverlaysFromPointsWithStartFromAndPoints:(NSArray *)arguments
{

    NSLog(@"end: %@ / coordinates count: %d",[arguments objectAtIndex:0],[[arguments objectAtIndex:1] count]);

    CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D)*2);
    Location *loc1 = (Location *)[[arguments objectAtIndex:1] objectAtIndex:[(NSNumber *)[arguments objectAtIndex:0] intValue]-1];
    Location *loc2= (Location *)[[arguments objectAtIndex:1] objectAtIndex:[(NSNumber *)[arguments objectAtIndex:0] intValue]];
    locations[0] = loc1.location;
    locations[1] = loc2.location;
    routeLine = [MKPolyline polylineWithCoordinates:locations count:2];

    [self.map addOverlay:routeLine];

    if(([(NSNumber *)[arguments objectAtIndex:0] intValue]+1) < [[arguments objectAtIndex:1] count])//add more overlays after delays unless this is the endpoint
    {
        NSArray *parameters = [NSArray arrayWithObjects:[NSNumber numberWithInt:[(NSNumber *)[arguments objectAtIndex:0] intValue] + 1],[arguments objectAtIndex:1],nil];
        [self performSelector:@selector(addOverlaysFromPointsWithStartFromAndPoints:) withObject:parameters afterDelay:0.1];
    }

}

In my case, I have kind of 3 subroutes to be created, so initially I have arrays with 7, 68 and 85 indexes each one. Check this log:

2013-02-21 10:31:22.372 SIGView[329:907] end: 1 / coordinates count: 7
2013-02-21 10:31:22.433 SIGView[329:907] end: 1 / coordinates count: 68
2013-02-21 10:31:22.528 SIGView[329:907] end: 1 / coordinates count: 85
2013-02-21 10:31:22.541 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.542 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.650 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.653 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.655 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.796 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.798 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.801 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.898 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.905 SIGView[329:907] end: 5 / coordinates count: 85
2013-02-21 10:31:22.909 SIGView[329:907] end: 5 / coordinates count: 85
2013-02-21 10:31:23.013 SIGView[329:907] end: 5 / coordinates count: 85
(...)

I seems like when the method is first called for each array, it works fine but then it assumes the last array (that contains 85 objects). So, i have only one "route" created and animated.

The log should look something like:

2013-02-21 10:31:22.372 SIGView[329:907] end: 1 / coordinates count: 7
2013-02-21 10:31:22.433 SIGView[329:907] end: 1 / coordinates count: 68
2013-02-21 10:31:22.528 SIGView[329:907] end: 1 / coordinates count: 85
2013-02-21 10:31:22.541 SIGView[329:907] end: 2 / coordinates count: 7
2013-02-21 10:31:22.542 SIGView[329:907] end: 2 / coordinates count: 68
2013-02-21 10:31:22.650 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.653 SIGView[329:907] end: 3 / coordinates count: 7
2013-02-21 10:31:22.655 SIGView[329:907] end: 3 / coordinates count: 68
2013-02-21 10:31:22.796 SIGView[329:907] end: 3 / coordinates count: 85

I hope you understand the problem and help how to get this fixed.

Thanks in advance!

EDIT

Here is how the method is called:

NSArray *argumentsToOverlay = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],locationsToLoadRegion, nil];

[self addOverlaysFromPointsWithStartFromAndPoints:argumentsToOverlay];

This part of the code is called three times when the locationsToLoadRegion array contains respectively 7, 68 and 85 objects.

The problem start when performSelector: is called the first time.

EDIT

- (void)createPathWithCoordinates:(NSMutableArray *)coordinates{

    int count;

// This for run through all sub routes
    for (NSArray *trackings in coordinates) {
        [locationsToLoadRegion removeAllObjects];

        count = [trackings count];

        if (count > 1){

            // This for save the coordinates available for sub route
            for (NSInteger index = 0; index < count; index++)
            {
                Location *tempLocation = [trackings objectAtIndex:index];
                [locationsToLoadRegion addObject:tempLocation];
            }

            NSArray *argumentsToOverlay = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],locationsToLoadRegion, nil];

            [self addOverlaysFromPointsWithStartFromAndPoints:argumentsToOverlay];

        }

    }
}
È stato utile?

Soluzione

Try this:

NSArray *argumentsToOverlay = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [[locationsToLoadRegion copy] autorelease],
    nil];

Altri suggerimenti

    NSArray *parameters = [NSArray arrayWithObjects:...];
    [self performSelector:... withObject:parameters ...];

You create an autoreleased array (parameters) which will be destroyed before the next call of the function. It's a mere coincidence that you still get usable data instead of an exception/crash.

A solution would be to retain the parameters as properties of the calling class, for example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top