Ios completionBlock错误处理

Ios completionBlock错误处理,ios,parsing,error-handling,Ios,Parsing,Error Handling,我正在尝试处理completionBlock错误,并在出现异常时捕获它 以下是我的代码: ParseOperation *parser = [[ParseOperation alloc] initWithData:self.appListData]; if([[ParseOperation alloc] initWithData:self.appListData] == nil) NSLog(@"[[ParseOperation alloc] initWithData:self.appL

我正在尝试处理
completionBlock
错误,并在出现异常时捕获它

以下是我的代码:

ParseOperation *parser = [[ParseOperation alloc] initWithData:self.appListData];
if([[ParseOperation alloc] initWithData:self.appListData] == nil)
    NSLog(@"[[ParseOperation alloc] initWithData:self.appListData] is nill");
__weak ParseOperation *weakParser = parser;
parser.completionBlock = ^(void) {
    if (weakParser.appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            rootViewController.entries = weakParser.appRecordList;
            if(weakParser.appRecordList == nil)
                 NSLog(@"weakParser.appRecordList is nill");
            if(weakParser.appRecordList != nil)
                NSLog(@"weakParser.appRecordList is Not nill");
            [rootViewController.tableView reloadData];
        });
    }
    self.queue = nil;
};
为了在控制台上获得错误结果,我在类中实现了以下内容:

parser.errorHandler = ^(NSError *parseError) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self handleError:parseError];
        NSLog(@"[self handleError:parseError] %@", parseError);
    });
};
weakParser.appRecordList
在大多数情况下(发生错误时)返回
nil


问题是否在
ParseOperation
类中?

问题是,您需要在weakParser之前添加\u块,否则,在运行完成块时,将不会引用weakParser。 请注意,变量在声明时分配给块,如果它们没有设置为\uuu block,它们的值将传递给块(与指针a.k.a.引用相反),因为在您的情况下,它是弱引用,当它到达方法底部时,它将被释放,block永远没有机会采取行动。 希望这有助于:


[编辑]p.S.:我不确定,但我认为从weakParser中删除\uu-weak也可以解决问题。

问题是,您需要在weakParser之前添加\uu块,否则,在运行完成块时,将不会引用weakParser。 请注意,变量在声明时分配给块,如果它们没有设置为\uuu block,它们的值将传递给块(与指针a.k.a.引用相反),因为在您的情况下,它是弱引用,当它到达方法底部时,它将被释放,block永远没有机会采取行动。 希望这有助于:


[编辑]p.S.:我不确定,但我认为从weakParser中删除\uu-weak也可以解决问题。

问题是,您需要在weakParser之前添加\uu块,否则,在运行完成块时,将不会引用weakParser。 请注意,变量在声明时分配给块,如果它们没有设置为\uuu block,它们的值将传递给块(与指针a.k.a.引用相反),因为在您的情况下,它是弱引用,当它到达方法底部时,它将被释放,block永远没有机会采取行动。 希望这有助于:


[编辑]p.S.:我不确定,但我认为从weakParser中删除\uu-weak也可以解决问题。

问题是,您需要在weakParser之前添加\uu块,否则,在运行完成块时,将不会引用weakParser。 请注意,变量在声明时分配给块,如果它们没有设置为\uuu block,它们的值将传递给块(与指针a.k.a.引用相反),因为在您的情况下,它是弱引用,当它到达方法底部时,它将被释放,block永远没有机会采取行动。 希望这有助于:


[编辑]p.S.:我不确定,但我认为从weakParser中删除\u-weak也可以解决这个问题。

我认为这是关于在块中使用appRecordList值:

id appRecordList = weakParser.appRecordList;
if (appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            rootViewController.entries = appRecordList;
            if(appRecordList== nil)
                 NSLog(@"weakParser.appRecordList is nill");
            if(appRecordList != nil)
                NSLog(@"weakParser.appRecordList is Not nill");
            [rootViewController.tableView reloadData];
        });
    }

我认为这是关于在块中使用弱,以下代码帮助分派方法传递
appRecordList
值:

id appRecordList = weakParser.appRecordList;
if (appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            rootViewController.entries = appRecordList;
            if(appRecordList== nil)
                 NSLog(@"weakParser.appRecordList is nill");
            if(appRecordList != nil)
                NSLog(@"weakParser.appRecordList is Not nill");
            [rootViewController.tableView reloadData];
        });
    }

我认为这是关于在块中使用弱,以下代码帮助分派方法传递
appRecordList
值:

id appRecordList = weakParser.appRecordList;
if (appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            rootViewController.entries = appRecordList;
            if(appRecordList== nil)
                 NSLog(@"weakParser.appRecordList is nill");
            if(appRecordList != nil)
                NSLog(@"weakParser.appRecordList is Not nill");
            [rootViewController.tableView reloadData];
        });
    }

我认为这是关于在块中使用弱,以下代码帮助分派方法传递
appRecordList
值:

id appRecordList = weakParser.appRecordList;
if (appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            rootViewController.entries = appRecordList;
            if(appRecordList== nil)
                 NSLog(@"weakParser.appRecordList is nill");
            if(appRecordList != nil)
                NSLog(@"weakParser.appRecordList is Not nill");
            [rootViewController.tableView reloadData];
        });
    }

有趣的事实是,如果我删除了_薄弱的部分或将其更改为_块,两种方式都可以!另一件事是,这是来自苹果网站的纯样本。有点困惑为什么它应该在那里。其他人都做同样的事情@AndreyMarkov,uu block将对weakParser的引用传递给block,因此在block中保留对它的强引用,并阻止它被释放,删除u weak使其成为强引用并做同样的事情(为了保留观点),这两种方法在一些方面是不同的,最好使用_块,这样更易于阅读,并且可以更改块中的参数。苹果的样品并非完美无缺,它可能意味着一些不同的东西。干杯。有趣的事实是,如果我删除了_薄弱部分或将其更改为_块,两种方式都可以!另一件事是,这是来自苹果网站的纯样本。有点困惑为什么它应该在那里。其他人都做同样的事情@AndreyMarkov,uu block将对weakParser的引用传递给block,因此在block中保留对它的强引用,并阻止它被释放,删除u weak使其成为强引用并做同样的事情(为了保留观点),这两种方法在一些方面是不同的,最好使用_块,这样更易于阅读,并且可以更改块中的参数。苹果的样品并非完美无缺,它可能意味着一些不同的东西。干杯。有趣的事实是,如果我删除了_薄弱部分或将其更改为_块,两种方式都可以!另一件事是,这是来自苹果网站的纯样本。有点困惑为什么它应该在那里。其他人都做同样的事情@AndreyMarkov,uu block将对weakParser的引用传递给block,因此在block中保留对它的强引用,并阻止它被释放,删除u weak使其成为强引用并做同样的事情(为了保留观点),这两种方法在一些方面是不同的,最好使用_块,这样更易于阅读,并且可以更改块中的参数。苹果的样品并非完美无缺,它可能意味着不同的东西