mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-03 19:03:52 +00:00
- added timer to API manager
This commit is contained in:
1 parent
f2d59782ae
commit
c982044eaa
3 files changed
+38
-11
No files matched your search
@@ -11,23 +11,44 @@ import CoreData
|
||||
|
||||
class APIManager: NSObject {
|
||||
|
||||
/* Base url of all requests */
|
||||
private var baseUrlString: String?
|
||||
|
||||
/* AFNetworking operation manager */
|
||||
private var manager: AFHTTPRequestOperationManager?
|
||||
|
||||
/* Date of last GPS position upload */
|
||||
var lastSync: NSDate?
|
||||
|
||||
/* Number of seconds between syncs */
|
||||
private var syncPeriod: NSTimeInterval = 3
|
||||
|
||||
var loop: NSTimer?
|
||||
|
||||
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
|
||||
|
||||
/* REST Paths */
|
||||
/* Map device to competitor */
|
||||
private let postDeviceMapping = "/sailingserver/rc/racelog"
|
||||
|
||||
/* Send location to server */
|
||||
private let postGPSFixPath = "/tracking/recordFixesFlatJson"
|
||||
|
||||
/* Singleton */
|
||||
class var sharedManager: APIManager {
|
||||
struct Singleton {
|
||||
static let sharedAPIManager = APIManager()
|
||||
}
|
||||
return Singleton.sharedAPIManager
|
||||
}
|
||||
|
||||
deinit {
|
||||
NSNotificationCenter.defaultCenter().removeObserver(self)
|
||||
override init() {
|
||||
super.init()
|
||||
|
||||
// TODO: move to initManager
|
||||
uploadData()
|
||||
}
|
||||
|
||||
/* Sets base URL for all requests. Starts reachability listener. */
|
||||
/* Sets base URL for all requests. Request and response are JSON. Starts reachability listener. Starts timer for uploading data. */
|
||||
func initManager(baseUrlString: String) {
|
||||
if self.baseUrlString == baseUrlString {
|
||||
return
|
||||
@@ -43,21 +64,21 @@ class APIManager: NSObject {
|
||||
|
||||
// set up reachability
|
||||
let operationQueue = manager!.operationQueue
|
||||
manager!.reachabilityManager.setReachabilityStatusChangeBlock({ (AFNetworkReachabilityStatus status) -> Void in
|
||||
manager!.reachabilityManager.setReachabilityStatusChangeBlock({(AFNetworkReachabilityStatus status) -> Void in
|
||||
switch (status) {
|
||||
case AFNetworkReachabilityStatus.ReachableViaWWAN, AFNetworkReachabilityStatus.ReachableViaWiFi:
|
||||
operationQueue?.suspended = false;
|
||||
break;
|
||||
default:
|
||||
operationQueue?.suspended = true;
|
||||
|
||||
}
|
||||
})
|
||||
manager!.reachabilityManager.startMonitoring()
|
||||
|
||||
// set up notifications
|
||||
/*
|
||||
NSNotificationCenter.defaultCenter().addObserver(self, selector: "sendGPSFixes:", name: DataManager.NotificationType.newGPSFix, object: nil)
|
||||
*/
|
||||
// TODO: only one operation at a time?
|
||||
//manager!.operationQueue.maxConcurrentOperationCount = 0
|
||||
|
||||
}
|
||||
|
||||
/* Send a device to competitor mapping. */
|
||||
@@ -86,4 +107,10 @@ class APIManager: NSObject {
|
||||
})
|
||||
}
|
||||
|
||||
/* See if any rows need to be uploaded. Schedule timer again. */
|
||||
func uploadData() {
|
||||
NSLog("uploadData")
|
||||
let loop = NSTimer.scheduledTimerWithTimeInterval(syncPeriod++, target:self, selector:"uploadData", userInfo:nil, repeats:false);
|
||||
NSRunLoop.currentRunLoop().addTimer(loop, forMode:NSRunLoopCommonModes);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
|
||||
// Override point for customization after application launch.
|
||||
DataManager.sharedManager
|
||||
APIManager.sharedManager
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -39,8 +39,7 @@ class DataManager: NSObject {
|
||||
func newLocation(notification: NSNotification) {
|
||||
let gpsFix = NSEntityDescription.insertNewObjectForEntityForName("GPSFix", inManagedObjectContext: self.managedObjectContext!) as GPSFix;
|
||||
gpsFix.deviceUuid = DeviceUDIDManager.UDID
|
||||
gpsFix.initWithDictionary(notification.userInfo!)
|
||||
return
|
||||
gpsFix.initWithDictionary(notification.userInfo!)
|
||||
}
|
||||
|
||||
func trackingStopped(notification: NSNotification) {
|
||||
|
||||
Reference in new issue
Block a user