2015-07-22 08:22:28 8 Comments
I have this code from here to do synchronous request of a URL on Swift 2.
func send(url: String, f: (String)-> ()) {
var request = NSURLRequest(URL: NSURL(string: url)!)
var response: NSURLResponse?
var error: NSErrorPointer = nil
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
var reply = NSString(data: data, encoding: NSUTF8StringEncoding)
f(reply)
}
but the function NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
was deprecated and I don't see how one can do synchronous requests on Swift, cause the alternative is asynchronous. Apparently Apple deprecated the only function that can do it synchronously.
How can I do that?
Related Questions
Sponsored Content
19 Answered Questions
[SOLVED] How to make an HTTP request in Swift?
- 2014-06-03 13:12:32
- Dicky Tsang
- 350935 View
- 347 Score
- 19 Answer
- Tags: ios objective-c swift iphone nsurlsession
1 Answered Questions
2 Answered Questions
1 Answered Questions
1 Answered Questions
[SOLVED] how to use JsonArray out of Queue
- 2015-02-20 06:07:17
- Gaurav Verma
- 227 View
- 1 Score
- 1 Answer
- Tags: ios objective-c swift ios7 xcode6
3 Answered Questions
1 Answered Questions
[SOLVED] how to add json parse data in core data in iOS?
- 2014-09-30 08:10:05
- chetan gharat
- 106 View
- 0 Score
- 1 Answer
- Tags: ios objective-c autolayout
2 Answered Questions
[SOLVED] setbackground loading with do while loop
- 2014-02-25 13:29:33
- Jaymin Raval
- 1104 View
- 3 Score
- 2 Answer
- Tags: ios objective-c nsxmlparser nsthread nsurlconnectiondelegate
3 Answered Questions
[SOLVED] how to compare a NSURLconnection http response data with a string value in iphone?
- 2009-05-02 06:31:41
- Xman_sid
- 1904 View
- 0 Score
- 3 Answer
- Tags: iphone string http comparison nsurlconnection
4 comments
@fpg1503 2015-07-22 12:27:05
If you really wanna do it synchronously you can always use a semaphore:
EDIT: Add some hackish ! so the code works, don't do this in production code
Swift 3.0+ (3.0, 3.1, 3.2, 4.0)
@SpaceDog 2015-07-22 12:44:35
Xcode 7b4 is complaining about a lot of things on your code.
@SpaceDog 2016-01-18 16:01:13
what down vote? I have not down voted you.
@Vinod Radhakrishnan 2018-03-21 09:53:09
I think it's better to use "dispatchGroup"
@Matej Ukmar 2016-11-09 15:19:17
Based on @fpg1503 answer I made a simple extension in Swift 3:
Then you simply call:
@Tom Andersen 2016-08-02 21:02:20
Synchronous requests are sometimes fine on background threads. Sometimes you have a complicated, impossible to change code base full of async requests, etc. Then there is a small request that can't be folded into the current system as async. If the sync fails, then you get no data. Simple. It mimics how the file system works.
Sure it does not cover all sorts of eventualities, but there are lots of eventualities not covered in async as well.
@Jiri Trecak 2015-07-22 11:34:29
There is a reason behind deprecation - there is just no use for it. You should avoid synchronous network requests as a plague. It has two main problems and only one advantage (it is easy to use.. but isn't async as well?):
Instead of this, just use asynchronous request:
iOS9 Deprecation
Since in iOS9 this method is being deprecated, I suggest you to use NSURLSession instead:
@SpaceDog 2015-07-22 12:03:57
that function was deprecated on iOS 9
@Jiri Trecak 2015-07-22 12:07:24
I edited my post with your note. Thank you
@Pierre Lebeaupin 2015-07-22 13:48:10
For a longer exposition on why synchronous networking is bad, look at devforums.apple.com/thread/9606?tstart=0 (requires iOS dev forums access)
@olivaresF 2016-02-02 00:43:28
I feel like, even though you're correct, the question was looking for a way to do synchronous networking and this isn't an answer.
@Jiri Trecak 2016-02-02 12:12:15
SO primary purpose is to give you a GOOD advice. Advising someone to even consider using synch requests is just.. bad. I understand you opinion and value it, but I feel this is exactly the mindset that hinders the quality of this site. At minimum, it is definitely not worth downvoting :)
@Janusz Chudzynski 2016-05-12 02:23:37
What if you need to have a network call inside a NSOperation's main method? That's where synchronous networking is justified.