Ordering multiple Asynchronous JSON parses from website - iphone

Quick brief of what I'm doing: I have two arrays, they both contain 50% of the information for a table view. Why? Because one of the arrays pulls current information from the internet, while the other array has saved user data. I had no idea as to how to get current information from the internet in a non messy way, as I'm an amateur to objective-C let alone networking in Objective-C. Anyway, so the internet Array is pulling information that corresponds to the objects in the saved array (saved in Core Data) using AFNetworking. It's therefore Asynchronous, which I want. Now here comes the problem, or at least what I can garner from the situation.
I'm doing a for loop that effectively counts through the objects in the saved array and passes the unique ID for each object so that the corresponding information from the internet can be downloaded, parsed and added to the internet array. However, since the networking is Asynchronous, the loop is effectively downloading all the information from the internet at once. Therefore the objects are being written to the internet array in order of which downloaded first. Therefore savedArray[0] does not correspond to the object in internetArray[0]. This is a pretty big flaw as you can imagine, as when I'm inserting the values into my tableView, nothing really matches up/makes sense.
I'm really looking for a way to postpone the downloading of the information, until the previous download has been completed and added to the internetArray, how on earth do I do this? Now for the code. Here is where I get the appropriate key:
for ( int i = 0; i < [mySavedObjects count]; i++) {
MySavedObject* mySavedObject = [mySavedObjects objectAtIndex:i];
[self retrieveMissingInformation: myObject.Id];
[self.tableView reloadData];
}
And here is where I actually get the information (simplified for the sake of space):
- (void)retrieveMissingInformation:(NSString *) Id
{
// create URL here.
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Success");
// Do some safety checks & parse the JSON, returning a searchResult.
[searchResults addObject:searchResult];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
// Do some error messages etc.
}];
[queue addOperation:operation]; //where queue is an ivar NSOperationQueue.
}
Finally, in the cellForRowAtIndexpath, I these use both:
MySavedObject *mySavedObject = [mySavedObjects objectAtIndex:indexPath.row];
&
SearchResult *searchResult = [searchResults objectAtIndex:indexPath.row];
To get the values for the cell.
Sorry for the massively large wall of text. I'm not really good enough to explain things efficiently, often getting tongue-tied on the terminology and having to resort to making up my own exmaples. Any help with ordering this mess and I'd be really grateful.
Regards,
Mike.

Good work mate, you've stumble on a common design pattern.
As you've seen, with asynchronous request, execution happens almost concurrently so you're not guaranteed order when you think of "queues" in terms of a for loop.
The way you need to think of your download queue is in terms of recursion.
In other words, instead of:
// your current method of queue-ing the submission of data
for(int i = 0; i < arrData.count; i++)
{
[self doAsyncDataSendForIndex:i];
}
You need to start doing something like this:
// perform the first submit for the first element in your array
[self doAsyncDataSendForIndex:dataIndex];
// a completion callback method
-(void)asyncFinishDownloading
{
[self parseResult:serverReturnedData];
}
-(void)parseResult:(id)serverData
{
// do all the processing of the data that you need here
...
// update index for next round
dataIndex++;
// -----------------------------------------------------------------
// now at the end of this method, perform a recursive call
// of your async download method again
// -----------------------------------------------------------------
[self doAsyncDataSendForIndex:dataIndex];
}
Using the callback delegate way of queueing your download you are telling the code to only download the next data after it has finished processing the last asynchronous download.
Now you can use a number of libraries to help you with your asynchronous stuff. I myself use ASIHttpRequest and ASIFormData, you can use AFNetworking too which is newer.
-(void)asyncSubmitData:(id)someData
{
NSURL *url = [NSURL urlWithString:#"http://www.yourserver.com/...."];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// setup your POST request parameters
[request setPostValue:someData forKey:#"someKey"];
...
// setup the completion callback delegate action
[request setCompletionBlock:^{
// process your server returned data here
NSString *serverResponse = [request responseString]; // raw server response
...
// increment your index and call your download method again
dataIndex++;
[self asyncSubmitData:[myArray objectAtIndex:dataIndex]];
}];
// setup fail block in case the server fails
[request setFailedBlock:^{
NSLog(#"Server error: %#", [[request error] localizedDescription];
}];
// start the asynchronous request now
[request startAsynchronous];
}
Hope that helps.

I think I understand your problem and it sounds like you're using the wrong data structures to store the data.
You have one array that is generating requests and the results to those requests are stored in a separate, unrelated array.
As you generate the request for information with an objectId, can you store the results in a NSDictionary instead of an array and use the objectId as the key to this array?
You have the NSURLRequest in the success method so you should be able to retrieve your objectId from the request through the NSURL.query if it is a parameter on the query string

Postponing the downloading of info and effectively making all of your network requests serial (one at at time) would work, but isn't a very nice solution, because it's often handy to have concurrent network requests going on - for example, allowing four requests at once would probably take less time than making all requests serially.
A much better solution is to correctly handle a piece of data when it comes back. To do this you need to handle the returned data in a more structured way. Rather than just appending it to an array, do one of the following:
A. Put returned data in a dictionary. The dictionary can map from an index or appropriate key to the returned data. (Hint: if you want to put an int as a key in a dictionary, construct an NSNumber containing that int.) This option is recommended over B.
B. Insert the returned data into an array. In order for ths to make sense, before you do any fetching, you should fill up your array with placeholder objects that denote "this bit of data hasn't been fetched yet". One possible placeholder object choice would be NSNull - it's just an object to denote null/nil.
So then you can return the correct item in cellForIndexPath: by dipping into your dictionary or array.
These two data structure ideas are just the most obvious ones that come to mind, there are doubtless other ways to crack this nut.

Related

Using AFIncrementalStore with an Auth token

I'm working on an iOS client for App.net, and I'd like to use AFIncrementalStore to sync the web service with my Core Data implementation in the app.
I've gotten the two to work for API requests that don't require an auth token, but I can't figure out how to use the auth token with AFIncrementalStore.
In other words, I'm able to pull down the global feed, since it doesn't require auth:
https://alpha-api.app.net/stream/0/posts/stream/global
...however, to get a user's stream, you need auth (you'll note this link gives an error):
https://alpha-api.app.net/stream/0/posts/stream
I understand that I need to append an auth_token to the end of the API call, but I'm not sure how to do that using AFIncrementalStore.
Update: Here's the chunk of code that currently gets the global stream:
This is a method pulled directly from the example App.net project included in AFIncrementalStore.
-(NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest withContext:(NSManagedObjectContext *)context {
NSMutableURLRequest *mutableURLRequest = nil;
if ([fetchRequest.entityName isEqualToString:#"Post"]) {
mutableURLRequest = [self requestWithMethod:#"GET" path:#"stream/0/posts/stream/global" parameters:nil];
}
return mutableURLRequest;
}
All you have to do with the case above to alter it to correctly use an access token is add parameters to the request instead of passing nil.
To build the parameters simply make an NSDictionary with the keys and values you need. For example in some of my App.net code I have this
NSDictionary *params = #{#"access_token": token};
Using the new compiler directives this builds an NSDictionary with a single key (in this case 'access_token') with the value in the NSString token.
After you have this just make your request something like:
[self requestWithMethod:#"GET" path:#"stream/0/posts/stream" parameters:params];
To pass the parameters in the request.
Let me know if this isn't clear enough!

How to know if data is ready

I am using RestKit Framework to parse JSON data coming from a web service. Once I send the request URL the data will be downloaded automatically and will be available in one of the delegate methods of RestKit . I am writing this as a re-usable wrapper class, so that I create an instance of this class wherever required and pass only the URL to download and rest of the process will be done by the class. Now, I have a problem, how the instance will know that the data is available to use after download ? How can i achieve that ? NSNotification or delegation ? Any coding examples or suggestions might help. Thanks in advance.
I found that the most convenient way to handle the callback in RestKit was to use the completion blocks. You can pass around this completion block to achieve what you are trying to do.
Here is an example of how you can create a completion block and pass it around.
-(void)startRequest {
RKRequestDidLoadResponseBlock block = ^(RKResponse *response) {
//your completion code
};
[self sendRequestWithCompletionBlock:block];
}
-(void)sendRequestWithCompletionBlock:(RKRequestDidLoadResponseBlock)completionBlock {
RKRequest *request = [RKRequest requestWithURL:[NSURL URLWithString:#"www.google.com"]];
//configure request;
request.onDidLoadResponse = completionBlock;
}

MappingProvider's errorMapping always fails with: Could not find an object mapping for keyPath: ''

I am experiencing difficulties mapping my API's errors. I am loading requests using blocks like this:
[self.objectManager loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
loader.onDidLoadObjects = ^(NSArray *objects) {
// ...
};
loader.onDidFailLoadWithError = ^(NSError *error) {
// ...
};
loader.onDidFailWithError = ^(NSError *error) {
// ...
};
}];
On the server side if, eg, I attempt to authenticate a user with wrong username/password, my API returns and error with the following format:
{ "error":
{"code":"401",
"message":"Wrong username etc. etc."}
}
In the object mapping docs I have found the following:
RKErrorMessage
A simple class providing for the mapping of server-side error messages
back to NSError objects [...] When an RKObjectManager is initialized,
object mappings from the "error" and "errors" keyPaths to instances of
RKErrorMessage are registered with the mapping provider. This provides
out of the box mapping support for simple error messages. [...]
Which I interpret as the object loader automatically will map any "error" og "errors" keyPath's accordingly, and didFailWithError: (or block) will get called. This is not the situation, though. I get this error every time an error response is loaded from the API:
Adding mapping error: Could not find an object mapping for keyPath: ''
I have tried solutions from this questions and this question, but the result is the same.
I know the json is loaded from my API, I have tracked that in the debugger. Also, if I create an RKObjectMapping and maps it to the "error" keyPath, onDidLoadObjects block is fired with an Error object, but this is not what I intend to do.
Long story short, my goal is that make RestKit fire an onDidFailWithError block whenever an "error" keyPath is returned by my API.
From your question I would assume the problem to be with your server the error response with a 2xx status code.
Other possibilities are either using a version of RK predating the docs you mention, or you might have found a bug :)

RestKit - Appending a parameter to all requests

I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request sent using the RKObjectManager's methods.
I've tried creating a Category overload of NSManagedObject in order to use the following method:
- (void)willSendWithObjectLoader:(RKObjectLoader *)objectLoader
That works fine, however I see no way of appending to the object loader's params. I've even gone as far as to reserialize the object but there is no way to do that without it being sent using escape characters. For example, the following code will give me the object in JSON serialized form
RKObjectMapping *serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[self class]];
RKObjectSerializer *ser = [RKObjectSerializer serializerWithObject:self mapping:serializationMapping];
NSObject<RKRequestSerializable> *obj = [ser serializationForMIMEType:RKMIMETypeJSON error:nil];
But the object returned there is intend to be used as a param right away so the following code does not work
[params setValue:[LCSingletonLoggedInUser sharedLoggedInUser].sessionToken forParam:#"token"];
[params setData:obj.HTTPBody forParam:#"data"];
I've also tried various combinations of setObject and setData and obj.HTTPBody as well as just obj right away.
Actually appending obj to params in any way will always result in it adding escape characters which the server can't handle. Setting params = obj will give the correct values to the server but won't include the token.
How about adding it to queryParams?
NSString *resourcePath = [#"/products" stringByAppendingQueryParameters:_queryParams];
Where queryParams is a NSMutableDictionary where you add your token.

Can more than one Web Services be called at the same time from iPhone?

I have made a social networking application and I m calling various Web Servies to get the Users's different data like the Friend list, the latest updates etc.
But while I m calling one Web Service in background, and then make call to another, the first one stops, no response comes back for the first one...
What should be done for that?
Is it not possible to call two Web Services at a time??
I am not sure how you are calling services. If you will show your code, any one can help you more...
you can try this -
- (NSData *)fetchProfileData:(NSString *)accessToken{
NSURL *url = [NSURL URLWithString:serverURL];
request= [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSError *error;
NSHTTPURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
return responseData;
}
to call other web services create separate separate functions for each service like
- (NSData *)fetchFriendsList:(NSString *)accessToken{
//write your logic, you see above method for more details
}
- (NSData *)fetchUpdates:(NSString *)accessToken{
//write your logic, you see above method for more details
}
call above methods from your view controllers like
[NSThread detachNewThreadSelector:#selector(callFetchProfileDataServices:) toTarget:self withObject:#"userToken"];
-(void)callFetchProfileDataServices:(NSString*)token{
NSData *response = [self fetchProfileData:token];
//now parse response data data using suitable parser
}
You need to create separate separate threads for each services and you may save these data in member field also .