I'm very new on iOS application development so please explain me about delegate and call back. When we use use call back and delegate?
Call backs are used to allow an API or service to provide information to your code when certain events occur (e.g. when a task has completed). This is useful in asynchronous programming, e.g. when you want your current thread to get on with something else, or to allow the user to continue using the UI. (i.e. a call back is a function or lambda you have written, which is passed as a parameter to another method)
A delegate is the 'signature' (the 'type definition' of a method, including parameters) that a method (such as a call back) must provide in order for it to be useable as callback or event handler.
Edit Just to be complete, Delegation is also a design pattern, whereby the responsibility of control or action is delegated from one object to another.
Big piece about delegates here on the dev centre:
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html
There is a tutorial app using callback/delegate
http://brandontreb.com/objective-c-programming-tutorial-creating-a-twitter-client-part-1/
Related
I am working on an application in which i have a requirement of detect call log info.
Means when ever a call coming and going and then after attending call that will disconnect. So i have detect and send a notification when call disconnect.
For this requirement i have done so much r&d and get some results but when i go through Apple's doc for core telephony framework then there is class "CTCallCenter". This class provide a event handler which will invoke app whenever a call state change.
Now problem is that when i go through document of that class then i get som texts which is shown below
To handle such call events, define a handler block in your application and assign it to this property. You must implement the handler block to support being invoked from any context.
link of apple doc for core telephony framework
In above text write down that u have to create a event handler and assign to property then it will handle call events.
So problem is that how I create a custom event handler and how make a property and assign to my custom event handler?
Thanks in advance...
Take a look at CoreTelephonyDemo. You will find your answer in there.
The event handler is a block. You can also look at Blocks Programming Topics
I'm wondering how to deal with this particular issue:
I'm creating a place object, which gets initialized with a geographical lat-long pair. Then I use the iOS geocoder to get an address for that coordinate. I want to set that address to one of my instance variables. However, this asynchronous call doesn't get completed in time, so when I instantiate my object and try to display the address, it hasn't been done yet. What are some strategies to deal with this and similar problems?
Thanks! Merry Christmas!
I don't feel like creating an extensive answer on Christmas Eve so I'll give a brief answer here for now, and edit it later if you've got questions and/or want more details.
Asynchronous requests all have delegate/protocol methods that let you know when the request has failed or succeeded. You should use the NSNotification API and register any object that needs the address for a notification that's triggered when the object completes the request. When the object receives the notification, it can then configure its views or whatever it needs to do. In the requestDidFinish (or whatever) method, you should send the notification.
Check out this article for details (as well as some cool stuff about threading!): http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial
I'm developing an iOS app which includes a search form. When a user clicks search, I want to use an NSInvocationOperation (which is fine) to spawn another thread. On this thread, I'll call my data layer (a separate class) to retrieve data from a web service. Is there any way I can pass the data layer a method in my ViewController subclass which should be executed on completion, along with the other arguments (search term etc)? Kind of like .NET's BackgroundWorker?
Sounds like a case for Blocks (iOS4+). There's quite a nice tutorial here and excellent documentation from Apple here. Blocks are perfect when you want to kick off an asynchronous task and pass in details of what to do when it has finished, so could well be appropriate in your case.
No offense, but I may be asking a strange question. I am a beginner, learning advanced OOP, and confused about a few concepts. Coming to the point. It may be ridiculous. But can someone tell me exactly and correctly what does callback literary mean? And how it differs from a proxy class in C++ which we use for information hiding. Apologies in advance, if I missused the terminology.
From Wikipedia:
A callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code.
As for proxy classes, see this question.
It's two different things. An instance of a proxy class can be used as callback, but that's about the only relation I can see between them.
The idea of a call back is to start some action then do something else until the action completes.
An example from the real world is to telephone the plumber for a repair. The plumber says they are currently at a work site and unable to see their schedule for the next day right now. However the plumber says that they will be in the office later and will be able to check their schedule. The plumber then promises to give you a call back once they are in the office and able to check the schedule. You could then either sit down next to the telephone and wait for the return telephone call (blocking call back) or read a book or paint the house until the plumber calls back (deferred call back).
In C, a callback function is a function body that is specified as a callback. The address of the function, a function pointer, is provided to the function being called to perform some kind of action. When the action is completed the callback function is invoked to do something, usually some form of cleanup and/or notification.
Normally a callback is used when a function is called to start some action and then returns immediately to the caller before the action completes and its result is known. The callback is used as a way to notify the result of the action when the started action is completed.
Another form of callback is to register a function for an event so that when the event happens, the function will be called to do something. So you might specify a callback for when a mouse click event is received.
A proxy class is a class that acts as an interface for a class. You could think of a proxy class as being similar to a stunt double who does the dangerous things for an actor. Or a proxy for a share holder's meeting is a person or organization who stands in for the actual share holder to perform specific duties for the share holder.
A callback is not a proxy though a callback may be used by a proxy as part of the functionality required to do its proxy duties.
hi i am developing a sms App for my client. so far i have put down this plan in place.
1)The app keeps polling the server with an asynchronous request so that it does not interfere with the user interface.
2) for sending sms i am currently using synchronous request , depending on the reply from server i have do various things. i am showing the spinning circle and make the user wait until i get the response from server.
my client has problem with point 2.
The client says as soon as the send sms button is clicked it has to go back to the homescreen and should be able to navigate to any screen and do all other things that the app offers. i could have used async request , but i am not sure how to handle the responses from the server when i am on different view controller other than the one request is called from.
Can somebody help me on this.
Thank You.
The classic way of handling the response of an asynchronous action is either using delegation or notifications. Do not use a singleton. This breaks modularity and decoupling of different view controllers.
Roadmap of how to handle asynchronous actions
Register for the response of the asynchronous actions. This can be setting the delegate of the requesting object e.g. NSURLConnection to the view controller, which is typically self in this context. The othe possibility is that you register for the notification which is fired by the requesting object if things have happend e.g. when a download is finished or an error occurred.
Implement the delegate methods or the notifications to update your model and/or your user interface. Be aware that updating the UI has to happen on your main thread.
Start the asynchronous action. What happens in the background is that a separate thread is spawned or an operation is dispatched using GCD. These are implementation details and do not bother you.
Wait for the answer, which will result in one of your implemented methods to be executed which you then use to update what has changed.
Difference between notifications and delegates
The two differences between delegates and notifications is that delegate is a one-to-one connection between the delegate and the delegating object. Notifications are posted application wide and can be observed by as many objects as needed creating a one-to-many connection. Think about this as a broadcast. The second main difference is that delegation can be used to transfer information back from the delegate to the delegating object. Meaning that the delegating object asks the delegate for certain information. Typical example would be the data source of an UITableView. Notifications however are a one way street. The information flows from the posting object to the observing objects. This makes sense because think about the situation where you would have more than one observer and each would give feedback to the posting objects. Which one would be the right one?
In your case you would have to look up the delegate methods of the asynchronous HTTP request object and implement them accordingly.
Maybe you can try ASIHTTpRequest , It has Sync an Async request
If you using Async request , you can do anything after you press a button to do a request .
Check this
The solution depends on the response processing.... if you are just showing user that the sms sending is failed/successful than you can do it in any general utility class that shows alert.. but for this you have to create the singletone instance of your connection class so delegate(class itself) don't die when the response comes back.......
For this we need to keep track of currentViewController ....... we can do this by creating a reference ........ id currentViewController in appDelegate(with setter/getters).......... so it could be accessible in everywhere application........
its referred object should be changed each time when user changes the viewController.... that will help us to know on which viewController user is currently working.
than when the singeltone class of connection finished its response loading we can use this currentViewController with your desired viewController.
I am not sure how you are using different view controller....... pushing it/ presenting it or adding its view.....