I am creating an iOS app that consumes web services.
I have a class that makes the connections and stores the response in a variable. It also has a status variable where 1 indicates successful connection.
I have set up an NStimer and a function to check when the connection and download is done and if it was successful.
My question is:
Is this a proper way to manage the connection and its outcome?
any suggestions?
Here is the programming guide from Apple Developer website and it describes how to use NSURLConnection delegate. You can manage the received data in connectionDidFinishLoading: method. Notice that using these delegate methods will load data asynchronously. If you want to handle data synchronously, please try sendSynchronousRequest:returningResponse:error:, but this function should never be call in the main thread.
Related
I'm attempting to adopt AFIncrementalStore for a Mac app that talks to App.Net. Unlike the example applications that come with the framework, I'm using the streaming APIs, with a websocket connection. For this I was using SocketRocket. These parts are working fine: I'm able to set up a request connection to ADN and get a connection ID back. It's this connection ID I supply to the later requests to ADN APIs.
My problem is that the Core Data stack is initialized and firing before I get my first connection ID back from ADN. I'm not sure how to handle this situation.
Currently, I have this code in my app delegate:
self.socketConnection = [[MUNConnectionManager alloc] init];
self.socketConnection.delegate = self;
My connection manager implements a delegate that calls back to the app delegate when the connection ID has been received:
# pragma mark MUNConnectionManager delegate method
- (void)didReceiveConnectionId:(NSString*)connectionId
{
self.connectionId = connectionId;
}
So once this connection ID is received, that's when I'd like to boot AFIncrementalStore into action. But this is perhaps a full second or so after launch, and my AFIncrementalStore client is already crapping out because it doesn't have that connection ID.
Any suggestions appreciated!
I think I may have found the answer to this. In my XIB I have an array controller with the "prepares content" checkbox on. That would have triggered the data store and loaded up all the Core Data stack. When I uncheck that box it doesn't load, and my ADN delegate is free to pull the ID.
So if anyone else runs into this, the answer is the CD stack doesn't load until you try to hit it.
I was hoping to capture AJAX post requests, and I have read this thread:
UIWebViewDelegate not monitoring XMLHttpRequest?>
However I was hoping there were some way to achieve this using Obj c without going through javascript. I already have a UIWebView setup along with its delegates. But since that doesn't capture XMLHttpRequest, what should I implement to achieve this and where? Thank you.
Take a look at NSURLProtocol - this should enable you to intercept the requests.
Basically you would
- create your own class derived from NSURLProtocol and register it in applicationDidFinishLaunchingWithOptions: using NSURLProtocol:registerclass:
the connection will call initWithRequest:cachedResponse:client: and then startLoading.
you would call the connection back with URLProtocol:didReceiveResponse:cacheStoragePolicy:, some number of calls to URLProtocol:didLoadData:, and finally URLProtocolDidFinishLoading:
Once you have intercepted the calls you can allow them to procede, evesdrop on them, stop them from going forward, etc.
I'm building an app, essentially a very basic Facebook client using the Facebook SDK for iOS. I have implemented the FBRequestDelegate protocol, and am sending my requests to the Open Graph API in the following way
[facebook requestWithGraphPath:#"me/friends" andDelegate:self]
This works fine, and once the requested content arrives, the SDK calls the delegate method, passing in the data
- (void)request:(FBRequest *)request didLoad:(id)result
My question is, does anyone know the best practice way to use the FBRequest passed into the delegate method to explicitly identify it as coming from my original request?
If I make several requests, how can I differentiate which request the delegate call is related to? I'm using BOOL flags, and only doing one request at a time at the moment, but it's not a good way of doing it.
Any help much appreciated :)
The method requestWithGraphPath returns a FBRequest object which will be also passed to request:didLoad later. So you can simply compare the two objects and determine if they're the same.
I am try to handle multiple NSURLConnection at the same time by using a different delegate for each connection, for each NSURLConnection I create, I create a new delegate object, but for some reason only one NSURLConnection works at a time, any NSURLConnection I try to start whilst one is already running simply do not start, my delegate does not receive any of the method calls connection:didReceiveResponse:, connection:didReceiveData:, connectionDidFinishLoading: or connection:didFailWithError:. Am I misunderstanding something about how NSURLConnection and its delegate works. Reading other posts most people seem to have a single delegate for all there connections and then use some kind of dictionary to get the right object to handle the right connection. Is that the way you have to do it.
What you are describing should work fine. You can have multiple NSURLConnections in flight at once, each with its own delegate.
If you want to know why your case isn't working, you'll probably need to show your code in your question.
I hope that at least answers the general question.
I have written an iOS app that calls NSUrlConnection multiple times to download image data from the web. Sometimes, one NSUrlConnection has not finished before the other starts. I am seeing corrupt jpeg data and I think it is because my didReceiveData delegate is saving data from two separate NSUrlConnections and munging the two jpeg data streams together into one data variable, hence causing the corruption.
My question is: what is the best way to avoid this? There doesn't seem to be a way to make each NSUrlConnection instance save to a separate data variable, or make each instance wait until the previous instance is done before saving.
My code basically follows Apple's example here except I call a loadData function multiple times which creates the NSURLRequest and NSURLConnection. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
Thanks in advance for any help.
When your delegate's connection:didReceiveData: method is called, you'll have the connection instance as the first parameter. So you'll need to use that to keep track of which connection just received data.
Apple's sample maintains one instance of NSMutableData. Your code will require several instances, one for each active connection.
Or, of course, you could have a separate delegate object (an individual instance) for each connection. That may be easier.