gpt4 book ai didi

ios - 后台 performFetchWithCompletionHandler 使用 Blocks 导致崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:40:41 25 4
gpt4 key购买 nike

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

在应用委托(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);
}];
}

在我的主 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.completionHandler是一个 NSUInteger , 所以 NSUInteger(YES)没有多大意义。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com