Как исправить NZombie в iOS

Я разрабатываю приложение в iOS

это приложение захватывает подписи, а также перерисовывает их обратно из базы данных...

Проблема возникла, когда я добавил нити, для печати на этикетках биометрических данных в режиме реального времени во время рисования подписи, случайного сбоя, я имею в виду, что иногда она завершает подпись, иногда только 3/4 подписи или даже не половина этого....

Использовал инструмент под названием Instruments, пытающийся обнаружить NSZombies, и вот что я получил...

и глядя в коде, это часть кода, которую я нашел...

это кусок кода, где он падает:

-(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 длина] является ответственным за зомби

это мой код:

- (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 ответ

Не делай так нанизывания, слишком легко выстрелить себе в ногу. Создайте подкласс NSOperation для рисования. Сделайте пользовательский инициализатор, который принимает массив. Сохраните массив с объектом операции, чтобы вы знали, что он не будет выпущен для вас. Это несколько дополнительных строк кода по сравнению с detachNewThreadSelector, но это гораздо безопаснее. Кроме того, вы можете отменить операцию, если пользователь отклоняется от отображаемого вами представления.

Читайте об операциях и очередях операций здесь: http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091

Другие вопросы по тегам