我有一个应用程序可以成功获取并显示我想添加后台获取的 RSS 提要。我收到:线程 1 EXC_BAD_ACCESS(代码=1,地址=0x10),如下所示。
在应用委托(delegate)中:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
//setup background fetch
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
//background fetch new RSS Feeds
-(void)applicationUIApplication *)application performFetchWithCompletionHandlervoid (^)(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);
}];
}
在我的主视图 Controller 中:
@property (nonatomic, copy) void (^completionHandler)(BOOL);
- (void) fetchNewDataWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler;
- (void) startParsingWithCompletionHandlervoid (^)(BOOL))completionHandler;
-(void)fetchNewDataWithCompletionHandlervoid (^)(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
}
我收到的输出是:
执行FetchWithCompletionHandler
回电
Best Answer-推荐答案 strong>
self.completionHandler = UIBackgroundFetchResultNewData;
不匹配类型。 completionHandler 属于 (void (^)(UIBackgroundFetchResult)) 类型,而 UIBackgroundFetchResultNewData 属于 NSUInteger 类型。
typedef enum : NSUInteger {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;
所以当你调用self.completionHandler(YES) 时,self.completionHandler 是一个NSUInteger ,所以NSUInteger(YES) 没有多大意义。
关于ios - 使用 Blocks 的后台 performFetchWithCompletionHandler 导致崩溃,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/30402990/
|