Search code examples
iosswiftnsoperationqueue

Reload TableView with addOperationWithBlock


i am trying to reload my tableView inside a block but my outlet is always nil. I have a background Download Manager with a small popover to show the current downloads. Everything works fine, but i am not able to reload my tableView while downloading (so show download speed for example).

i have a simple outlet of my UITableView

class FileDownloadHandlerViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSURLSessionDelegate, NSURLSessionDownloadDelegate {

    @IBOutlet var fileDownloadTable: UITableView!

// This is my Download Task

func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {

....

 if(success == true) {

        // Change the flag values of the respective FileDownloadInfo object

        let index:Int = self.getFileDownloadInfoIndexWithTaskIdentifier(downloadTask.taskIdentifier)

        let fdi = arrFileDownloadData.objectAtIndex(index) as FileDownloadInfo

        fdi.isDownloading = false
        fdi.downloadComplete = true
        fdi.taskIdentifier = -1
        fdi.taskResumeData = nil

        // Reload the respective table view row using the main thread.

        let indexPath = NSIndexPath(forRow: index, inSection: 0)

        println(indexPath)

        if(fileDownloadTable == nil)
        {
            println("still nil")
        }

        NSOperationQueue.mainQueue().addOperationWithBlock({

            if(self.fileDownloadTable == nil)
            {
                println("nil again")
            }

//This is the Problem. fileDownloadTable is always nil


            //self.fileDownloadTable.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

        })

    } else {

        println("Unable to copy temp file. Error: \(error)")

    }

Any Ideas? Do i need to pass my Outlets to this Block? If i used this DownloadController not inside a popover (so still i my mainViewController) everything works fine.

Thanks in advance.

EDIT

Ok found out that the problem is that i will set my NSUrlSessionConfiguration as a global variable outside my FileDownloadClass.

var session:NSURLSession!
var sessionConfiguration:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.company")

If i set it in my class, i can reload the tableView. But, now i get after closing my popover and load something else the following error:

A background URLSession with identifier ... already exists!

How can i check if there is already a URLSession, or set this as a global URLSession?


Solution

  • in ViewDidLoad , add

       [self backgroundSession];
    

    then add this function as it is

     - (NSURLSession *)backgroundSession
        {
            static NSURLSession *session = nil;
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.iosDevelopment.VDownloader.SimpleBackgroundTransfer.BackgroundSession"];
                session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
            });
    
        return session;
    }