Asynchronous Calls (specifically iOS geocoding) - iphone

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

Related

observer design pattern in rest aplications

I'm trying to learn design patterns, and I have come with the Observer pattern. I think I understand the concept itself, but I don't see when to use it.
I try to explain myself. I work mostly with web applications, so, stateless applications. Normally, the client makes a petition from the browser (for example, update a record). then the operation is completed.
Let us suppose that I want to notify some persons every time a record is updated. It seems to me the perfect scenario for the Observer patter, but when I think of it, it will end something like:
user makes petition of update.
get all the persons that have to be notified.
put all the persons that have to be notified in the observer.
make the update. (that also will make the notification for the observer pattern).
but... doing it this way, I have to iterate all the persons that I want to notify twice!
And because its a stateless application, I have to go and get all the persons than needs to be notified every time!
I don't know if the observer pattern is more useful for other types of applications, but I can only think of this pattern in a static form, I mean, making the Observer static.
I know I'm losing something, it's a common and accepted pattern, everyone accept it as a valid solution for this concrete problem. What I am not understanding?
First, let's straighten out the terminology.
Each person who wants to be notified is an Observer.
Each type of event which can trigger a notification is an Observable.
Each Observer (person) needs to register itself with the server. It sends a request essentially saying, "I'm interested in foo Observables," which in this case would be, "I'm interested in update events." The server maintains mappings of who is interested in which events.
Every time the server makes an update, it iterates over the mapping of update Observers and sends a notification to each of them.
The advantage is that the server and its Observables have no compile-time knowledge of who the Observers are. Observers are free to register (and unregister) themselves at runtime for any event(s) they are interested in.

How to use Trampoline IOS

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];

Posting location updates in background from iOS4

Can i send location updates to my server in background from iOS4?. I want to get users location changes and post it to a server using a web-service call. Main question here is, is it possible to call a web-service or http-post while app is in background?
Absolutely, and you may want to review this earlier thread when I asked the same question - credit due to #RedBlueThing for providing the key information.
I've written this up as an example on our blog as well. The key to this working is beginBackgroundTaskWithExpirationHandler: without that you won't get reliable results as it will perhaps work some of the time, but not others.
Yes. You can basically do anything you want in the background callbacks.
I'd like to add that you should make sure you are aware of the possible impact on battery life, as the antennas may have to be turned on to perform your requests. Perhaps you could store the locations in core data, and post it to the server when the app resumes.
But there's no technical reasons to not do it, and afaik it will pass review too.

asynchronous request objective c

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.....

How would I get SMS/Phone events, and act on them before they're sent to the system apps?

I'm trying to develop an application for a jailbroken iPhone similar to Mcleaner or iBlacklist.
What I want my app to be able to do is intercept SMS or Phone events and handle them before they're sent to the appropriate receiver. So for example, I want the application to get an SMS event, compare the sender to an array of numbers, and either process the data or ignore it and pass it off to the SMS app.
In MCleaner, you can define a blacklist of numbers, and if a text message is received that matches the blacklist the user won't get an alert from the phone and the data is instead handled by MCleaner.
How would I go about getting these events, and further, how would I get these before the appropriate apps receive them? From what I understand, I'd need to become a first responder for these things, but I really have no clue where to start. I can't seem to find any hints on how I'd go about this as this app will not be calling apple classes to get the data..
Thanks.
I can only guide in with this, sorry, the code isn't mine to give.
In memory, all the action calls are all just pointers. What you have to do is over ride the pointer to make it point to your own function. Once you do that, you do w/e you want in your function, and then call the default function call. This ensures the integrity of the action calls and allows you to intercept the actions.
You need to use the CKMessage class.
Check this out: http://hexorcist.com/private_frameworks/html/interface_c_k_message.html
and this: http://www.ifans.com/forums/showthread.php?t=232745