Tracking individual requests to Open Graph through iOS SDK - objective-c

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.

Related

How to perform non-asynchronous AFHTTPRequestOperation with AFNetworking?

Having followed the Ray Wenderlich Parse + Core Data + AFNetworking tutorial, it seems that he pulls JSON from Parse by creating an AFHTTPRequestOperation object using an NSMutableURLRequest and then adds that operation to a queue via enqueueBatchOfHTTPRequestOperations:(NSArray *)operations progressBlock:(void (^__strong)(NSUInteger, NSUInteger))progressBlock completionBlock:(void (^__strong)(NSArray *__strong))completionBlock.
My question is: Is it possible to process an AFHTTPRequestOperation immediately (rather than adding it to queue) so that the subsequent line of code processes once the request has been fully processed? Is this bad form?
What I am actually trying to do: If a requested object does not exist locally, then attempt to download it from Parse. If that request fails, assume (for now) that the object does not exist on Parse. If that request succeeds, then the user can now start using that object locally.
Advanced apologies if this is a dumb question as I am new to AFNetworking and any kind of data synchronization...also the few iOS developer friends I have have never used AFNetworking / Parse / any kind of data synchronization.
Most people consider it bad form, although I understand wanting to try it when you are getting started. I would advise never doing it in code you are submitting to the App Store. Some discussion about the same thing here: Synchronous AFNetworking calls

Cancelling an NSURLRequest

Is there a method provided in the native SDK to cancel the URL request made?
I know this method:
[connection cancel]
But I am wondering if we can cancel a request. I only want to use the native SDK and I am not allowed to use the third party libraries.
Also is there a way to track the progress of the download with the native library?
For the first part, you need to call cancel on the NSURLConnection as [connection cancel].
As per NSURLRequest documentation:
NSURLRequest encapsulates two basic data elements of a load request:
the URL to load, and the policy to use when consulting the URL content
cache made available by the implementation.
And as per NSURLConnection documentation
An NSURLConnection object provides support to perform the loading of a
URL request. The interface for NSURLConnection is sparse, providing
only the controls to start and cancel asynchronous loads of a URL
request.
So you are canceling a URL connection and not a URL request.
For the second part, check the NSURLConnectionDownloadDelegate Protocol Reference. It has the following delegate methods for this purpose.
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
– connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:
– connectionDidFinishDownloading:destinationURL:

Using NSMutableURLRequest and how to manage response/failure

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.

XMLHttpRequest in Objective C

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.

Which NSURLConnection wrapper handles GET and POST equally well?

Which NSURLConnection wrapper handles GET and POST equally well ?
For GET method, I prefer google's GTMHTTPFetcher over ASIHTTPRequest. ASIHTTPRequest uses delegate, which probably the idea you will come up with normally. But that's the exactly reason I chose not to use it because when you have several connections(many connections in my case), then each connection has its own delegate and you end up with too many object. Or you can have just 1 delegate but you have find a way to find out which response is for which connection.
GTMHTTPFetcher handle this way much better in my opinion. It uses 1 SEL for 1 connection, sorta like target-action model. The code is much cleaner than delegate model.
But for POST method, ASIHTTPRequest has ASIFormDataRequest. I did not find an easy way to do POST with GTMHTTPFetcher. It does have setPostData method to set post data. But you have to set post body and those mime parameters by yourselves(from what I have see) And that's the headache. I find it has another class called GTMHTTPUploadFetcher. But I can't really figure out how to use it (I keep getting the NSAssert "need upload location hdr").
So for POST, I guess ASIHTTPRequest is easier.
I did not get a chance to use facebook-ios-sdk. And would like to hear other opinion about it.
So is there NSURLConnection wrapper handle both GET and POST well ? And any idea how to use GTMHTTPUploadFetcher?
The GTMMIMEDocument class is used to create a stream for uploading via GTMHTTPFetcher (or via anything else that takes an NSInputStream.) An example is here.
GTMMIMEDocument keeps a sparse list of the data parts to be uploaded, avoiding duplicating the data in memory.