performSelector:withObject:无法正常工作

时间:2013-02-21 13:48:08

标签: iphone ios objective-c mkoverlay

我有这种方法不能按我需要的方式工作。它创造了一个MKPolyline“动画”:让我说我有85分。连接这些点的每条折线在前一个之后创建0.1秒。为此,它通过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];
    }

}

在我的情况下,我有3种子路由要创建,所以最初我有每个都有7,68和85个索引的数组。检查此日志:

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
(...)

我似乎首次为每个数组调用该方法时,它工作正常,但它假设最后一个数组(包含85个对象)。所以,我只创建了一个“路线”并制作了动画。

日志应该类似于:

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

我希望你能理解这个问题并帮助解决这个问题。

提前致谢!

修改

以下是调用该方法的方法:

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

[self addOverlaysFromPointsWithStartFromAndPoints:argumentsToOverlay];

locationsToLoadRegion数组分别包含7,68和85个对象时,这部分代码被调用三次。

问题在第一次调用performSelector:时开始。

修改

- (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];

        }

    }
}

2 个答案:

答案 0 :(得分:0)

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

您创建一个自动释放的数组(参数),在下次调用该函数之前将对其进行销毁。只是巧合,您仍然可以获得可用数据而不是异常/崩溃。

解决方案是将参数保留为调用类的属性,例如。

答案 1 :(得分:0)

试试这个:

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