Ver Fonte

Background sync implemented.

Iñigo Valentin há 9 anos atrás
pai
commit
38963d3385
2 ficheiros alterados com 11 adições e 3 exclusões
  1. 8 1
      app/AppDelegate.swift
  2. 3 2
      app/ViewController.swift

+ 8 - 1
app/AppDelegate.swift

@@ -43,8 +43,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 	
 
 	func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+		NSLog(":BACKGROUND:LOG: Start bg sync...")
 		Sync()
-		completionHandler(UIBackgroundFetchResult.newData)
+		
+		let when = DispatchTime.now() + 120 // 2 minutes to perform the sync.
+		DispatchQueue.main.asyncAfter(deadline: when) {
+			NSLog(":BACKGROUND:LOG: Calling completion handler.")
+			completionHandler(UIBackgroundFetchResult.newData)
+		}
+		
 		/*if let tabBarController = window?.rootViewController as? UITabBarController, let viewControllers = tabBarController.viewControllers {
 			for viewController in viewControllers {
 				if let fetchViewController = viewController as? FetchViewController {

+ 3 - 2
app/ViewController.swift

@@ -190,8 +190,9 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		fetchLocation()
 		self.locationTimer = Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(fetchLocation), userInfo: nil, repeats: true)
 				
-		NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
-		Sync()
+		//NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
+		NSLog(":CONTROLLER:DEBUG: Skyp sync")
+		//Sync()
 
 		
 		self.delegate = UIApplication.shared.delegate as? AppDelegate