gronzzz
gronzzz

Reputation: 615

Connection difference between protocol-delegate methods and block

i'm learning swift and found these protocol-delegate methods for connection:

func connection(connection: NSURLConnection!, didFailWithError error: NSError!)

and 3 more... so they start as

connection.start()

But there are also block structure:

NSURLConnection.sendSynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in

So difference with "sendAsynchronousRequest" i understand, but what's difference with this block method and what is a best practice ?

Upvotes: 0

Views: 390

Answers (1)

dinesh
dinesh

Reputation: 72

My understanding both are Asynchronous operation .

Delegate Methods : If you want to monitor the status of connection and recieveing data use delegate methods

Blocks: If you dont want to monitor and only need to perform some operation when the data has been fully received use blocks .

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-SW1

Hope that helps!

Upvotes: 1

Related Questions