Tearing my hair out on this, any thoughts or suggestions would be greatly appreciated.
I have an object that calls performSelectorOnMainThread:withObject:waitUntilDone: on itself from a child thread. This works fine 95% of the time. Every once in a while since iOS betas and now into release, a device will refuse to execute the performSelectorOnMainThread:withObject:waitUntilDone: calls... No error message, it doesn't crash, I can't make a device go into a state where it "fails" but once it's there it continues to fail until I either delete and re-install the app, or force it to quit, then adjust it's location services allowance, then launch it again, then re-adjust it's location services again back to normal... restarting the device does not fix it. Re-installing without deleting first doesn't fix it. It's very strange.... I know it works most of the time because most devices have no problems, however some devices fail somewhat regularly (every 3 or 4 days maybe). I know it's specifically that performSelectorOnMainThread:withObject:waitUntilDone: is not calling what it should because I've got a failing device now, and I've put an NSLog into the method that should be called. It function fine, but on the failing device, when performSelectorOnMainThread:withObject:waitUntilDone: is used to call that method the NSLog shows it's not being run...
This started happening with iOS 5 betas and again, happens on release. It happens most often on 2 of my devices, but on none of the other 10 devices I have personally tested on. I assumed it was just my device from some hiccup in the beta, but it happens on my brand new 4S which never touched beta, as well as on one user's iPad 2 (not on my iPad 2).
I really don't know where to look. I tell it to execute and it usually does on almost every device, but the same line gets no response and no errors etc. on some...
performSelectorOnMainThread:withObject:waitUntilDone can be wonky sometimes. Have you thought about trying to use lib dispatch?
You can create a block and post it on the main thread like this:
dispatch_async(dispatch_get_main_queue(), ^{
<do work here>
});
This would have the same save affect as using performSelectorOnMainThread:withObject:waitUntilDone
http://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1
A Thread's RunLoop has several different modes.
It is possible that during the time you make your call the main thread is running in a mode different than the default one (which is the one targeted by performSelectorOnMainThread:withObject:WaitUntilDone:.
Consider using performSelectorOnMainThread:withObject:waitUntilDone:modes:
See Apple's documentation.
Also - GCD (libdispatch) is awesome, but it won't protect you from cooking yourself a deadlock:
Consider the case where your method is running on the main thread and you're calling:
dispatch_sync(dispatch_get_main_queue(), ^{
<do work here>
});
Related
I have encountered a problem in my app that didFinishLaunchingWithOptions was taking too long to finish, what could possibly result in the app being terminated.
I have also been getting crash reports from itunes connect that seem to indicate that the app takes too long to launch.
This might be caused by the fact that I was loading some textures synchronously during didFinishLaunchingWithOptions, a process that takes more than a second.
In an attempt to solve it, I call the method that loads the data after delay, in the following way:
[self performSelector:#selector(loadTextures) withObject:nil afterDelay:0.5f];
The way I understand - though I might be wrong - the scheduled task will not be carried out on a different thread, and therefore will only be processed after didFinishLaunchingWithOptions is done.
My question is:
Are there any best practice solutions for such a problem? Is the solution I suggested works as I intended?
Also, is apple's watchdog known to check apps for responsiveness even after the launch phase in cases such as this?
We had a similar problem in our ebook reader application where we copy ebooks (of several MBs) from bundle to the documents directory.Scheduling selector for copying the books has been the best solution so far.The same solution works fine in your case as well.I am not sure about apple's watch dog but our application is live on the app store and running since more then one year with this solution incorporated.
Hee
Does anybody know how to recognize a multitask-kill.
When the user puts the application in background state and then kills the app through the menu in iOS 4.2 my application shuts down.
Before there used to be an function called:
- (void)applicationWillTerminate:(UIApplication *)application
This method is not called anymore in iOS 4 and higher.
Is there a way to recognize it?
Thanks already.
Actually that's not entirely true. It is called on iOS4 devices that do not support multi-tasking and the documentation says that it can be called on other handsets (though I've never seen it myself).
But to answer your question, no, you can't recognise when a user kills your app. If you have state that you want to save you need to do this when your app goes into the background and not when the app is killed.
If you look at the crash reports you'll see that iOS sends SIGKIL which you can't catch.
You cannot catch this, your processed really is killed. Hard. Without notice. That us why you need to save state when you enter background now.
Maybe setting up a signal handler might work (don't know which signal to catch, though).
I have a super weird problem. Every time I run a specific bit of code in the iPhone simulator, my entire Mac freezes: I can't close the simulator, or any other app, and can't open any apps. Worst of all, I can't shut down. I have to force a shut down manually by holding the power button.
I'm running on a 15" MacBook Pro, and the only thing running is Xcode and the iPhone simulator. I used Activity Monitor to shut down any unnecessary apps running in the background. I've also used OnyX to clean up my system.
I'm using Xcode 3.2.4, with iOS 3.2 (iPad simulator).
At first I suspected this was a processor issue - so I ran the same code to induce a crash while I had Activity Monitor running. I can still switch between apps when frozen, I just can't open/close any or do anything with Xcode or the iPhone simulator. Activity Monitor reported that there was plenty of room for more processing, so it can't be that. I'm not running low on system memory. If I've missed anything out that might help in debugging this, leave a comment and I'll find the information. I want to fix this ASAP.
The code in question handles a bunch of timers, 3 background threads and a lot of UDP packets. The background threads are: two UDP receive threads, and a send thread. The background threads run a constant loop, looking for data on two different ports. The send thread runs another constant loop, but only sends data when there is any in its queue. The UDP processing just uses socket(), bind(), sendto(), recvfrom(), and select() calls.
I was under the impression that any problems in the current Xcode project would only become apparent in the iPhone simulator - an over-release only crashes the simulator, etc. How is it possible to crash the OS, not the simulator?
Edit: It runs perfectly fine on a device. Doesn't crash, doesn't report any memory issues, nada.
To see if a background thread is stuck on a blocked network call, put an NSLog (or fprintf to a file and fflush it) before and after every possible blocking call, and run with the debugger console visible.
I have a question,
i carefully went through and thoroughly understood the tutorial for iOS Core Data.
The issue was that when i coded everything and run on iOS 4 with multitasking.. the program crashed when i re-opened it (kill task from fast app switching and relaunch)
Then i went curious and recode it on iPad (since it hasn't support multitasking) and it works fine...
I went to explore abit, and some says that the fast app switching kill app is like the OSX kill app and there is applicationTerminate method in the delegate file which maybe need some changes (based on the comment).
Could someone enlighten me on that issue? Is there anything additional that i need to do in order to have Core Data to support the Multitasking feature..
Thank you!
Update
Ok but the thing i am not getting is that.. once i killed the app.. go Fast App Switch, kill it... and when i re-run the app.. it crashed.. Do i need to add some stuff to the multitasking methods? It crashes even if its a basic app with 1 button... What are the states that i have to save?
Update
The app just freeze on its own
Update
Alright my bad, trying my best, for the one button app... I sticked a button in there, and for each click, it prints an NSLog and a label. When i build and run it, it works fine, clicking it prints them normally. After going to background and close the app.. it sends SIGKILL Then when i relaunch the app, it no longer prints to NSLog and crash there 0x98a450f0 <+0000> mov $0xffffffe1,%eax 0x98a450f5 <+0005> call 0x98a453d8 <_sysenter_trap> 0x98a450fa <+0010> ret 0x98a450fb <+0011> nop
Update
However when i open the Iphone simulator and run the program directly... without building it from xcode.. It works just fine and it was able to print that value. Same is true for the Core Data.. if i just run it from the Simulator. Kill it and re-run, it works fine.. Not sure what's going on
With iOS 4.0, your app may not receive a -applicationWillTerminate call. It may only receive a call to go into the background and then be killed.
When you get the call to go into the background you should dump as much memory as possible to avoid being killed, that includes saving your context and perhaps calling reset on the context to get it to drop its cache.
Other than that, no changes need to be made.
What crash are you seeing, a stack trace would be helpful to explore this further.
Update
Again What crash are you seeing?
Update
Freezes where? Where does it stop when you look at it in Shark or the debugger? You are not giving me a lot of information to go on here :)
I have an application that uses the accelerometer. Sometimes, the application will launch without the accelerometer data updating. Relaunching the app, sometimes the problem persist, sometimes it doesn't. And even weirder, sometimes I can try 10 times and everything works as expected. Is this a bug, or maybe something I am missing. Debugging, this code is never called when the accelerometer is not updated:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
Any ideas?
I finally found a work around. This is a known bug. So the work around I found is to start a thread and have this thread check if the accelerometer delegate has been called, if it has, then quit the thread, if not, set the delegate again, and re-test, until the accelerometer delegate gets called. I tested this throughly and it works flawlessly. I hope this gets resolved on the next update of the iPhone OS.
I have this same problem. It happens perhaps 1/20 times with an app I made from the CrashLanding sample. After I noticed it with my app I grabbed a fresh version of Crashlanding, installed it, and finally got it to start with accelerometer failing.
I don't know how to fix it. Honestly I hate the accelerometer... at least for controlling games :-\
Also, the accelerometer has occasionally failed when I start the "accelerometer" sample project.
As others have mentioned, this is a known bug; I have logged the bug with Apple (Bug Reporter problem ID 6093028), perhaps others have done so as well. As far as I know, all apps that makes use of UIAccelerometer (including Apple's sample apps) suffer from this issue, though the frequency of occurance varies.
FWIW, my app is widely used and it uses the accelerometer, and I've never had this problem.
My use case may be different than yours: I only enable it on user request, well after the application is launched.