后台 performFetchWithCompletionHandler 使用 Blocks 导致崩溃

Background performFetchWithCompletionHandler using Blocks causes crash

我有一个应用程序可以成功获取并显示我想要添加后台获取的 RSS 源。我收到:线程 1 EXC_BAD_ACCESS(代码=1,地址=0x10),如下所示。

在应用委托中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //setup background fetch
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];

return YES;

}

        //background fetch new RSS Feeds
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"performFetchWithCompletionHandler");
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MasterViewController *navigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"MasterView"];
    MasterViewController *viewController = navigationController;

    [viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
        completionHandler(result);
    }];
}

在我的主要ViewController:

@property (nonatomic, copy) void (^completionHandler)(BOOL);
- (void) fetchNewDataWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler;
- (void) startParsingWithCompletionHandler:(void (^)(BOOL))completionHandler;

-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

self.completionHandler = UIBackgroundFetchResultNewData; //completionHandler;
[self startParsingWithCompletionHandler:^(BOOL success){

    if (success) {
        completionHandler(UIBackgroundFetchResultNewData);
        NSLog(@"completionHandler");
    }
    else{
        NSLog(@"error");
    }
}];

}

    - (void) storyIsDone//called when parser completed one rss feed
{
    numberOfCompletedStories ++;
    if (numberOfCompletedStories == self.rssFeedAddresses.count)
    {
            //if all the feeds are done cancel time-out timer
        [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(stopParsing) object: nil];
        [self.activityIndicator stopAnimating];
        storysAreLoading = NO;
        [self.refreshControl endRefreshing];
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reloadRSSfeeds) name: @"ReloadFeeds" object: nil];
        canRefresh = YES;
        NSLog(@"call back");
        self.completionHandler (YES);//crash here:  Thread 1 EXC_BAD_ACCESS (code=1, Address=0x10)
    }//else not yet complete
}

我收到的输出是:

performFetchWithCompletionHandler

回调

self.completionHandler = UIBackgroundFetchResultNewData; 

不匹配类型。 completionHandler(void (^)(UIBackgroundFetchResult)) 类型,而 UIBackgroundFetchResultNewDataNSUInteger.

类型
typedef enum : NSUInteger {
   UIBackgroundFetchResultNewData,
   UIBackgroundFetchResultNoData,
   UIBackgroundFetchResultFailed 
} UIBackgroundFetchResult;

所以当你调用 self.completionHandler (YES) 时,self.completionHandler 是一个 NSUInteger,所以 NSUInteger(YES) 没有多大意义。

试试这个: 创建一个变量 var completionHandler:((UIBackgroundFetchResult)->Void)! 当您收到推送通知集时

self.completionHandler = completionHandler.

然后执行后台操作,通常是从服务器下载新数据。 完成后,像这样调用 completionHandler:

if let handler = self.completionHandler{
    UIBackgroundFetchResult.NewData
}
self.completionHandler = nil

那么你应该去掉那个警告。