Ios 后台performFetchWithCompletionHandler使用块导致崩溃
我有一个应用程序,可以成功获取并显示RSS提要,我想添加后台获取。我收到:线程1执行错误访问(代码=1,地址=0x10),如下所示 应用程序内代理:Ios 后台performFetchWithCompletionHandler使用块导致崩溃,ios,objective-c,objective-c-blocks,background-process,Ios,Objective C,Objective C Blocks,Background Process,我有一个应用程序,可以成功获取并显示RSS提要,我想添加后台获取。我收到:线程1执行错误访问(代码=1,地址=0x10),如下所示 应用程序内代理: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //setup background fetch [application setMinimumBackgroundFe
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//setup background fetch
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
在我的主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");
}
}];
}
我收到的输出是:
performFetchWithCompletionHandler
回电
self.completionHandler = UIBackgroundFetchResultNewData;
不匹配类型completionHandler
的类型为(void(^)(UIBackgroundFetchResult))
,而UIBackgroundFetchResultNewData
的类型为NSUInteger
typedef enum : NSUInteger {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;
因此,当您调用self.completionHandler(YES)
时,self.completionHandler
是一个nsuiger
,因此nsuiger(YES)
没有多大意义。尝试以下方法:
创建变量var completionHandler:((UIBackgroundFetchResult)->Void)!
当您收到推送通知集时
self.completionHandler = completionHandler.
然后做你的后台工作,通常从服务器下载新数据。
完成此操作后,按如下方式调用completionHandler:
if let handler = self.completionHandler{
UIBackgroundFetchResult.NewData
}
self.completionHandler = nil
那么你应该摆脱这个警告。谢谢。这解决了崩溃问题,但现在receive:Warning:Application delegate收到了对-Application:performFetchWithCompletionHandler:的调用,但从未调用过完成处理程序。self.completionHandler=UIBackgroundFetchResultNewData;修复了崩溃,但解决了崩溃,但现在接收:警告:应用程序委托接收到对-Application:performFetchWithCompletionHandler:的调用,但从未调用完成处理程序。我发现self.completionHandler==nil,当它到达storyistone方法时
self.completionHandler = completionHandler.
if let handler = self.completionHandler{
UIBackgroundFetchResult.NewData
}
self.completionHandler = nil