我有一个可以成功获取并显示我想添加背景获取的RSS feed的应用程序。我收到:线程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)),而UIBackgroundFetchResultNewData的类型为NSUInteger
typedef enum : NSUInteger {
   UIBackgroundFetchResultNewData,
   UIBackgroundFetchResultNoData,
   UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;

因此,当您调用self.completionHandler (YES)时,self.completionHandlerNSUInteger,因此NSUInteger(YES)没什么意义。

关于ios - 使用Blocks的后台performFetchWithCompletionHandler导致崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30402990/

10-11 16:20