为什么VerificationController在didReceiveData中崩溃

时间:2013-05-09 17:19:29

标签: ios objective-c cordova automatic-ref-counting

这是崩溃的功能。确切的代码行是:removeObjectForKey。即使测试函数完全为空,它也会在removeObjectForKey上崩溃。注意:我只是传入一个空函数回调。目前,我已关闭ARC,是否需要打开它?如果可能的话,我想用ARC关闭它,因为打开它意味着处理很多编译问题。

该函数确实说明了非保留对象,因此可能是内存问题。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    // So we got some receipt data. Now does it all check out?
    BOOL isOk = [self doesTransactionInfoMatchReceipt:responseString];

    VerifyCompletionHandler completionHandler = _completionHandlers[[NSValue valueWithNonretainedObject:connection]];
    [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];

    if (isOk)
    {
        //Validation suceeded. Unlock content here.
        NSLog(@"Validation successful");
        completionHandler(TRUE);

    } else {
        NSLog(@"Validation failed");
        completionHandler(FALSE);
    }
}

这是verifyController的用法:

    [[VerificationController sharedInstance] verifyPurchase:transaction completionHandler:^(BOOL success) {
        if (success) {

            NSLog(@"Hi, its success.");
            [self testMethod];

       } else {
            NSLog(@"payment not authorized.");
        }
    }];         
}

- (void) testMethod {

}

我可以使用__weak然后我必须打开ARC,我试图避免。注意:当我将它放在其他类/对象中时,verificaitionController会工作,但是只要我把它放在InAppPurchaseManager中,它就会在它尝试访问self时随时爆炸。 Self指向像这样定义的InAppPurchaseManager实例(它是一个phonegap插件):

@interface InAppPurchaseManager : CDVPlugin <SKPaymentTransactionObserver> {

}

4 个答案:

答案 0 :(得分:3)

我也遇到了这个问题我用这种方式解决了这个问题 主要问题是当你在verifyPurchase方法中设置值为完成处理程序时,它设置为nil值,所以在verifyPurchase方法中找到这一行

  _completionHandlers[[NSValue valueWithNonretainedObject:conn]] = completionHandler;

并将其替换为

 [_completionHandlers setObject:[completionHandler copy] forKey:[NSValue valueWithNonretainedObject:conn]];

找到connectionDidReceivedata方法并将其替换为

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    // So we got some receipt data. Now does it all check out?
    BOOL isOk = [self doesTransactionInfoMatchReceipt:responseString];


    if (_completionHandlers && [_completionHandlers respondsToSelector:@selector(removeObjectForKey:)])
    {
        VerifyCompletionHandler completionHandler = _completionHandlers[[NSValue valueWithNonretainedObject:connection]];
        [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
        if (isOk)
        {
            //Validation suceeded. Unlock content here.
            NSLog(@"Validation successful");
            completionHandler(TRUE);

        } else {
            NSLog(@"Validation failed");
            completionHandler(FALSE);
        }

    }
    //[_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
   }

希望这可以帮助你并节省大量时间。

答案 1 :(得分:0)

_completionHandlers是否为零?你可能会做这样的事情 -

if (_completionHandlers && [_completionHandlers respondsToSelector:@selector(removeObjectForKey:)]) {
    [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
}
祝你好运。

答案 2 :(得分:0)

查找以下字符串:

[_completionHandlers setObject:completionHandler forKey:[NSValue valueWithNonretainedObject:conn]];

改为:

[_completionHandlers setObject:[completionHandler copy] forKey:[NSValue valueWithNonretainedObject:conn]];

答案 3 :(得分:0)

我不知道你是否找到了答案,但我刚刚意识到_completionHandlers永远不会被分配(如果你在设置断点后设置了_completionHandlers,你会发现它是零)。希望这有帮助!

// in VerificationController.m

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        transactionsReceiptStorageDictionary = [NSMutableDictionary dictionary];
        _completionHandlers = [NSMutableDictionary dictionary];
    }
    return self;
}