GCDAsyncSocket for Synchronous Requests with Swift 1.2
Tag : ios , By : usingtechnology
Date : March 29 2020, 07:55 AM
wish help you to fix your issue So I decided to use NSOperation for this. Created a class file called SyncRequest.swift with the following code: import Foundation
class SyncRequest : NSOperation {
var socket:GCDAsyncSocket! = nil
var msgData:NSData! = nil
override var concurrent: Bool {
return false
}
override var asynchronous: Bool {
return false
}
private var _executing: Bool = false
override var executing: Bool {
get {
return _executing
}
set {
if (_executing != newValue) {
self.willChangeValueForKey("isExecuting")
_executing = newValue
self.didChangeValueForKey("isExecuting")
}
}
}
private var _finished: Bool = false;
override var finished: Bool {
get {
return _finished
}
set {
if (_finished != newValue) {
self.willChangeValueForKey("isFinished")
_finished = newValue
self.didChangeValueForKey("isFinished")
}
}
}
/// Complete the operation
func completeOperation() {
executing = false
finished = true
}
override func start() {
if (cancelled) {
finished = true
return
}
executing = true
main()
}
override func main() -> (){
println("starting...")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReadData:", name: "DidReadData", object: nil)
sendData()
}
func sendData() {
socket.writeData(msgData, withTimeout: -1.0, tag: 0)
println("Sending: \(msgData)")
socket.readDataWithTimeout(-1.0, tag: 0)
}
func didReadData(notif: NSNotification) {
println("Data Received!")
NSNotificationCenter.defaultCenter().removeObserver(self, name: "DidReadData", object: nil)
completeOperation()
}
}
// sync the request to the controller
let queue = NSOperationQueue() // sync request queue
let requestOperation = SyncRequest()
requestOperation.socket = socket // pass the socket to send through
requestOperation.msgData = msgData // pass the msgData to send
queue.maxConcurrentOperationCount = 1
queue.addOperation(requestOperation)
public func socket(socket : GCDAsyncSocket!, didReadData data:NSData!, withTag tag:Int){
...
NSNotificationCenter.defaultCenter().postNotificationName("DidReadData", object: data)
...
}
|
RxJs: How can I make synchronous requests?
Date : March 29 2020, 07:55 AM
|
How to make scrapy requests synchronous
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further If I understood correctly, the problem here is because the session information is stored in a stateful server. Right? A way to handle this would be having one session for each state, managing it through cookiejars. E.g: for year in years[1:]:
for state in states[1:]:
yield Request(
# ...,
callback=self.parse_city,
meta={'cookiejar': state}
)
|
Synchronous API requests with Queue in Swift?
Tag : ios , By : Caleb Ames
Date : March 29 2020, 07:55 AM
this one helps. You can create your own DispatchQueue and put you operations on it as DispatchWorkItems. It is serial per default. Just remember to call your completions on DispatchQueue.main if you plan to update the UI. John Sundell has a wonderful article about DispatchQueues here:
|
Synchronous HTTP Requests in Swift
Date : March 29 2020, 07:55 AM
|