Using NSURLSession from a Swift command line program

后端 未结 3 1101
星月不相逢
星月不相逢 2020-12-08 01:17

I\'m trying to test a little proof-of-concept command line app prior to integrating it into a larger app. What I\'m trying to do is download some data using NSURLSession usi

相关标签:
3条回答
  • 2020-12-08 01:19

    For proof of concept(s) or tryouts/testing purposes, you can simplify asynchronous complexity by hard coding some timeout period until your stuff finishes. (see notes below)

    SWIFT 5

        //...your magic here
        // add a little                                                                     
    0 讨论(0)
  • 2020-12-08 01:20

    Try this

    let sema = DispatchSemaphore( value: 0)
    
    let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg")!;
    
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
      print("after image is downloaded");
      sema.signal(); // signals the process to continue
    };
    
    task.resume();
    sema.wait(); // sets the process to wait
    
    0 讨论(0)
  • 2020-12-08 01:29

    You can use a semaphore to block the current thread and wait for your URL session to finish.

    Create the semaphore, kick off your URL session, then wait on the semaphore. From your URL session completion callback, signal the semaphore.

    You could use a global flag (declare a volatile boolean variable) and poll that from a while loop, but that is less optimal. For one thing, you're burning CPU cycles unnecessarily.

    Here's a quick example I did using a playground:

    import Foundation
    
    var sema = DispatchSemaphore( value: 0 )
    
    class Delegate : NSObject, URLSessionDataDelegate
    {
        func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
        {
            print("got data \(String(data: data, encoding: .utf8 ) ?? "<empty>")");
            sema.signal()
        }
    }
    
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config, delegate: Delegate(), delegateQueue: nil )
    
    guard let url = URL( string:"http://apple.com" ) else { fatalError("Could not create URL object") }
    
    session.dataTask( with: url ).resume()    
    
    sema.wait()
    
    0 讨论(0)
提交回复
热议问题