<SWIFT> Same login session in tabs

Dear experts - I am writing a program by using swift (newbie). I would like to use NSURLSession to interact with http server. However, I meet a problem.


In my apps, there is contain 2 tabs.


Tab 1 - It shows the latest blog info.

Tab 2 - It shows user favorite posts.


I would like to share same session to access the http server data. However, there is no share area to do so. I just wonder that only singleton design pattern is my final answer or we have another alterntive solution. Thank you for your advise.


Regards,


Tommy

Answered by DTS Engineer in 34705022

In situations like this I generally recommend that you have a model-level controller that owns the NSURLSession and hence receives all of its delegate callbacks. You can either implement that as a singleton or inject a reference to it into each of your view controllers.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Before going further I recommend that you brush up on the model-view-controller design pattern. This is formally covered in the "Model-View-Controller" section of "Cocoa Core Competencies".

https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html#//apple_ref/doc/uid/TP40008195-CH32-SW1

There is, however, a lot more information about this technique available from both Apple and non-Apple sources. I'm not in a position to recommend non-Apple resources but on the Apple front I'm a big fan of WWDC 2010 Session 116 Model-View-Controller for iPhone OS.

It's an old 'un but a good 'un.

In my experience the best way to address this issue is to put your networking code at the model layer. That is, have your view controllers simply arrange to display model objects and have the networking code populate and mutate those objects like it would with any other model objects.

This approach has some really important advantages:

  • It decouples the view controller from the networking, which simplifies your development and makes things easier to test.

  • It makes it easier to deal with threads, because there's a clear delineation between the model, where threading is an issue, and the view controller, where everything must be running on the main thread.

  • Like any other MVC setup, it facilitates data coherency; you can have two view controllers showing related information without the world exploding.

Once you do that then you just have to arrange for each view controller to get access to the model. You can do this with a singleton if you like, but personally I'm starting to be won over by the arguments in favour of dependency injection.

IMPORTANT: This design does not mean you have to do your networking in the model objects themselves. It's fine to have controller-style objects working at the model layer.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

eskimo - Thank you for your share. Actually, I had tried to follow MVC model. I just wonder that the NSURLSession in tab1 and tab2 controller are using same session to connection server or not. Thank you.


// tab 1

class tab1controller: UIViewController

{

override func viewDidAppear(animated: Bool) {

var page1: readPost() = readPost("L")

}

}


// tab 2

class tab2controller: UIViewController

{

override func viewDidAppear(animated: Bool) {

var page2: readPost() = readPost("F")

}

}


class webConnect()

{

// use NSURLSession

}

class readPost()

{

init(postType: String)

{

// postType - L = Lastest; F = favorite

}


func readPost()

{

var webConn : webConnect()

}

}

eskimo - After I keep working on my project, I need to re-parse my question. I found that NSURLSession.sharedsession() is shared to connect server. When I use it in Tab 1 and Tab 2, the taskIdentifier of each task is 1 and 2 respectively.


However, I want to config sharedsession as code below (setting). So that I can fire the during downloading and download is completed by setting up delegate: self. Thank you.


setting

        var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        var mainSession = NSURLSession(configuration: configuration,
            delegate: self,
            delegateQueue:NSOperationQueue.mainQueue())


    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)


   func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL)
Accepted Answer

In situations like this I generally recommend that you have a model-level controller that owns the NSURLSession and hence receives all of its delegate callbacks. You can either implement that as a singleton or inject a reference to it into each of your view controllers.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
&lt;SWIFT&gt; Same login session in tabs
 
 
Q