我希望在我的iOS应用程序中集成后台获取功能,我已经在项目的功能列表中启用了后台获取功能,然后我插入了以下内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
要启用最小间隔的后台获取功能,然后在应用程序委托中插入委托方法:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"Call fetch iCloud");
[[MySingleton sharedManager] startCheckOnCloud];
}
我的问题是这样的,我知道我需要调用这个函数:
completionHandler(UIBackgroundFetchResultNewData);
我曾经看到很多关于在performFetchWithCompletionHandler
代理方法中插入completionHandler
的例子,但是在这个方法中,我调用了一个Singleton
中的NSOperation
,该操作检查iCloud文件夹并对Sqlite DB
进行一些更改。如果我这样做:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"Call fetch iCloud");
[[MySingleton sharedManager] startCheckOnCloud];
completionHandler(UIBackgroundFetchResultNewData);
}
操作开始后就没有执行任何操作,可能是因为系统立即将其休眠,如果我删除completionHandler,会收到以下警告:
Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
我的问题是,我如何使用NSOperation
处理completionHandler
?
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * Int64(NSEC_PER_SEC)), dispatch_get_main_queue()) { completionHandler(UIBackgroundFetchResult.NewData) } }
- Logan