Doing some downloading without blocking you app - iphone

I'm working on my first app that's doing a few different web connections at once.
My first screen is my Menu.
And at the bottom of viewDidLoad of MenuViewController i call a method that gets and parses a .xml file that is located on my webserver.
Also at the bottom of viewDidLoad i do
FootballScores = [[FootBallScores alloc] init];
and FootballScores makes a connection to a html page which it loads into a string and then parses out data.
Now since both of these are getting called at the bottom of viewDidLoad of the class thats is responsible for the main menu(first screen in the app) it means the app is kinda slow to load.
What is the right way to do the above? Should i remove the 2 pieces of code from my viewDidLoad and replace with maybe
dataGetterOne = [NSTimer scheduledTimerWithTimeInterval:1.000 target:self
selector:#selector(xmlParser) userInfo:nil repeats:NO];
dataGetterTwo = [NSTimer scheduledTimerWithTimeInterval:2.000 target:self
selector:#selector(htmlParser) userInfo:nil repeats:NO];
This would mean that the methods get called later on and the viewDidLoad gets to finish before the i try get the data from the web servers.
Making 2 connections to we bservers a second apart to quick? Can the iphone handle having 2 connections open at once?
I'm really unsure of anything bad/dangerous I am doing in regards to connections.
Many Thanks
-Code

Try using ASIHTTPRequest. It's easy to use and lets you make asynchronous requests that don't block your app with few lines of code.
Hope this helps,
ender

Also, I wrote a brief tutorial on writing your own XML/JSON data-parsing app. I hope it may be helpful in thinking about how to structure such an app.

Related

multitasking in ios 4 later for notifications

i want to run one of my services in the background even my app is in foreground or background states,if i get any data from that service i want to show that one as a notification to the user. how can i proceed .
as my reserach gives me an idea to follow multi taking in iphone .
or shall i use any timer so that the method calls for particular time interval for every time
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(notification) userInfo:nil repeats:YES];
[timer fire];
-(void)notification
{
//shall i write all of my services here
}
or shall i create any new threads like
[self performSelectorInBackground:#selector(notification) withObject:Nil];
in AppDelegate file .
or shall i need to follow any other thing plz guide me thank you.
Generally, you should consider using push notifications sent by the server to show notifications to the user when something changes. This is preferable to running in the background since it is better for system performance & battery life.

NSThread causes UIWebView Crash

i would like to explain my issue from the beginning. I am creating a app with UIWebView. Also I am capturing the screen at the moment user using the app and create a video using that image array.
I am using NSTimer to get screen shots of the screen. I worked well but i got one issue. That was when user start scrolling the web page or when user hold his touch on the screen NSTimer got pause and after he releases the touch NSTimer continue.
Here is my NSTimer used to get Screen shots of the screen.
assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/24.0f
target:self
selector:#selector (writeSample)
userInfo:nil
repeats:YES];
I googled this issue and most of the people got same issue as i got. So they found a answer for that and that is using NSRunLoop. They said it stops pausing the NSTimer.
So I used NSRunLoop and add NSTimer into the NSRunLoop and here is the code after I did that.
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/24.0f
target:self
selector:#selector (writeSample)
userInfo:nil
repeats:YES];
[runLoop addTimer:assetWriterTimer forMode:NSRunLoopCommonModes];
[runLoop run];
This code worked well and it didnt stops pausing NSTimer. But after i used NSRunLoop My app got really really slow. Also it took lots of time to load the webpage into the WebView and scrolling also very slow.
So I started to search in google again and i found the issue. That was all those things are doing in main thread and iphone cant process every thing in same time. So people suggest to use NSThread to over come the problem. So what i did was create a NSThread and attach the process to the custom thread.
Here is my code after i apply NSThread to it.
[NSThread detachNewThreadSelector:#selector(launchTimer) toTarget:self withObject:nil];
-(void)launchTimer{
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/24.0f
target:self
selector:#selector (writeSample)
userInfo:nil
repeats:YES];
[runLoop addTimer:assetWriterTimer forMode:NSRunLoopCommonModes];
[runLoop run];
}
-(void) writeSample{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//did some coding here to take screen shot
[pool release];
}
This solves the slowing the app issue forever and my app is running again very smoothly but it brings up serious problem now. That is happening because of using threads i guess. My custom thread and main thread getting conflict now and causes the app to crash. I searched the google for the reason. I found some questions related to this kind of problem and some people answered those and said Any UI changes need to be done in Main thread. Also UIWebView WebThread must run in main thread.
Those are some error reports i am getting,
Those errors are like some thread conflicting. But I have no idea how to solve this problem. I used my code to some native app that is not having any Web Views or Maps. So it works well with out crashing. But when i use this to app that is having WebView or Map its crashing some times and give me this errors.
I am in soo much trouble in here and If any one can give any idea or sample code to handle threads , handle thread priorities , or using NSTimer and NSRunLoop with out using NSThreads , or Overcome the app slowness without using NSThread , Any answer would be very very very helpful and I am looking forward to hear from you soon.
Thanks.
My guess is the code that you wrote to capture screen shots is causing the problem. A simple test would be to comment out all the code in the writeSample method and see if the app still crashes. I say this because you are right that all UI related tasks should work in the Main Thread at all times.
I would suggest to use a separate thread for timer so that your timer isn't paused and then from the writeSample method force the screen capture code to run on the main thread.
Hope this helps
First off:
As soon as you are using timers, you are working with run loops — as you can easily see from the reference:
Timers work in conjunction with run loops.
(That’s the first sentence of the second paragraph)
With that out of the way, to your syptoms:
Blocking a thread by capturing images, causes it to stutter.
This shouldn’t surprise you!
Moving the work off of that thread, releases the strain on the first one.
Shocking, I know!
Well, given that you didn’t show us any of the code that causes your synchronization issues, (read: crashes) this sarcastic answer is all the help I can offer you.
That and the tip to read and internalize the document I linked to above…
Edit
I just forgot to mention, this:
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:blablabla];
[runLoop addTimer:assetWriterTimer forMode:NSRunLoopCommonModes];
[runLoop run];
doesn’t make much sense because…
-scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
(Which is explained in the magnificent documentation, as well.)
In essence: when you’re on your own private thread, the default mode is sufficient. You have to make sure that the run loop is actually running, though.
This is actually accomplished by the last line of that code snippet, but it effectively causes your method to not return until the timer is cancelled…

Adding delay in sending SMS in iOS

I am developing an application that sends SMS to the selected contacts at regular intervals for a specified number of times. I have the SMS-sending functionality working, but I need to add a delay between sending SMS after each round.
For example, if I want a message M to be sent to S and D in my contacts, I need the app to wait for N seconds before sending the same message again to S and D.
How can I get this working? How do I add the delay function between sending the messages to the contacts?
Thanks in advance!
simply said, you can't.
It's not allowed to send messages yourself, you can only pop-up the sms-displayer and the user himself has to tap 'send'.
So if doing it programmatically in an app, this is not possible.
It might be possible if you're sending text messages using a server or something..
Would it be possible to set a NSTimer to go of for a certain number of times at certain intervals and have your app run in the background after it has been closed?
Make a timer:
int N = 3;
_myTimer = [NSTimer scheduledTimerWithTimeInterval:N target:self selector:#selector(myMethod) userInfo:myContact repeats:YES];
-(void)myMethod:(NSTimer*)timer {
// Now I can access all the properties and methods of myObject
[[timer userInfo] myObjectMethod];
}
Running in the background I am not as familiar with however if you start looking for beginBackgroundTaskWithExpirationHandler: Im sure you will find the answers that you need.
Hope this helps, Good Luck!
Well, if you have SMS sending functionality figured out then all you need is:
[self performSelector:#selector(sendSMS) withObject:nil afterDelay:5.0];
This is if your SMS data is somewhere in the model. If you need to send some data to your sendSMS method then use:
[self performSelector:#selector(sendSMS:) withObject:objectData afterDelay:5.0];

Can I have set the time for Launch image(app Start up)..? I mean,for 5 seconds

I have added launch image in my application in info.plist. So,may I know is it possible to set the time for launching application for 5 seconds.Thanks
You can definitely do that, but you shouldn't. The user will be staring at an useless splash screen just for nothing.
In your app delegate's didFinishLaunching method, do:
sleep(5);
In your App delegate class, in the didFinishLaunchingWithOptions method.
[NSThread sleepForTimeInterval:5.0];
This will make your app waiting for 5 sec.
Yes. You can set time for launch image. Below is code. It may help you.
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(onLaunchExpire) userInfo:nil repeats:NO];
Use this and let me know for more details..

iPhone web app auto refresh

I'm making an iPhone app where all the data is online so its really a web app and the app listed on the app store is the portal to get onto it.
I would like to know how can I make it that every time the app is opened the page is refreshed and it take the user back to the homepage, and auto refresh say after 10mins.
try:
[NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(refreshWebView) userInfo:nil repeats:YES];
-(void)refreshWebView{
//refresh your webView here..
}
note: untested