如何在iOS中修复NZombie

时间:2013-08-06 22:50:46

标签: ios xcode nszombies

我在iOS开发应用

enter image description here

此应用程序捕获签名并从数据库中重新绘制它们......

当我添加线程时出现问题,在绘制签名时实时在标签上打印生物识别数据,随机崩溃,我的意思是有时它会完成签名,有时只有签名的3/4甚至不是它的一半......

使用名为Instruments的工具试图检测NSZombies,这就是我得到的......

enter image description here

enter image description here

并查看代码,这是我找到的代码部分......

enter image description here

这是崩溃的代码片段:

-(void)imprimeValoresFirmaEnTabla:(NSMutableArray *)valores{
    if ([valores count] == 3) {
        if ([valores objectAtIndex:0] != nil) self.pressureStrokeLabel.text = [valores objectAtIndex:0];
        if ([valores objectAtIndex:1] != nil) self.speedStrokeLabel.text    = [valores objectAtIndex:1];
        if ([valores objectAtIndex:2] != nil) self.locationStrokeLabel.text = [valores objectAtIndex:2];        
    }
}

它说 - [NSConcreteMutableAtributedString lenght]是Zombie的负责人

这是我的代码:

- (IBAction)drawSignature{

    [self clear]; // clear the canvas
    NSData  *datos  = [self desSerializarFirma:[self traerFirmadeBD]]; //unpack the registered signature and store it on the NSData object

    if (datos.length <= 1) // if there's no data stored on database
        [[[UIAlertView alloc]initWithTitle:@"Notification" message:@"For showing your signature, it is required to register it first, draw your signature on the canvas and tap the Save button" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];

    if (datos.length > 1) { // if is data stored on database

        //store the signature on the object
        self.firmaCompleta  = [NSKeyedUnarchiver unarchiveObjectWithData:datos];


        //this loop draws each point on the canvas
        for (int i = 0; i < [self.firmaCompleta.location count]; i++) {


            SmoothStroke *currentStroke = [canvasView getStrokeForTouchHash:[[self.firmaCompleta.touchHash objectAtIndex:i] intValue]];

            //this draws each point using the biometric info
            [canvasView addLineToAndRenderStroke:currentStroke
                                         toPoint:CGPointFromString([self.firmaCompleta.location objectAtIndex:i])
                                         toWidth:[canvasView widthForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]
                                         toColor:[canvasView colorForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]];



            //this stores the biometric data in an array for sending to background thread
            if (!valoresFirma)  valoresFirma = [[NSMutableArray alloc]init];
            [valoresFirma removeAllObjects];

            [valoresFirma addObject:[NSString stringWithFormat: @"%f", [canvasView widthForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]]];
            [valoresFirma addObject:[NSString stringWithFormat:@"%f",velocidadTrazo]];
            [valoresFirma addObject:[self.firmaCompleta.location objectAtIndex:i]];


            //this triggers the tread for printing the values on labels (real time)          
            [NSThread detachNewThreadSelector:@selector(imprimeValoresFirmaEnTabla:) toTarget:self withObject:valoresFirma];

        }
    }
}

和线程调用的代码:

-(void)imprimeValoresFirmaEnTabla:(NSMutableArray *)valores{
    if ([valores count] == 3) {
        if ([valores objectAtIndex:0] != nil) self.pressureStrokeLabel.text = [valores objectAtIndex:0];
        if ([valores objectAtIndex:1] != nil) self.speedStrokeLabel.text    = [valores objectAtIndex:1];
        if ([valores objectAtIndex:2] != nil) self.locationStrokeLabel.text = [valores objectAtIndex:2];        
    }
}

提前感谢您的支持

1 个答案:

答案 0 :(得分:3)

不要那样做穿线,在脚上射击自己太容易了。创建一个NSOperation子类来进行绘制。创建一个带有数组的自定义初始化程序。使用操作对象存储数组,以便您知道它不会被释放。与detachNewThreadSelector相比,这是一些额外的代码行,但它更安全。此外,如果用户离开您正在显示的视图,您可以取消操作。

在此处阅读操作和操作队列: http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091