willEnterForeground randomly restarts app - ios

I thought my app was almost ready to ship, but this one thing is making me crazy!
In my didEnterBackground handler, I save some images to cache and dump everything I can possibly think of and the OS seems to randomly dump my app and restart it. It doesn't seem to be very consistent. Sometimes it comes back fine and sometimes I get a restart. According to Allocations, my memory usage is down to around 1 or 2 mb in my didEnterBackground.
And by dumping everything, I mean I remove subviews, remove objects from arrays and set a ton of stuff to nil...which means in my willEnterForeground I have to recreate a bunch of stuff.
I really don't know what else to do.
Any help here would be greatly appreciated. Thanks!
BTW, this is in iOS 5 and using ARC.

At the background state the app can always get killed by the watchdog. There is no guarantee that the app is always working in the background.
To learn more about Multitasking watch the Session 320 from WWDC 2011 there is a plenty of information about multitasking.

Related

Delay in CoreData to PersistentCoudKitContainer synchronization

I am having an issue with my CoreData to iCloud synchronization with NSPersistentCloudKitContainer.
The synchronization works, but when i do a fresh install of the app there is an annoying delay of several seconds between app launch and the end of synchronization. I need to decide at launch whether to create new data entities or use the "old" data from iCloud.
I could live with the delay and wait for the sync to finish if there was a way to
a) determine at launch that there is data in iCloud to be synchronized and
b) get a notification when synchronization is finally done
Does anyone know of a solution to achieve this? Setting
NSPersistentStoreRemoteChangeNotificationPostOptionKey does not help much, as it is fired several times during sync and does not give any status information.
At the risk of providing you the most unhelpful answer possible, I don't think it is possible and, even if it is, I think you are battling against the philosophy of NSPersistentCloudKitContainer.
With NSPersistentCloudKitContainer, you should assume that synchronisation happens at irregular, erratic intervals or not at all. It is supposed to operate seamlessly, in the background, with nothing for you to worry about. You shouldn't try to speculate in your code when it happens or if it happens.
It is very similar to taking a photo on your iPhone and then it taking several seconds for that photo to appear on your iMac. The iCloud sync decides if and when that sync will take place.
I know this is not helpful, but I thought you should be aware of this perspective.

What could slow down a iOS App over time besides Memory Leaks?

I have an app that adds a lot of UIClasses to many different Views. Views get removed and get added again. But then after some time working with the app, the app starts to slow down. Reloading everything, including the Viewcontroller, doesn't help.
The problem is, that i don't know what could slow down the app. The Memory Usage of the app doesn't go up and the CPU usage doesn't go up aswell. It just seems that the App does everything slower. Views take longer to get added to the view and so on.
Is there anything that i could check what could cause the app to slow down over time? I know that this question is very broad, but maybe someone of you can show me the direction to look at. I`m out of ideas.
It's hard to answer a question like this in the abstract.
I suggest using the Instruments tool and running a time profile. That lets you see where your app is spending most of it's time. You should be able to run the app for a period of time and see the culprit start to take more and more time as you run the app.

iOS Action Extension slow loading time

So, basically I'm trying to decrease loading times for an Action Extension I'm writing for IOS.
According to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionCreation.html#//apple_ref/doc/uid/TP40014214-CH5-SW7
I should aim to get the loading time below 1 second (if it takes too long it might be shut down by the system prematurely)
Right now it loads in around 4 seconds on an iPad (a bit faster in the simulator) - so far iOS haven't shut down my extension, but this will be destructive for the user experience.
As far as I'm aware I don't have access to a AppDelegate.swift file when working with extensions, thus I'm having a hard time figuring out what is causing the slow loading times.
Does anyone have any idea where to look or maybe some experience with this?
Thanks!
The reason for the slow loading times was that I launched the app extension in debug mode. Running the app without the debugger it was significantly faster.
Did not consider this at all, but now it works like a charm :)

Restarting Main Thread in IOS

I have an application/game which I am adding a restart button to. The easiest thing for me to do this is if there was a bit of code which would release all objects created by the app running and restart the main thread (there are many of them including timers which may be running when the reset button is being pressed).
Is there any such code?
I understand that it may be possible to run the app in a secondary thread and refresh that but I dont have any threading experience.
can anyone help?
The only thing i know is you can force kill your app but be prepared as it will be rejected by apple and even if you get success in killing app then you cannot restart it.
It is not possible to restart the main thread or anything similar. You're probably thinking of the wrong design. Think of it from an object oriented perspective: what you need to do is to restore the state of some objects that keep track of the state of your application.
So say you have a Game class that has some properties like:
level
points
what you'd have to do is restore those to 0 (or whatever the initial value is).
Hope you get the idea ;)
there is not easy way to do that, if you want to restart the game or level you will have to recreate the level, by re assign the level variables redrawing the correct components etc..., you will not be able to restart the game or level by restarting a thread,

What actions are required when your app get an applicationWillResignActive call?

This question io3->ios4 upgrade said to support applicationWillResignActive. While implementing this call, I also implemented applicationDidEnterBackground and applicationWillEnterForeground. However, I found that my app would crash. After some debugging on the simulator I determined that I needed to reinitialize a key data structure in applicationWillEnterForeground. So my question is how would I have known that from reading the documentation? (In fact, I may be doing the wrong thing and just so happened to get it working again.) Is there an exact description of what to do when these methods are called?
Thanks.
The only things you should do when supporting multitasking, is saving the state of your app when it enters the background, and reload it when it becomes active. (If you generate a new template in Xcode, you'll see this.)
Saving state means writing any user preferences or data to disk. Reloading the state involves reading saved preferences and data, recreating any in memory data structures that might need it (like in your example you gave).
In most circumstances, there's little else you need to do. The only thing that would crash your app that is unique to multitasking would be trying to run code in the background for longer than the allotted amount of time, (which is 10 minutes.) Otherwise, it sounds like you have got other problems with your code.

Resources