I need to parse some XML that triggers an NSURLConnection. After the parsing finished, I receive some data and then I set the root view controller. My problem is that application:didFinishLaunchingWithOptions: returns before the composeRootController method and an error occurs because the application cannot find any root view controller. How can I wait until composeRootController returns?
My code is the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self xmlConnect];
return YES;
}
here xmlConnect function is implemented for parsing
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//convertim la data a string
NSString *receivedDataAsString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(#"connectionDidFinishLoading %#", receivedDataAsString);
//xml parsing
xmlParser = [[NSXMLParser alloc] initWithData:receivedData];
[xmlParser setDelegate:self];
self.receivedData = nil;
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
[self composeRootController];
}
here composeRootController sets rootcontroller
Put your
[self composeRootController];
inside
didFinishLaunching...
And have composeRootController call xmlConnect.
You might have to move the xmlConnect method out of your app delegate.
Related
I need to make multiple NSURLConnections to a JSON Web Service. I would like each WS call to keep in UI informed, probably with a UIActivityIndicatorView and label. So far I've created a NSURLConnection helper class to handle the connection and placed the URL delegates in the View. This works great for updating the UI with a single WS call.
For multiple calls, I'm trying to use an NSOperationQueue. I'd like to setMaxConcurrentOperationCount to one on the queue so that each Operation executes one at a time. Here's the relevant code on my View Controller:
ViewController.m
#import "URLOperationHelper.h"
#implementation ViewController
- (IBAction)showPopup:(id)sender
{
// Dictonary holds POST values
NSMutableDictionary *reqDic = [NSMutableDictionary dictionary];
// Populate POST key/value pairs
[reqDic setObject:#"pw" forKey:#"Password"];
[reqDic setObject:#"ur" forKey:#"UserName"];
operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:1];
[operationQueue cancelAllOperations];
[operationQueue setSuspended:YES];
URLOperationHelper *wsCall1 = [[URLOperationHelper alloc] initWithURL:#"urlString1" postParameters:reqDic urlDelegate:self];
URLOperationHelper *wsCall2 = [[URLOperationHelper alloc] initWithURL:#"urlString2" postParameters:reqDic urlDelegate:self];
[operationQueue addOperation:wsCall1];
[operationQueue addOperation:wsCall2];
}
// Did the URL Connection receive a response
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"Did receive response: %#", response);
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
// Handle status code here
webData = [[NSMutableData alloc]init];
}
// Did the URL Connection receive data
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(#"Did receive data: %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
assert(webData != nil);
[webData appendData:data];
}
// Did the connection fail with an error?
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"%#", error);
}
// Executes after a successful connection and data download
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Connection finished");
}
#end
And here is my URLOperationHelper.m
#implementation URLHelper
- (id)initWithURL:(NSString *)urlPath
postParameters:(NSMutableDictionary *)postParameters
urlParentDelegate:(id) pDelegate
{
if(self = [super init])
{
connectionURL = urlPath;
postParams = postParameters;
parentDelegate = pDelegate;
}
return self;
}
- (void)done
{
// Cancel the connection if present
if(urlConnection)
{
[urlConnection cancel];
urlConnection = nil;
}
// Alert
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
executing = NO;
finished = YES;
[self willChangeValueForKey:#"isFinished"];
[self willChangeValueForKey:#"isExecuting"];
}
- (void)cancel
{
// Possibly add an NSError Property
[self done];
}
- (void)start
{
// Make sure this operation starts on the main thread
if(![NSThread isMainThread])
{
[self performSelectorOnMainThread:#selector(start) withObject:nil waitUntilDone:NO];
return;
}
// Make sure that the operation executes
if(finished || [self isCancelled])
{
[self done];
return;
}
[self willChangeValueForKey:#"isExecuting"];
executing = YES;
[self main];
[self willChangeValueForKey:#"isExecuting"];
}
- (void)main
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postParams options:NSJSONWritingPrettyPrinted error:&error];
// Convert dictionary to JSON
NSString *requestJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"JSONRequest: %#", requestJSON);
// Declare Webservice URL, request, and return data
url = [[NSURL alloc] initWithString:connectionURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[requestJSON UTF8String] length:[requestJSON length]];
// Build the request
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:requestData];
// Connect to Webservice
// Responses are handled in the delegates below
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:parentDelegate startImmediately:YES];
}
- (BOOL)isConcurrent
{
return YES;
}
- (BOOL)isExecuting
{
return executing;
}
-(BOOL)isFinished
{
return finished;
}
#end
The problem that I'm having is the Start method for the URLOperation is never called. The OperationQueue is created and the Operations are called, but nothing happens after that, execution or thread wise.
Also, is this a correct line of thinking to provide UI feedback using NSOperationQueues like this? I.E. calling the NSURLDelegates from the Operation?
If you set setSuspended to YES before adding the Operations then your Operations will be queued into a suspended queue.. i suggest not to suspend the queue at
furthermore, your operation never ends anyway. You need to assign the operation itself as the delegate and implement all necessary delegate methods. In these methods you can forward the messages to your parentDelegate and decide when you are finished and call your done method when appropriate (i suggest connection:didFailWithError: and connectionDidFinishLoading:)
There is a good tutorial here: http://blog.9mmedia.com/?p=549
You are also not completely implementing key-value-coding compilant properties correct. Whenever you call willChangeValueForKey: you also need to call didChangeValueForKey afterwards:
- (void)start
{
...
[self willChangeValueForKey:#"isExecuting"];
executing = YES;
[self didChangeValueForKey:#"isExecuting"];
[self main];
}
and:
- (void)done
{
...
// Alert
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
executing = NO;
finished = YES;
[self didChangeValueForKey:#"isFinished"];
[self didChangeValueForKey:#"isExecuting"];
}
See this Q/A for KVC: when to use "willChangeValueForKey" and "didChangeValueForKey"?
I have an app which has an UITabelView which gets its content from a server side.In my app i read the content from the server each 30 seconds....for that I use a NSTimer.
This NSTimer gets initialized when I load the view which contains the UITableView and it is invalidated when I leave this view.
My problem is this:
if on the server side the content for the UITableView is updated with new item and the JSON received in the iphone app as a response to a request to the server contains that item....the UITableView that appears on the screen is still not updated.
Here is how I did it:
//the timer is started when this view is loaded and the method repeatServerRequest is called
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(playlistTimer == nil)
playlistTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target: self selector: #selector(repeatServerRequest) userInfo: nil repeats: YES];
}
//the method repeatServerRequest starts a new thread in the background which does a request //to server for downloadeding the content
- (void) repeatServerRequest{
[NSThread detachNewThreadSelector:#selector(backgroundThinking) toTarget:self withObject:nil];
}
- (void) backgroundThinking{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:#"a link to server"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[pool release];
}
///when the response from server comes in these methods are called:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self performSelectorOnMainThread:#selector(didFindAnswer:) withObject:request waitUntilDone:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"the value of error %#", error);
}
- (void) didFindAnswer:(ASIHTTPRequest *) request{
NSLog(#"update tabel");
SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];
NSArray *statuses = [parser objectWithString:responseString error:nil];
streams = [statuses valueForKey:#"_playLists"];
[parser release];
playList = [[NSMutableArray alloc] init];
idList = [[NSMutableArray alloc] init];
int ndx;
for (ndx = 0; ndx<streams.count; ndx++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:ndx];
[playList addObject:[stream valueForKey:#"name"]];
[idList addObject:[stream valueForKey:#"id_playlist"]];
}
NSLog(#"playList %#", playList);
oneview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
tableViewPlaylist =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableViewPlaylist.bounces=NO;
tableViewPlaylist.backgroundColor=[UIColor clearColor];
[tableViewPlaylist setDelegate:self];
[tableViewPlaylist setDataSource:self];
}
So when I update the content on server side, the JSON thet I get as a response on server side is updated but the UITAbelView is not, UNLESS I RUN AGAIN MY APP.Any idea why?
Two main problems:
You are creating a new UITableView every time the server updates. This is unnecessary.
Even if you wanted a new tableView, you are not adding it as a subview of the main view.
What you should do instead is don't create a new tableView at all. After you've updated the data model call [tableView reloadData] on the main thread. Your table will update and all is good. Assuming you have everything configured correctly and it sounds like you do if you see the expected data when you launch the app.
I am fetching XML data from server i want that when data is loading in progress i show a loading bar with default screen this is code in appDidFinishing
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigationController.view];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(55, 67, 100, 100)];
[self.activityIndicator startAnimating];
[self.window addSubview:self.activityIndicator];
[self loadXMlTwo];
[self loadXMlMain];
[self performSelectorInBackground:#selector(loadXMlOne) withObject:nil];
[self.activityIndicator removeFromSuperview];
[window makeKeyAndVisible];
return YES;
}
-(void)loadXMlMain{
NSURL*url= [[NSURL alloc]initWithString:#"http://46.137.28.14/app/ipadApplic/working.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
}
-(void)loadXMlOne{
NSURL*url1=[[NSURL alloc] initWithString:#"http://46.137.28.14/app/ipadApplic/rowone.xml"];
NSXMLParser *xmlParserRow = [[NSXMLParser alloc] initWithContentsOfURL:url1];
//Initialize the delegate.
RowOneParser *parser1 = [[RowOneParser alloc] initXMLParser];
//Set delegate
[xmlParserRow setDelegate:parser1];
BOOL success1 = [xmlParserRow parse];
if(success1)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
}
-(void)loadXMlTwo{
NSURL*urlRowTwo=[[NSURL alloc] initWithString:#"http://46.137.28.14/app/ipadApplic/rowtwo.xml"];
NSXMLParser *xmlParserRowTwo = [[NSXMLParser alloc] initWithContentsOfURL:urlRowTwo];
//Initialize the delegate.
RowTwoParser *parserRowTwo = [[RowTwoParser alloc] initXMLParser];
//Set delegate
[xmlParserRowTwo setDelegate:parserRowTwo];
BOOL successRow = [xmlParserRowTwo parse];
if(successRow)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
}
What you need to do is unblock your main thread while you are doing background fetch of xml data. Easiest way to do that is add your fetch operations in NSOperationQueue. Set up delegates for the class which fetches the xml and handle the response.
this is my first iPhone application and I'm using JSON framework to decode JSON sent from a server.
I insert the data in a NSMutableArray from an AppDelegate file.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
responseData = [[NSMutableData data] retain];
museums = [[NSMutableArray alloc] init];
viewController = [[RootViewController alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://my_json_link"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Connection failed: %#", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *data = (NSDictionary *)[json objectWithString:responseString error:&error];
[responseString release];
if (data == nil)
NSLog(#"JSON parsing failed: %#", [error localizedDescription]);
else {
for (NSDictionary *item in data) {
// Read the data
NSString *aName = [item objectForKey:#"name"];
NSString *aDescription = [item objectForKey:#"description"];
// Create a new museum object with the data from json
Museum *museum = [[Museum alloc] initWithName:aName description:aDescription];
// Add the museum object to the Array
[museums addObject:museum];
[museum release];
}
}
viewController.museums = museums;
}
The museums array is not empty inside connectionDidFinishLoading function, but I can't see it when I try to print it in RootViewController.
I tried to set the array in the line
viewController.museums = museums;
but I didn't understand what is wrong.
I can fix the problem only if I move these lines:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
from the first function to connectionDidFinishLoading function. But in this case doesn't work the other view when I click one record of the table.
Thanks for any help.
viewController = [[RootViewController alloc] init];
First, I need you to make sure that the viewController you create in didFinishLaunching... is actually the correct viewController. Have you actually wired up that instance to be what you think it is or do you two instance os RootViewController?
Second if you are actually setting museums on the right instance of RootViewController you need to make sure that your timing is correct. This means that are you setting museums BEFORE you trying to print it out in viewController
--Edit--
OK since we established that things are happening in the wrong order you should try and reload the table. The UITableView has a method called reloadData that will take care of this for you and you need to call this everytime you change the data source after the table has been created.
So in RootViewController add a method called reload which in turn calls reloadData on your UITableView and modify your code:
viewController.museums = museums;
[viewController reload];
You could add to your view controller, temporarily just for debugging:
-(void) setMuseums:(NSMutableArray*)m {
self->_museums = [m retain];
}
and then add a breakpoint in there. Make sure it's getting hit, or maybe there's something later coming along and setting it to nil.
The Museums property is declared as #property (nonatomic, retain) NSMutableArray *museums; right?
Try to put a NSLog inside the for loop and check if it is executed.
try using
viewController.museums = [museums retain];
I am creating application that simply reads data from an XML file and displays it in a table view.
I created a "refresh" button when clicked i want it to redownload the xml file and display it again however it seems to crash my application if there is a XML file already downloaded.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ipb = [[IPB alloc] init];
sectionTitle=[[NSMutableArray alloc]init];
currentURL=#"http://localhost:8888/xml/Sinnergy.xml";
[self reloadTableView];
[window makeKeyAndVisible];
return YES;
}
-(void)reloadTableView
{
pathURL = [NSURL URLWithString:currentURL];
parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL];
[parser setDelegate:self];
[parser parse];
[mainTableView reloadData];
}
You are leaking the parser, and if its an instance variable it might cause issues. You should go
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL];
[parser setDelegate:self];
[parser parse];
[parser release];
Also you are asking the parser to start parsing, but at that point of time you shouldn't be reloading the table, it should be in your
- (void)parserDidEndDocument:(NSXMLParser *)parser
delegate method. Try that and if it still crashes post the crash report