How to use Trampoline IOS - iphone

I was looking for this on google and I found some articles on it They say it is used for HigherOrderMessaging and I tried to read the code to but everything was over my head can any body give simple example of it how we can use them? They were saying its used for passing returned object from method to another object. And another question when I develop apps never came situation where I need to use something like this.

In Objective-C, a trampoline is an object returned by a method that exposes some kind of message interface. When messages are received, it bounces the message on to another object.
Example One:
Return a proxy of a service client. When methods are invoked on the proxy, it first checks if the user has permission to proceed.
Example Two:
Make all the objects in an array do something:
[[windowsArray do] setHidesOnDeactivate:YES];

Related

How to create 'globally' scoped variables that are global only during one request?

When a request hits my dancer2 app I want to set up an object that is accessible by all packages involved in handling this request.
How can I make sure that this object has a scope only within this individual request and is not accessible by other requests?
Specifically this object should be filled with messages of all kinds (errors, warnings, debug messages, etc.) as execution travels through my libraries.
Obviously, those messages are request-specific and I am afraid that naively declaring a global reference to this message object is exposing it to all requests hitting the app.
I was thinking about creating an instance of this message class in the router and then passing a reference to it throughout all methods involved in handling this request.
My gut feeling tells me that I am missing something fundamentally here architecture-wise regarding dancer2, so I decided to ask you. It's my first post here, by the way, so I apologize for any shortcomings my question may have.
It looks to me like you could use a var to hold your object.
See https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#var
If you need it to be accessible even from methods that aren't aware of Dancer, you could use a var and also store your object in a global variable using a weak reference.

Asynchronous Calls (specifically iOS geocoding)

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

difference of proxy function and call backs

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.

How do I make a reusable web interface class in objective C?

I'm working on a new iPhone/iPod app that includes the need to do web services requests. I've found methods for doing these requests synchronously, or asynchronously by setting the controller as the delegate. What I'd really like to be able to do, though, is to create a single class that can handle all web requests for the whole application, and just create an instance of that class when I need to use it. That way, cookies and common pieces of code can be handled in one place, rather than all over the app.
So far the only thing I thought of that could accomplish what I'm trying to do is to create a new thread that handles the request synchronously within itself, then sends a message back to the calling controller once the request is complete. Is there a better way to accomplish what I'm trying to do?
Cookies are already a shared resource.
I would suggest reading the URL Loading System Overview to get an idea of how Apple set everything up. From what you describe, you want something very similar to how they have set up the system, maybe with a Singleton class for the connection. You can also look at ASIHTTPRequests which is a good wrapper around all of the connections stuff.
I would not suggest writing my own code here. Lots and lots of people have solved this problem for you.

iphone RESTful webservices

Not even sure if the title is correct, however, what I'm trying to do is use the standard NSURLConnection class to handle responses from calling my webservice. I am using the POST method to update a database and the GET method to retrieve rows from the database. The problem I have is that these 2 actions may occur simultaneously so that the methods to handle the request may step on each other. In other words in my "connection didReceiveData" method I have 2 paths through the code depending on whether I'm handling a response from a GET or POST request.
I keep track on which request in being processed by an instance variable called requestType. The problem is that since the requests are being executed simultaneously the instance variable is being changed from GET to POST before the GET completes (or vice-versa). My question is how do I maintain 2 separate requests in this scenario? Do I synchronize the requests? Is there a way for threads to work here? Do I create 2 separate objects for each of the requests and have the logic in "didRecieveData" query for which object is being processed? Any help would be much appreciated!!
Dealt with a similar issue in one of our apps. The solution involved creating a class that represents a webservice call, responsible for calling its own url, loading its own data, etc. The call class had a delegate that would handle parsing the responses (in our case, a web service controller). Wound up getting rather complicated, but prevented the issue of NSURLConnections stepping on each other.
Seems like you've created a messy problem by having a class that tries to do too many things. I would suggest taking one of the following three approaches:
1) Write two classes, one for updates and one for retrievals. Each class creates it's own private NSURLConnection object and acts as the delegate for the async notifications received from the NSURLConnection. The classes could possible share some utility parsing code or extend a base object that has that parsing code in it. But the key being that the code calling these classes would instantiate one of them, make the call, and then release it. This will keep your code cleaner and will insure that the event notifications don't get intermingled.
2) Create a single class that, depending on initialization, does either a post or a get with it's own private instance of NSURLConnection. When a call needs to be made, instantiate the class, get the results, and then release the class.
3) Write your connection handling classes so they use the synchronous NSURLConnection method and call that call that class in a background thread.
Either way, clean code and clear object orientation will prevent messy scenarios like the one you're describing.
Create separate objects that handle the calls. If you want to issue multiple requests at once I would strongly recommend looking at NSOperationQueue, and making these objects subclasses of NSOperation... much nicer way to deal with multiple background requests.
A good example is here:
http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/
The idea there is that you use the non-asyncronous web calls, in operations that are run on separate threads. You can still use asynch calls in NSOperation as well, but doing so is a little trickier and for simple calls you probably do not need to.