使用Blocks的后台performFetchWithCompletionHandler导致崩溃

时间:2015-05-22 17:52:33

标签: ios objective-c objective-c-blocks background-process

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

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

在我的主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

回电

2 个答案:

答案 0 :(得分:4)

self.completionHandler = UIBackgroundFetchResultNewData; 

与类型不匹配。 completionHandler的类型为(void (^)(UIBackgroundFetchResult)),而UIBackgroundFetchResultNewData的类型为NSUInteger

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

因此,当您致电self.completionHandler (YES)时,self.completionHandlerNSUInteger,因此NSUInteger(YES)没有多大意义。

答案 1 :(得分:0)

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

self.completionHandler = completionHandler.

然后做你的背景,通常从服务器下载新数据。 完成后,请像这样调用completionHandler:

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

然后你应该摆脱那个警告。