Просмотр исходного кода

First steps to a background sync.

Iñigo Valentin 9 лет назад
Родитель
Сommit
2a8cd15f1c
5 измененных файлов с 65 добавлено и 24 удалено
  1. 5 0
      app.xcodeproj/project.pbxproj
  2. 19 0
      app/AppDelegate.swift
  3. 30 20
      app/HomeView.swift
  4. 4 0
      app/Info.plist
  5. 7 4
      app/ViewController.swift

+ 5 - 0
app.xcodeproj/project.pbxproj

@@ -345,6 +345,11 @@
 						DevelopmentTeam = 9F9ER4X7TG;
 						LastSwiftMigration = 0830;
 						ProvisioningStyle = Automatic;
+						SystemCapabilities = {
+							com.apple.BackgroundModes = {
+								enabled = 1;
+							};
+						};
 					};
 				};
 			};

+ 19 - 0
app/AppDelegate.swift

@@ -34,10 +34,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 		// Override point for customization after application launch.
 		
+		// Google Mapas API KEY
 		GMSServices.provideAPIKey("AIzaSyBfIiBM_YSBlxybmI_Uz_fGoUFN4wacR80")
 		
+		// Background mode.
+		UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
+		
 		return true
 	}
+	
+	func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+		Sync()
+		completionHandler(UIBackgroundFetchResult.newData)
+		/*if let tabBarController = window?.rootViewController as? UITabBarController, let viewControllers = tabBarController.viewControllers {
+			for viewController in viewControllers {
+				if let fetchViewController = viewController as? FetchViewController {
+					fetchViewController.fetch {
+						fetchViewController.updateUI()
+						completionHandler(.newData)
+					}
+				}
+			}
+		}*/
+	}
 
 	func applicationWillResignActive(_ application: UIApplication) {
 		// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

+ 30 - 20
app/HomeView.swift

@@ -41,8 +41,13 @@ class HomeView: UIView {
 	@IBOutlet weak var pastActivitiesSection: Section!
 	@IBOutlet weak var socialSection: Section!
 	
+	var moc: NSManagedObjectContext? = nil
+	var delegate: AppDelegate? = nil
+	var lang: String? = nil
+	
 	override init(frame: CGRect){
 		super.init(frame: frame)
+		
 	}
 	
 	/**
@@ -69,32 +74,37 @@ class HomeView: UIView {
 		socialSection.setTitle(text: "Siguenos")
 		
 		//Get info to populate sections
-		let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
+		self.moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
 		
-		let appDelegate = UIApplication.shared.delegate as! AppDelegate
-		moc.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
-		let lang : String = getLanguage()
-		
-		//Populate sections
-		setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection.getContentStack())
-		setUpBlog(context: moc, delegate: appDelegate, lang: lang, parent: blogSection.getContentStack())
-		setUpFutureActivities(context: moc, delegate: appDelegate, lang: lang, parent: futureActivitiesSection.getContentStack())
-		setUpSocial(parent: socialSection.getContentStack())
-		setUpGallery(context: moc, delegate: appDelegate, parent: gallerySection.getContentStack())
+		self.delegate = UIApplication.shared.delegate as! AppDelegate
+		self.moc?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
+		self.lang = getLanguage()
 		
+		populate()
 		
-		pastActivitiesSection.expandSection()
-		
-		
-		//Always at the end: update scrollview
+		/*//Always at the end: update scrollview
 		var h: Int = 0
 		for view in scrollView.subviews {
-			//contentRect = contentRect.union(view.frame);
-			h = h + Int(view.frame.height) + 30 //Why 30?
-			print("curh: \(h)")
+		//contentRect = contentRect.union(view.frame);
+		h = h + Int(view.frame.height) + 30 //Why 30?
+		print("curh: \(h)")
 		}
 		// TODO: Calculate at the end
-		self.scrollView.contentSize.height = 1300// CGFloat(h);
+		self.scrollView.contentSize.height = 1300// CGFloat(h);*/
+	}
+	
+	func populate(){
+	
+		//Populate sections
+		setUpPastActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.pastActivitiesSection.getContentStack())
+		setUpBlog(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.blogSection.getContentStack())
+		setUpFutureActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.futureActivitiesSection.getContentStack())
+		setUpSocial(parent: self.socialSection.getContentStack())
+		setUpGallery(context: self.moc!, delegate: self.delegate!, parent: self.gallerySection.getContentStack())
+		
+		
+		//pastActivitiesSection.expandSection()
+
 	}
 	
 	func getLanguage() -> String{
@@ -135,7 +145,7 @@ class HomeView: UIView {
 			for r in searchResults as [NSManagedObject] {
 				count = count + 1
 				//get the Key Value pairs (although there may be a better way to do that...
-				print("Perm: \(r.value(forKey: "permalink"))")
+				print("Perm: \(String(describing: r.value(forKey: "permalink")))")
 				
 				
 				//Create a new row

+ 4 - 0
app/Info.plist

@@ -24,6 +24,10 @@
 	<true/>
 	<key>NSLocationWhenInUseUsageDescription</key>
 	<string>Permitiendo acceso a tu ubicación, podrás conocer la distancia a la cuadrilla y a determinados eventos.</string>
+	<key>UIBackgroundModes</key>
+	<array>
+		<string>fetch</string>
+	</array>
 	<key>UILaunchStoryboardName</key>
 	<string>LaunchScreen</string>
 	<key>UIMainStoryboardFile</key>

+ 7 - 4
app/ViewController.swift

@@ -145,6 +145,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		}
 	}
 	
+	override func viewWillAppear(_ animated: Bool) {
+		NSLog(":CONTROLLER:DEBUG: Forcing a call to populate")
+		self.populate()
+		super.viewWillAppear(animated)
+	}
+	
 	/**
 	 Run when the app loads.
 	*/
@@ -170,16 +176,13 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		self.delegate = UIApplication.shared.delegate as? AppDelegate
 		self.delegate?.controller = self
 		
-		NSLog(":CONTROLLER:DEBUG: NOT Forcing a call to populate")
-		//self.populate()
-		
 		super.viewDidLoad()
 		
 	}
 
 	func populate(){
 		if (self.containerViewHome != nil){
-			//(self.containerViewHome as HomeView).populate2()
+			//(self.containerViewHome as HomeView).populate()
 			//let ng = self.containerViewHome.dbgTxt
 			//NSLog("AAAAA \(ng)")
 		}