ios - 如何在 iOS 中修复 NZombie

标签 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] 是僵尸的责任

这是我的代码:

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

在此先感谢您的支持

最佳答案

不要那样线程,太容易搬起石头砸自己的脚了。创建一个 NSOperation 子类来进行绘图。制作一个接受数组的自定义初始化程序。将数组与操作对象一起存储,这样您就知道它不会被释放给您。与 detachNewThreadSelector 相比,它多了几行代码,但它更安全。另外,如果用户导航离开您正在显示的 View ,您可以取消操作。

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

关于ios - 如何在 iOS 中修复 NZombie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18091855/

相关文章:

xcode - 无法在 Xcode : CocoaPods linking error 上存档

ios - 如何在自定义的 UIBarButtonItem 上放置角标(Badge)

swift - 无法加载从带有标识符的包中的 Nib 引用的 “” 图像

swift - 以下代码中僵尸的原因是什么

ios - 一起滚动 2 个组件时 UIPickerView 崩溃

ios - 核心数据管理对象上下文保存

ios - 在后台的预定点播放文本到语音

ios - 在 NSURLSessionData 的实现中未调用委托(delegate)方法

ios - 从服务器加载 10,000 多行的 UITableView 的正确方法是什么?

ios - UITapGestureRecognizer 导致崩溃并显示不同的错误消息