fetch JSON data asynchronously - iphone

I want to fetch JSON data asynchronously. The data is set up in a way that one request will bring only 8 records. I need to send the requests repeatedly until the response becomes empty or returns less than 8 records.
Currently, I have these methods in myviewcontroller.m class:
(void)myCallback:(id)sender {
MyDataRequest *objMyDataRequest = [[[MyDataRequest alloc] init] autorelease];
objMyDataRequest.myRequiredVariableToGetAuthTokenDataResponse = classOfMyCallBack.someVariable;
// Initiate getAuthToken request
[objWishListRequest initiateGetAuthTokenRequest:self requestSelector:#selector(getAuthTokenDataResponse:)];
}
Now here is the definition of getAuthTokenDataResponse:
(void) getAuthTokenDataResponse:(NSData *)data {
NSString *stringResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *objDictionaryForStringResponse = [parser objectWithString:stringResponse];
[stringResponse release];
[parser release];
MyListRequest *objMyListRequest = [[[MyListRequest alloc] init] autorelease];
objMyListRequest.myRequiredValueToGetMyDataResponse = [objDictionaryForStringResponse objectForKey:#"Data"];
// Initiate GetMyDataResponse request
[objMyListRequest initiateGetMyDataRequest:self requestSelector:#selector(getMyDataResponse:)];
}
(void) getMyDataResponse:(NSData *)data {
NSString *stringResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *objGetMyDataRootDictionary = [parser objectWithString:stringResponse];
[stringResponse release];
[parser release];
NSDictionary *dataElements=[objGetMyDataRootDictionary objectForKey:#"Data"];
Wish *objMyData;
for (NSDictionary* objGetMyDataRootDictionary in dataElements) {
objMyData = [[Wish alloc]init];
//add different elements from dataElements into member variables of object objWish
[self.myDataArray addObject:objMyData];
[objMyData release];
}
[self.myDataTableView reloadData];
}
This method lies in MyDataRequest class:
(void)initiateGetMyDataRequest:(id)requestDelegate requestSelector:(SEL)requestSelector{
// Set the delegate and selector
self.delegate = requestDelegate;
self.callback = requestSelector;
NSString* unescapedUrlString = [NSString stringWithFormat:#"http://test.mytesturl.com/core.svc/alldata/My/get/All/?token=%#&search=&page=1",myRequiredtokenparameter];
[self request:url];
}
I need to send multiple requests to the same url (with different parameter value i.e. value of page number) to fetch the results. How may I achieve it given the above scenario? The calls must be asynchronous.
How should I make the actual flow between all these calls? How may I get the data of "all the pages" asynchronously?

I think you are looking for a operation queue. I use ASIHTTPRequests in my apps and they work.
If you want to use this library, here's the link how to use it: Show UIActivityIndicatorView when loading NSString from Web

Related

working with json data

I have the follow code that parses JSON data received from a server:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSArray *array_webdata=[[NSArray array] init];
NSString *searchStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
array_webdata = [parsedata objectWithString:searchStatus error:nil];
NSDictionary *usersList = [array_webdata valueForKey:#"results"];
//I think that is not a real NSDictionary because if I write NSArray *keys = [usersList allKeys]; the execution crashes
NSLog(#"\n usersList =\n %# \n", usersList);
[searchStatus release];
[connection release];
[webData release];
[pool drain];}
the json data stored in usersList has the structure:
(
{
createTime = "date hour";
fullname = "user name";
"prof_id" = number;
thumb = "image.jpg";
},
{
data of rest of users...
}
)
And I would like create a class to store the data of each user and use "prof_id" when I want to use a particular use.
I need this because the app needs a list with all users (not tableview) and I think this is de easiest way.
Can someone help me? Thanks!!
Please used JsonKit Framework to parse json data received from web service.
Read data and parse using JSONKit:
NSData* jsonData = [NSData dataWithData:webData];
JSONDecoder* decoder = [[JSONDecoder alloc]
initWithParseOptions:JKParseOptionNone];
NSArray* json = [decoder objectWithData:jsonData];
After that, you'll have to iterate over the json variable using a for loop.
Create new class with the name User (file->new->file) inherited from NSObject class, create required parameters in .h/.m file.(do synthesize to generate getter/setter for attributes)
import User.h in your connection class and create objects of User entity in iterator loop and add those object in global scope array.
for(NSDictionary *userInfo in json) {
User* user=[[User alloc] init];
user.fullName=[userInfo valueForKey:#"fullname"];
user.prof_id=[[userInfo valueForKey:#"prof_id"] integerValue];
// Add into your global array
[usersList addObject:user];
[user release];// if ARC is not enable
}
// Check for successful object creation
NSLog(#"USER LIST contain User class Objects- %#",userList);
if i'm not wrong the only thing you need to do is :
NSMutableArray *yourArray = usersList;
and then with a for loop like
for(int i = 0;i<[usersList count] ;i++)
{
NSMutableDictionary *yourDictionary = [usersList objectAtIndex:i];
int prof_id = [yourDictionary valueForKey:#"prof_id"];
}
you can get your prof_id like that.
i hope this helps...
Use JSON Framework, and parse data using below code.
NSString* newStr = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"yout link to json file"] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"new str - %#",newStr);
NSArray *response = [newStr JSONValue];
NSLog(#"json array - %#",response);
Use the response array to show your results.

how parse value of array of one class to other and other to another?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1 && indexPath.row == 0) {
NSString *requestString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/xml?origin=%#&destination=%#,OK&sensor=false",startField.text,endField.text];
NSURL *url = [[NSURL alloc] initWithString:requestString];
NSData *tempData =[[NSData alloc] initWithContentsOfURL:url];
NSString *Str = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(#"%#",Str);
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
xml1 *parser = [[xml1 alloc] init];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success){
NSLog(#"No Errors");
arySteps=[[NSMutableArray alloc]init];
arySteps=[parser.ListofSteps1 mutableCopy];
}
else
NSLog(#"Error Error Error!!!");
controller.arayStep=[[NSMutableArray alloc]init];
controller.arayStep=[parser.ListofSteps1 copy];
controller= [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
In above code i have parsed XML data. In XML-parser file i have store objects in array that is ListofSteps1. Now i access in root-view controller class which is shown in above code. So in root-view class ListofSteps1 assign value to array of root-view controller. Now i want to assign value of array of root-view controller to next view's array which is also maintain in above code but array of next view is not getting value. What is problem in this code so it not getting value?
There are a couple of things wrong with your code above. You alloc and init a fresh array only to write over it on two separate occasions in your method implementation:
arySteps=[[NSMutableArray alloc]init]; // This line serves absolutely no purpose
arySteps=[parser.ListofSteps1 mutableCopy]; // This line is sufficient on its own
Calling accessor methods on an null pointer will do you absolutely no good. You need to instantiate the object before you attempt to set its properties:
controller= [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
controller.arayStep=[parser.ListofSteps1 copy];
These mistakes are very basic and indicate that you need to work on the fundamentals before you can get too much further. I would suggest that you pick up a good book on Objective-C - for example, Programming in Objective-C by Stephen Kochan - and read it thoroughly. A solid understanding of the core concepts will serve you well going forward.

NSURLRequest converting NSData to an array

I need to convert data received from the web via an array in a PHP script into an array that I can pull values out of. Here's my code!
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//NSString *payloadAsString = [NSString stringWithUTF8String:[receivedData bytes]];
NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];
[payloadAsString finishEncoding];
verified = [payloadAsString objectAtIndex:0];
NSLog(#"logging");
//NSString *no = [[NSString alloc] init stringWithCString:verified];
NSLog(#"%#", verified);
if([verified isEqualToString:#"admin"]){
NSLog(#"test admin");
[self performSelector:#selector(changeViewAdmin) withObject:nil afterDelay:0.05];
}
if([verified isEqualToString:#"user"]){
NSLog(#"test user");
[self performSelector:#selector(changeView) withObject:nil afterDelay:0.05];
}
if([verified isEqualToString:#"No"]){
NSLog(#"test no");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Invalid UserName/Password combination!"
delegate:self
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[payloadAsString release];
//NSLog(#"%#", verified);
// INSERT GOOGLE MAPS URL REQUEST HERE
/*if(requestType == 1){
NSString* addressText = payloadAsString;
// URL encode the spaces
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", addressText];
// lets throw this text on the log so we can view the url in the event we have an issue
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
// */
//
//}
[connection release];
self.receivedData = nil;
}
Unfortunately, my console returns null and asks if I've put the -finishencoding method in. Question is, if that's correct, where would I do so?
PS: Another question, is if I'm retrieving an array of data from a database, is a PHP script the best way to go? Thank you.
1) Of all this code the only string relevant to your question is
NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];
I really doubt that PHP script returns you data in NSKeyedUnarchiver-compatible format. I believe the only reason you don't get NSInvalidArgumentException exception from this method is that receivedData is nil (did you initialize it anywhere?). Try to make a string from what you receive like this
[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]
and log it. From this I hope it will be clear how to parse response.
2) Do not name NSArray instances like 'blahBlahString'. Strings and arrays are completely different.
NSKeyedUnarchiver can only unarchive instances which are produced by instances of the NSKeyedArchiver class.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedUnarchiver_Class/index.html

NDictionary getting autoreleased even after retain or copy

I am using following method to get back an NSDictionary object in ViewDidAppear. But when I attempt to access it in CellForRowAtIndexPath() it is always nil. I have tried adding an extra retain and copy to it, but it still gets released. I have been pulling my hair for 3 hours now. Any help would be appreciated.
Excerpt :
#property(nonatomic, retain) NSDictionary* userInfoObj;
- (void) viewDidAppear:(BOOL)animated
{
[super viewWillAppear:animated];
**//The object has data in it at this point**
self.UserInfoObj = [self getUserInfo];
}
- (NSDictionary*)getUserInfo
{
JsonHelper *helper=[[JsonHelper alloc] autorelease];
NSString* apiURL = [self.appDelegate urlGetUserInfo];
apiURL = [apiURL stringByReplacingOccurrencesOfString:#"{user_id}" withString:[UserSettings lastLoginUserId]];
return [helper getJsonDictionaryFromWebMethod:apiURL];
}
- (NSDictionary*)getJsonDictionaryFromWebMethod :(NSString*) url
{
.....
.....
....
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
dict = [[parser objectWithString:json_string error:nil] retain];
return dict;
}
Try putting self.UserInfoObj = [self getUserInfo]; in the viewDidLoad delegate method instead.

XMLParser delegate and memory leaks

I am building an application that has to handle the parsing of several different kinds of XML files. As I want to standardize the procedure as much as possible, I created a singleton class which handles the parsing of any XML data. The class is handed two options, a unique identifier telling it what kind of XML data it is going to parse and the data itself. Within the class, there is the following function which does the parsing and returns an NSMutableArray object containing the results:
- (NSMutableArray*) initAPIDataParse:(NSData *)data APIRequestType:(int)requestType {
// Init parser
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// Set delegate for parser
if (requestType == kXMLParserTypeOne) {
[parser setDelegate:[[[XMLParserOne alloc] init] autorelease]];
} else if (requestType == kXMLParserTypeTwo) {
[parser setDelegate:[[[XMLParserTwo alloc] init] autorelease]];
} // etc.
// let's parse the XML
[parser parse];
[parser release];
return lastParsedDict; //lastParsedDict is the NSMutableArray object returned from the Parser delegate
}
Above code works like a charm, except that if you parse the same type of XML multiple times this line leaks (which makes sense):
[parser setDelegate:[[[XMLParserOne alloc] init] autorelease]];
I have tried several things to resolve this issue like creating an instance variable with an instance of the delegate and releasing it whenever the same type of XML Parser was asked for again, yet it didn't work.
I am glad for any help here, thanks a lot!
Delegates objects are never retained. So using an auto-released object for them is mostl likely going to end up in a crash.
I refactored your code:
- (NSMutableArray*) parseData: (NSData*) data withAPIRequestType: (int) requestType
{
NSMutableArray* result = nil;
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
if (parser != nil)
{
switch (requestType) {
case kXMLParserTypeOne:
delegate = [XMLParserOne new];
break;
case kXMLParserTypeTwo:
delegate = [XMLParserTwo new];
break;
}
if (delegate != nil)
{
[parser setDelegate: delegate];
[parser parse];
result = [delegate.result retain];
[delegate release];
}
[parser release];
}
return [result autorelease];
}