how to show loading image indicator inside uitable? - iphone

i'm new using Trhee20 in Xcode and i'm building an APP that needs to send a post request to a page to register a new user using the following code:
TTURLRequest *request = [TTURLRequest requestWithURL:page delegate:self];
request.httpMethod = #"POST";
request.cachePolicy = TTURLRequestCachePolicyNoCache;
request.response = [[[TTURLJSONResponse alloc] init] autorelease];
[request.parameters addObject:nombre forKey:#"username"];
[request.parameters addObject:email forKey:#"email"];
[request.parameters addObject:pass1 forKey:#"pass"];
[request.parameters addObject:pass2 forKey:#"cpass"];
I know that i need to use:[request sendSynchronously];to send the data but i don't know how to save the data that i'll retrieve from the server into a variable because this method only gives me a true or false.
The second thing i would like to know is how to set an image loading activity indicator into a uitable to block this while the request is being send to the server and quit that image once it has finished.
Thanks a lot for your help.

Answer #1: You set up a TTURLRequestDelegate and define the methods of what you want to happen for each callback.
Answer #2: This isn't Three20 specific, you should just be able to add the loading image when you make the request, and then remove it once you get one of the above mentioned delegate methods called.
On a side note: make sure that if you're actually doing something synchronously that you do not do it on the UI thread as it will make your app hang.

Related

How to keep the ASIHTTPRequestDelegate even when if the user popped from that view controller?

I have an application in which I need to have a settings page,which has some credentials of the user then he can edit that.its a table view loading from an array taken from the httprequest.by clicking on each of this it will have the option to go to another view and update that value and come back. I have done the update call to the server on that update view like this..
dispatch_async(backgroundQueue_, ^{
[self performSelectorInBackground:#selector(load) withObject:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
[self showHUD];
});
because in the mainqueue i am doing the popping back operation.so i need that update service to be called in the background.But the problem is when i coming back i am calling another service in the settings viewcontroller.to load the updated value.some times the delegates of the request is getting crashed.I am calling the service like this.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:uidstr forKey:#"userId"];
request.userInfo=[NSDictionary dictionaryWithObject:#"update" forKey:#"type"];
[request setPostValue:self.string forKey:#"age"];
[request setDelegate:self];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[self showHUD1];
[request startAsynchronous];
Can anybody point me how i can do this with out crashing my app.I think the problem is the delegate getting nil.
Decouple your network requests from your view controller code. That way if the views are unloaded, the network delegate will still exist.
For example: Make a NetworkRequest singleton class that does all the communication with the network, and then you could use a mechanism like NSNotifications, or an #protocol interface in the singleton class that view controllers could become delegates of, to pass results and status changes as needed.
For a good tutorial on singletons in Objective-C, see: http://www.galloway.me.uk/tutorials/singleton-classes/
Better drop usage of ASIHTTP since this framework is no longer supported by the developers. You won't get support for possible changes of future iOS versions.

NSURLRequest delegates not getting called on Device Works fine on simulator.

NSURLRequest delegate methods are not getting called when i run the application on the device. It works perfectly on the simulator though. Also its not the case of my view loading before the request is fulfilled because i enable to view to be loaded only once the connection has received the data.
My code requesting url is here. Any help greatly appreciated.
- (void)viewWillAppear:(BOOL)animated
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://-dev01x/content"]; cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
[request setHTTPMethod:#"GET"];
_getData = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(#"HELLO %#",_getData);
}
Your code as provided here makes no sense:
_getData = [[NSURLConnection alloc] initWithRequest:request delegate:self];
That line returns a pointer to a NSURLConnection object, your variable name is misleading. If you want to block at this point (and does that even work) then it would appear you need to use sendSynchronousRequest at that point.
I do something similar to what you want to do, but in a more traditional way. In viewDidLoad or even in the initWithFrame, I will start up an asynchronous connection, set a flag, and set the view backgroundColor to black or white (I use a spinner too normally). When I get viewWillAppear, if the connection has completed, all is well and I set the various UI elements. If not, then don't do anything, and later, when the connection completes, pull down the spinner and update the UI.
The only way I can think of to block the main thread at the point you are trying to would be to use that synchronous request (which IMHO is a really bad way to deal with this).
The beauty of doing things in the background is that if its taking too long, the user can tap the back button or go somewhere else in the app [in which case you cancel the connection and tear everything down.]
Check url u provide to NSMutableURLRequest when checked in browser gives nothing
NSMutableURLRequest needs valid url

Iphone: Need release using ASIFormDataRequest

i have one simple question, if i'm using ASIFormDataRequest when i need to release the request object?
NSURL *url = [NSURL URLWithString:#"url"];
ASIFormDataRequest *requestForm = [ASIFormDataRequest requestWithURL:url];
[requestForm addPostValue:[[NSUserDefaults standardUserDefaults] stringForKey:#"user"] forKey:#"user"];
[requestForm setRequestMethod:#"POST"];
[requestForm setDelegate:self];
[requestForm startAsynchronous];
Thanks
You need to remember that you are always responsible for releasing an object if the method from which you receive it contains new, copy, or init.
In this case, you don't need to release it. The ASIHTTPRequest class autoreleases it for you.
A quick search in the implementation file shows that it'll be autoreleased.
+ (id)requestWithURL:(NSURL *)newURL
{
return [[[self alloc] initWithURL:newURL] autorelease];
}
Your request is autoreleased in your code, so you don't need to release it (as others have said).
However, you are starting an asynchronous request - it'll complete/fail sometime in the future, and if it is associated with other objects which will get freed when the view exits you're potentially leaving yourself open to a crash. So I'd suggest you would want to make requestForm a property of your class (so when you assign the request to self.requestForm it will get retained for you), and explicitly release & nil it when the request completes.
If it's a very simple app with just one view you may get away without that though.

Create a new delegate class for each asynchronous image download?

First, I'm using an NSURLConnection to download JSON data from twitter. Then, I'm using a second NSURLConnection to download corresponding user avatar images (the urls to the images are parsed from the first data download).
For the first data connection, I have my TwitterViewController set as the NSURLConnection delegate. I've created a separate class (ImageDownloadDelegate) to function as the delegate for a second NSURLConnection that handles the images. After the tweets are finished downloading, I'm using this code to get the avatars:
for(int j=0; j<[self.tweets count]; j++){
ImageDownloadDelegate *imgDelegate = [[ImageDownloadDelegate alloc] init];
Tweet *myTweet = [self.tweets objectAtIndex:j];
imgDelegate.tweet = myTweet;
imgDelegate.table = timeline; //to reload the data
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:myTweet.imageURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
imgConnection = [[NSURLConnection alloc] initWithRequest:request delegate:imgDelegate];
[imgDelegate release];
}
So basically a new instance of the delegate class is created for each image that needs to be downloaded. Is this the best way to go about this? If I were to create only one instance of the delegate class there's no way to figure out which image is associated with which tweet, correct? Nor would I be able to figure out the exact order in which the images are being downloaded.
The algorithm works fine... I'm just wondering if I'm going about it the most efficient way .
Put image downloads into something like an NSOperationQueue.
When the image download is done, save it to the caches directory, then send out a notification containing the original image URL, and the filename the image is now located at.
Anything that wants the image can listen for the notification. If nothing cares anymore (say cells that have scrolled off the screen) then they will have unsubscribed from notifications, so the image will just sit there until the system cleans out your cache directory...
It's also trivial with this system to check and see if an image already exists on disk before you download it, just keep somewhere a mapping of URL's to filenames.
You might perhaps use the queuing feature of ASIHTTPRequest instead. You can create an ASINetworkQueue to handle a ordered queue of requests, and each request can perform behaviors on completion, so that you can track requests with responses.
Requests are based on the NSOperation class, and the queue on NSOperationQueue, so this framework does a lot of the coding work for you.

TTURLResponse is nil

I am trying to implement a simple TTURLRequest in my app. I'm pretty new to the Three20 framework. I mainly want TTURLRequest and TTImageView for the awesome caching stuff.
I have the following code in my delegate:
- (void)requestDidFinishLoad:(TTURLRequest*)request {
TTURLDataResponse *response = request.response;
// Stuff to process response.data
}
response is always nil. I can't figure out what I'm doing wrong. I looked in the Application Support directory and it's creating the cache file with the proper data, so I know it's getting a response. What am I doing wrong?
Debugged your code - you actually have to create a response object and add it to the request before sending your request:
NSString *url = #"http://twitter.com/statuses/user_timeline/samsoffes.json?count=1";
TTURLRequest *theRequest = [[TTURLRequest alloc] initWithURL:url delegate:self];
theRequest.response = [[[TTURLDataResponse alloc] init] autorelease];
[theRequest send];
One would think the request would be in charge of creating the response, but the Three20 stuff doesn't.
Mark Allen from revetkn.com posted a datasource implementation for TTURLRequest.
He also posted an example project where he assumes you have Three20 installed at ../ (relative to your project). It's here: http://revetkn.com/?p=72