     }()
 
 var shouldKeepRunning = true
-let allRoutesTask = getAllRoutesWithParentManagedObjectContext(parentManagedObjectContext: managedObjectContext){
+let service = NetworkService()
+let task = service.getAllRoutesWithCompletionHandler {
     routes, error -> Void in
     
     if (error)


import Foundation
import NearbyTramsStorageKit

let baseURL = NSURL(string: "http://www.tramtracker.com")

func parseJSON(inputData: NSData) -> NSDictionary
{
    var dictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
    
    return dictionary
}

func getAllRoutesWithParentManagedObjectContext(parentManagedObjectContext managedObjectContext:NSManagedObjectContext, completionHandler: ((Route[]?, NSError?) -> Void)!) -> NSURLSessionDataTask!
{
    // thanks to: http://wongm.com/2014/03/tramtracker-api-dumphone-access/
    let url = NSURL(string: "Controllers/GetAllRoutes.ashx", relativeToURL:baseURL)
    let session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
    let task = session.dataTaskWithURL(url, completionHandler:{
        data, response, error -> Void in
        
        if (error)
        {
            completionHandler(nil, error)
        }
        else if let routesArray = parseJSON(data)["ResponseObject"] as? NSDictionary[]
        {
            let localContext = NSManagedObjectContext(concurrencyType: .ConfinementConcurrencyType)
            localContext.parentContext = managedObjectContext
            
            let routes = routesArray.map({
                (routeDictionary: NSDictionary) -> Route in
                
                let route = Route.insertInManagedObjectContext(localContext)
                route.configureWithDictionaryFromRest(routeDictionary)
                
                return route
                })
            localContext.save(nil)
            completionHandler(routes, nil)
        }
        })
    task.resume()
    
    return task;
}

var managedObjectContext: NSManagedObjectContext = {
    let moc = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)

    }
    else if (routes)
    {
        let localContext = NSManagedObjectContext(concurrencyType: .ConfinementConcurrencyType)
        localContext.parentContext = managedObjectContext
        
        let coreDataRoutes = routes!.map({
            (routeDictionary: NSDictionary) -> Route in
            
            let route = Route.insertInManagedObjectContext(localContext)
            route.configureWithDictionaryFromRest(routeDictionary)
            
            return route
            })
        localContext.save(nil)
        
        println("got all routes")
    }
    
