Issue with background connection in a TodayExtension

I am trying to fill a table in a today extension each time a user swipes it. I managed to have it working fine the first time the extension is opened, yet when I close and reopen it the cell does not get populated notwithstanding the source is correctly populated. When I look at the logs it looks like just:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

is called but no:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

Moreover I have really no cue about how to handle the session among the widget openings. If I invalidate it when I exit the extension, then it finds it invalidated on return, if I put it to nil and regenerate it or I just ignore it when exiting I get:

A background URLSession with identifier .... already exists!

and sometimes even the nasty:

Attempted to create a task in a session that has been invalidated even the first time.

This latter usually solved by cleaning the Derived folder.

This is the piece of Swift code involved:

  func processData(data: NSData){
        let datastring = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("Task completed: "+(datastring! as String))
        self.busCollection=BusCollection(data: data)
        dispatch_async(dispatch_get_main_queue(), {
            self.actInd.stopAnimating()
            self.tableView.reloadData()
            self.resetContentSize()
            self.processingUrl=false
            if ((self.widgetCompletion) != nil){
                println("completion count=\(self.busCollection.count())")
                self.widgetCompletion!(self.busCollection.count()>0 ? .NewData : .NoData)
            }
        })
    }
    func URLSession(session: NSURLSession,
        downloadTask: NSURLSessionDownloadTask,
        didFinishDownloadingToURL location: NSURL){
            println("path=\(location)")
            let data = NSData(contentsOfURL: location)
            if let fileData = data {
                let content = NSString(data: fileData, encoding:NSUTF8StringEncoding) as! String
                println("arriva \(content)")
                let queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                dispatch_async(queue, {
                    self.processData(fileData)
                })
                } else {
                    self.processingUrl=false
                }
    }

    var backgroundSession:NSURLSession{
        if self.session == nil{
            var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.information.inarrivo.widgetBackgroundConfiguration")
            configuration.sharedContainerIdentifier="group.com.information.inarrivo"
            configuration.allowsCellularAccess = true;
            configuration.timeoutIntervalForRequest = 20.0;
            configuration.timeoutIntervalForResource = 40.0;
            /
            /
            /
            let queue=NSOperationQueue.mainQueue()
            self.session = NSURLSession(configuration:configuration, delegate:self, delegateQueue:queue)
        }
        return self.session!
    }

    func downloadCloseBuses(prefetch:Bool){
        var phpFile = prefetch ? "getBusesForWidget" : "collectivePoll";
        println("prefetch=\(prefetch) phpFile=\(phpFile)")
        /
        if let location=currentLocation {
            /
            processingUrl=true;
            let urlString="http:/
            println(urlString)
            let url=NSURL(string:urlString)!
            let urlRequest=NSURLRequest(URL: url, cachePolicy:.ReloadRevalidatingCacheData, timeoutInterval: 50)
            /
            let request = NSURLRequest(URL: NSURL(string: urlString)!)
            /var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.information.inarrivo.widgetBackgroundConfiguration")
            configuration.sharedContainerIdentifier="group.com.information.inarrivo"
            let queue=NSOperationQueue.mainQueue()
            let backSession = NSURLSession(configuration:configuration, delegate:self, delegateQueue:queue)*/
        
            let task = backgroundSession.downloadTaskWithRequest(request)
            task.resume()
        }
    }

    func widgetPerformUpdateWithCompletionHandler(
        completionHandler: ((NCUpdateResult) -> Void)!) {
            widgetCompletion=completionHandler
    }

As said, both times the code is executed, the url is correctly sent and the result returned, albeit with the Session warning, but only the first time the table is shown. Even more strangely the activity indicator (self.actInd) is not hidden, notwithstanding the code passes through that instruction

I even tried to do a jump in a secondary queue and then return on the main thread to follow the Apple recommendations, but that changed nothing. Such issue looks definitely one of some interface being shown on the wrong thread or, if it were not Swift, of self being nil, but I totally miss how I could have ended in a background thread in my code, and just the second time I open the widget!

Issue with background connection in a TodayExtension
 
 
Q