Search code examples
iosobjective-cobjective-c-blocksbackground-process

Background performFetchWithCompletionHandler using Blocks causes crash


I have an app that successfully fetches and displays RSS Feeds that I'd like to add background fetch. I receive: Thread 1 EXC_BAD_ACCESS (code=1, Address=0x10) where indicated below.

In app delegate:

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

In my main 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
}

The output I receive is:

performFetchWithCompletionHandler

call back


Solution

  • self.completionHandler = UIBackgroundFetchResultNewData; 
    

    does not match types. completionHandler is of type (void (^)(UIBackgroundFetchResult)) while UIBackgroundFetchResultNewData is of type NSUInteger.

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

    So when you call self.completionHandler (YES), self.completionHandler is an NSUInteger, so NSUInteger(YES) doesn't make much sense.