applicationWillEnterForeground - sometimes my app is very lazy - iphone

My app contains many graphics and sounds. it's actually running very well - only if I go back in my app after a long time my app stops responding for about 2-7sec
I release my Images and sound (and all the rest) as soon as possible to keep the apps memory consumption as low as possible
how can I fix it?
thanks in advance

Your best bet would be to spawn a background thread to load all the resources back into your app. At least that way the rest of the app should be responsive (save the elements you're loading).
A good place to start learning about using threads with iOS and Objective-C would be the Concurrency Programming Guide.

Related

Can I lazy-load my libraries so they don't delay my app startup?

My app takes a long period of time during the startup, while the splash screen is shown. I assume that It is so due to the size of what the iOS has to load, including libraries. My question is, can I load those in the moment the user actually wants to use it, so it makes the startup time shorter?
Are there other ways to do it shorter?
Thanks a lot.
All 3rd party libraries are statically linked to your app. In theory you can lazy load only Apple's own weakly bound libraries. I am not aware how you can control this process on iOS. It's certainly possible on Mac.
sure you can as long as you don't need them directly.
LazyLoad is not limited to what you apply it too, for example if you have a huge Opengl scene you can chose not to laod its textures until the user actually click on the button you need.
The downside of this is that the waiting time to open whatever requires a lazyload will be moved further down the app (when the user wanted to play he will have a longer loading time).
What you could try is to launch Thread that handles the loading at startup and from the thread you do a setBooleanLibXFinishedLoading this way your app will only have to wait for all the booelan to be set to proceed.
This should reduce the apparent waiting time for the user while optimizing the time actually spend loading.
hope this helps
Jason

iPhone App Gets Killed Before It Finishes Loading

Okay, so Apple apparently has this thing where if the app takes too long to load, iOS will automatically quit the app or something. So when I'm building my iPhone app, I have quite a few high resolution images, which take a while to load, and they never finish before the app is automatically killed. Can anyone help with this?
Thanks!
From the iPhone Application Programming Guide: "Initialization time is not the time to start loading large data structures that you do not intend to use right away ... If your application requires additional time at launch to load data from the network or do other tasks that might be slow, you should get your interface up and running first and then launch the slow task on a background thread."
As Alex said, I recommend you to load resources on a background thread. However, be careful to use UIKit on a background thread. For the most part, UIKit classes should be used only from an application’s main thread. You should use thread-safe API.
For example, UIImage +imageWithContentsOfFile: is thread-safe. UIImage +imageNamed: is not thread-safe.
(From Developer Forums thread)
which take a while to load ...
Not just app start up... If you want a responsive app, that app shouldn't do anything that takes more than a few dozens of milliseconds, synchronously, on the main UI thread or run loop.

Continuous Timeline in Iphone App?

Background: Workout App is designed to have a continuous timeline that I can then input what exercise and reps I'm doing in real-time, then graph the results in real-time.
Issue: When the iphone sleeps for too long, we lose the continuity of the Timeline and it restarts. We haven't figured out how to keep the timeline updating while the App is sleeping. I've asked him to do a call to the Iphone internal clock and do the math on where the viewer should be and move them to that point on the timeline. However, he can't figure out how to do this.
Solutions: Have you folks heard of anyways to do this call to the internal clock? Perhaps a better question, can you think of another way around the problem in general?
You could try CACurrentMediaTime which returns mach_absolute_time in seconds. This should be accurate across sleep.
There is no certain way that can assure you app will be running in the background for ever. (If this is what you need)
Even Steve said is Multitasking, iPhone resources are very limited and this means that your app will might be killed while it is in the background without any kind of notification.
(Except by the notification you get when entering the background)
The best thing you can do is low your resource usage. (release as many objects as possible)
Because when the system needs memory it will start killing apps in the background starting by the app that uses more resources.
If you are implementing streaming protocols then is different.
Hope this helps

Does iOS 4 make “Real Multitasking” available to 3rd party developers?

Ever since the first beta came out I’ve been trying to find out if “real” multitasking is possible — i.e. can you put a program in the background and have it hang on to a network connection indefinitely?
I’m thinking about IM or IRC apps, for example. I’ve compiled an app myself on iOS 4, and without changing a thing it appeared to stay running in the background, but for all I know it was just suspended to memory.
The docs say the best you can do is request up to 10 minutes, but in the developer presentation they showed off Skype sitting in the background and then notifying the user that a call was coming in. Does anyone know for sure how this all works?
It appears the answer is no. The API for Skype is a very special case, called the "voip" mode, and requires special behavior, such as marking the socket in use for VoIP.
You can receive alarm notifications in the background (such as time passed). The amount of time you are in the background running state is severely limited by the OS.
Android's background model is complete and in many ways much nicer.
Apple has a guide named "Supporting Multitasking In Your Applications" which you should be able to locate.
Apple's iOS 4 developer docs outline this all very clearly.
When your app is closed or switched away from, it is almost immediately "suspended", meaning the OS freezes the app's state. When the user switches back to your app, your code keeps running just where it kept off. You don't need to add any code to your app to do this, just compile it against OS 4.
The above is true in most cases. There are two reasons the "suspended" model may not apply:
1) If the device starts to run low on memory, the OS will start terminating suspended apps that haven't been switched to in a while, without warning. This is why it's in your best interest for your app to remember it's state as well, so if your app is terminated, then re-opened, the user doesn't really notice because it still returns to right where they left off.
2) Your app uses one of the "background" APIs. These are for audio playback, VoIP services, or location services. In this case, your app is allowed to continue running in the background but only has access to those APIs. Additionally, your app can designate certain long-running tasks as "background tasks" that need to be completed before the app is suspended or terminated, like uploading pictures to Flickr or rendering a video, etc.
The "background task" method doesn't cover pinging servers indefinitely, as there is a time limit for the task, after which it will be forcibly halted. Apps that need that sort of functionality are expected to implement push notifications, just as before.
That should clear this up. All in all I think it's a pretty elegant solution to multitasking on a mobile device.
iOS 4 applications can either be running or suspended. The operating system will try to keep as many requested applications as possible in memory, while all other applications are suspended.
Applications that run in the background can access features such as navigation, audio, and VOIP (but NOT instant messaging). So it looks like you might be out of luck.
-- PC World Multitasking on Apples iPhone 4
It is possible for apps to request background time. Read the docs. I would say it iOS is "controlled multitasking".
You can create a long running background task, I believe these can include networking features. Just have to set the background task flag on the work block.
https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
the OS can limit exactly how much time you get though... it will tell you when your time is up giving you a chance to cleanup nicely.
iOS 4 has "real" multitasking to some extend. There are two things to consider:
The UI event loop is single tasking. So only the front app executes on the UI event loop. Not a big deal: detach your main code form the UI event loop (maybe you need to redesign your app).
The OS "may" terminate your app if some criteria are met (e.g. low memory).
Currently one of these criteria is that execution time is limited to 10 minutes (real time not cpu time). However I expect this to change and see better criteria for background app termination (I hope to).
Apart from this you can have timers (event loops) in background.
There is no real multitasking in iOS 4.2 even. because apps will only be allowed to finish the task related to states..for small interval of time and then it will be in suspended state.. If you will set background task for long interval of time then... it will behave unexpectedly like no method will be called when you will try to run the app from anywhere..
You may be interested in this blog post that outlines how "multitasking" works in systems such as iPhone OS 4 and Android.
in fact u can do this, although it's not allowed by Apple. u gotta set up a toolchain in ur mac and use some unofficial SDK...
check http://code.google.com/p/iphone-backgrounder/ for more information
You should use the Push Notifications framework for the feature set you are creating!

iPhone image upload in background

I'm writing an app with an image upload feature. Right now I'm using NSURL POST like: 125306.
When the app is closed as far as I can tell all of the uploads abort and the threads die. Is there
1) a way to make these upload threads persist when the app is no longer in the foreground?
2) an iPhone OS service that will accept requests to queue a job and run it in a mode managed by the OS?
EDIT: This is an old answer from 2009. Current iOS (2016) has background URL tasks. See NSURLSessionUploadTask.
Original answer follows:
You aren't allowed to run in the background on an iPhone (as per Apple's Guidelines). Your app will be rejected from the AppStore if you do.
As has been failry well documented, no background processes are allowed using the SDK.
As noted, you cannot have a background process - a good compromise would be to resize the photo down to a size that could be transferred in a reasonable amount of time (user configurable would be best), and display an activity indicator of some kind so the user would know the upload was in progress.
An even better solution would be the resize, along with a progress indicator giving the percentage currently uploaded - but that involves some custom low-level HTTP code.
After app suspending you only have ~10 mins to do the job.
You should make a background process thread when app is suspended.
And you have no garanties that os will keep app alive. It depends on process complexity.