Save data in ApplicationDidEnterBackground terminating due to background thread pause delay - ios

My app currently downloads data from a web service. The data received is processed using background serial queues.
I also want to save the data to the file system when the user goes into background mode and use NSCoder to do this. Based on Apple's recommendation, I have included the save file logic in the ApplicationDidEnterBackground method of the app delegate.
However, I am having problems ensuring the data is not mutated before the save to file system is called because the background thread pause is not called immediately.
For example, a refresh of client data is kicked off and returns 2000+ records (simple data scheme). However, while this is running, the user presses the Home button and the App goes into the background and attempts to save the data to file system.
Within the block processing of the background thread, I have included logic to log when clients are added to the array, as well as included NSLog messages to tell me when the ApplicationDidEnterBackground method is called. Here are the relevant parts of the logging results:
2014-06-06 12:32:02.360 MyApp[629:190049] Added client
2014-06-06 12:32:02.365 MyApp[629:190049] Added client
2014-06-06 12:32:02.416 MyApp[629:189936] This is a call to applicationDidEnterBackground to save data to file
2014-06-06 12:32:02.371 MyApp[629:190049] Added client
. . . (Log continues with Added Client messages)
2014-06-06 12:32:03.952 MyApp[629:190049] Added client
2014-06-06 12:32:03.951 MyApp[629:189936] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x157c74f0> was mutated while being enumerated.'
*** First throw call stack:
(0x23df543b 0x3096ed1f 0x23df4ec1 0x24ab44e7 0x24ab4ad9 0x24ab32ed 0x24affb53 0x4f879 0x4f71b 0x2a257 0x2762021d 0x2761c51d 0x2a32ae27 0x2a3319f5 0x23dbd135 0x23dbc3f9 0x23dbaf53 0x23d077e1 0x23d075f3 0x2ae5a261 0x27412e3d 0x27e4b 0x30f07aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
2014-06-06 12:32:03.960 MyApp[629:190049] Added client
2014-06-06 12:32:08.025 MyApp[629:190049] Added client
2014-06-06 12:32:08.028 MyApp[629:190049] Added client
2014-06-06 12:32:08.030 MyApp[629:190049] Added client
As can be seen from the logs, the background thread block processing continues to process records even after ApplicationDidEnterBackground is called (and in fact, even after the exception is called), causing the file save to fail due to a mutated array.
Does anyone have any suggestions on how best to handle ensuring the save of file only occurs AFTER the background queue is paused? I would prefer not to cancel the thread processing if possible. Thanks in advance.

In case someone else has a similar problem...
I resolved this through adding the save to file methods to the same background queues I was processing each data set on rather than trying to cancel what was part way through.
This way, the save data did not kick in until the other data updates were complete, and the issue of saving to file while the array was being mutated has disappeared.

Related

how to solve 'Object has been deleted or invalidated.' Realm exception

i have different realm objects in different ViewControllers and from my settingViewController i'm deleting all the data of app (the realm stored objects ) its working fine but when i move back to those viewControllers i got this exception :
Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'
*** First throw call stack:
(0x180c0adb0 0x18026ff80 0x100a13e7c 0x1001bd54c 0x1001be77c 0x1860c288c 0x1860c2c3c 0x185eb78e8 0x185d775b4 0x185eb6d34 0x192375f40 0x185ef1c94 0x192375ccc 0x1001b97ac 0x1001bbe4c 0x1860bf030 0x1860bf198 0x1860ae298 0x1860c3c64 0x185e548c4 0x185d641e4 0x1836f698c 0x1836f15c8 0x1836f1488 0x1836f0ab8 0x1836f0818 0x1836e9ddc 0x180bc0728 0x180bbe4cc 0x180bbe8fc 0x180ae8c50 0x1823d0088 0x185dd2088 0x100121cc8 0x1806868b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
i know its happening because system (kernel , iOS) dont knows that i've deleted those Realms Objects and system is trying to use that data which is not exists anymore (Correct me if i'm wrong) , any one can guide me on how i can fix this problem ???
I suggest you to make notification before deleting all your data to all view controllers, that manipulate with it:
Push notification from your settingViewController before wipe
Subscribe to this everywhere you need to clear objects.
delete references
perform clean in your settingViewController.
Or, other way - implement delegate pattern for your purpose. The idea is the same.
Hope this helps.

Why am I getting a `unrecognized selector sent to instance` error?

I have a Ruby on Rails application that has an API, it's an OAuth 2.0 provider and uses Doorkeeper. I am creating an iPhone client for that application and am using the gtm-oauth2 library for authentication. Here is the Github repository for the iPhone app.
I manage do the authentication request using the library and get the response from the OAuth server, but (I think that) when the iPhone app receives the response, the iPhone app crashes. I get the following error:
2013-03-25 07:30:51.563 Catapult for iOS[68917:c07] -[NSNull length]: unrecognized selector sent to instance 0x14f2678
2013-03-25 07:30:51.564 Catapult for iOS[68917:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x14f2678'
*** First throw call stack:
(0x13c1012 0x11e6e7e 0x144c4bd 0x13b0bbc 0x13b094e 0xf074 0x185e3 0x13b51bd 0x13b50d6 0x1531a 0x1512b 0x14ae2 0x13b51bd 0x13b50d6 0x11d0a 0x1032a 0x13b51bd 0x13b50d6 0x79be 0x77ed 0x8cf2 0xcec589 0xcea652 0xceb89a 0xcea60d 0xcea785 0xc37a68 0x4a2a911 0x4a29bb3 0x4a67cda 0x13638fd 0x4a6835c 0x4a682d5 0x4952250 0x1344f3f 0x1344a39 0x1367734 0x1366f44 0x1366e1b 0x22be7e3 0x22be668 0x12affc 0x2c6d 0x2b95)
libc++abi.dylib: terminate called throwing an exception
I am a complete noob/beginner when it comes to iOS development and Objective-C programming, and I am learning how to create my first app using this little project. I asked the same question in the gtm-oaut2 Google Group and according to them, the problem comes from my code and not the library. The problem is that all of my code is taken from their wiki and I can't pinpoint where the app crashes. If I understand correctly, at some point I am calling length of NSNull, but I am not calling length anywhere, hence my confusion. Also, when the app crashes, the line hilighted is in the main.m file and the error (on the right label, not in the output) is the following:
Thread 1: signal SIGABRT
I have no clue what that means...
Does anyone know what the problem might be please?
You're right that the issue is that somewhere you've got the length message being sent to an instance of NSNull. To pinpoint where exactly this is happening set a breakpoint on objc_exception_throw. You can do this in the Xcode UI with little "+" button in the bottom left corner on the breakpoints tab. Select "Add Exception breakpoint." Then the debugger will stop your program at the point where the error occurs instead of waiting for the program to actually crash.
Well... you understood the error correctly.. You are somewhere calling length on a NSNull instance.
I must find out where this is happening.
I think this is happening in a JSON response. Maybe you (or "the code") is calling length on something it expects its a NSString but it is instead NSNull (that is the json has a NULL value).
Try if you manage to print the json response and see if you found a key mapped to a null value...
NSNull does not respond to length
You can check the documentation for NSNull to see that this is the case.
Without having an idea of what your code base is doing I am not sure where to look, you must be calling [NSNull null]; at some point to get the NSNull object or you are using a framework somewhere that returns this.

Exception with insertObject:atIndex: on iOS6

I'm getting the following Exception on iOS6 (on an App with CoreData):
"2012-10-15 10:21:28.952 MyApp[68650:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
* First throw call stack:
(0x28e6012 0x2659e7e 0x2899b6a 0x2899a20 0x1646941 0x1642c67 0x164f846 0x164f908 0x6c540 0x2057e83 0x28a5376 0x28a4e06 0x288ca82 0x288bf44 0x288be1b 0x33967e3 0x3396668 0x15a165c 0x13a22 0x2845)
libc++abi.dylib: terminate called throwing an exception"
This doesn't happen on iOS5, so something happens on iOS6 what I don't understand.
I set a Breakpoint on every point where I call insertObject:atIndex: but these are not called - it have to be something in this libc++abi.dylib which gets called and crashes.
Does anyone know what could be wrong?
thank you
This is probably because either iOS5 did not throw an exception for this error (and should have, but now iOS6 throws one which is better than having erratic behavior later), or because you have some different behavior in iOS6 which makes your object nil whereas it was not in iOS5.
Whatever the reason, you can add a Symbolic Breakpoint on the insertObject:atIndex: symbol, so that it will break every time this method is called, wherever it is in your application (in your own code or not).
Go to the "Breakpoints Navigator" view (Cmd-6 shortcut)
Click on the "+" button to add a symbolic breakpoint
Set the symbolic breakpoint to break when it hits the symbol [NSArray insertObject:atIndex:]
Thus you can see when this is called with a nil value for the first parameter and fix your problem where it occurs.
You can also instead add an Exception Breakpoint to break when an exception is thrown, thus knowing when in the code your exception occurs. This is another way to let you know which part of the code (your own or another) generate the exception.
Once the breakpoint has been hit and the program stops before the exception occurs, you can check in the call stack what part of your own code led to trigger this exception at the end.
The reason for the crash is that the object you are trying to insert is nil. This means it is not properly instantiated. This in turn means something has gone awry before you reached that exception.
Could you post the code that alloced and initialized the object you were trying to insert?
In order to find the relevant line of code, please try the following: Go to the "Exception" tab in your Xcode project:
Then click the "+" button (at the bottom of the page) and select "Add Exception Breakppoint ...". Leave all settings to their defaults and click "Done".
If you rerun your project it should now stop at the relevant line of code before the exception is thrown. Then you can move up the call-stack and identify from where in your code you called the library function that is responsible for this behavior. Then try to see if all objects are correctly initialized at this point.

Unity iOS game crashing with NSInternalInconsistencyException

I am using Prime31's GameCenter Turn Based plugin in my game in order to handle online matches. A problem arises when I receive an "invitation to play" notification from Game Center while Game Center's matchmaker is showing (called using the plug-in => GameCenterTurnBasedBinding.findMatch(2,2,false); ).
The app crashes and the following output is shown in Xcode:
2012-08-20 08:39:27.050 Cabrais[1808:707] *** Assertion failure in -[NSIndexPath row], /SourceCache/UIKit/UIKit-1914.85/UITableViewSupport.m:2606
2012-08-20 08:39:27.052 Cabrais[1808:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
*** First throw call stack:
(0x355ec88f 0x33210259 0x355ec789 0x349e03a3 0x32d4d673 0x35dba49d 0x35dd90e5 0x35dd9379 0x32d4cefb 0x32d4bfd9 0x32d4b763 0x35da7657 0x32ceff37 0x3554b1fb 0x32716aa5 0x327166bd 0x327165c9 0x35dd8179 0x35da936b 0x35dbce65 0x35dbb6b3 0x33979c59 0x33984e91 0x355bf2ad 0x355424a5 0x3554236d 0x332dd439 0x32d1acd5 0x6954 0x3388)
terminate called throwing an exception(lldb)
I have tried disabling notifications through the Ipad/Iphone settings for both my app and GameCenter as well as through the code by removing all notification related code but this hasn't had any effect at all.
I know through debugging that the code in my function attached to GameCenterTurnBasedManager.handleTurnEventEvent doesn't get called before the crash occurs, and neither does the one registered to EtceteraManager.remoteNotificationReceived. (Both these function work perfectly fine otherwise.)
I have asked Prime31 and they have told me that the exception is a mishandling of the tables data source, and to file a bug report with Apple.
Has anyone experienced a similar crash/error while working with Unity?
And is there any way I can try to catch the notification before it arrives and remove the matchmaker or be able to handle the error from unity?
Any insight/help/comments would be greatly appreciated,
Thanks.

ios understand the SIGABRT in my block

I have a block that fires once my image is downloaded. the data coming in from the block is the image. I'm not sure why I'm getting a SIGABRT though. This works fine on the first load, but when I come back to this same spot I always crash out the second time.
Everything you would want to see should be in the bottom left and printed out on the bottom right in the following order: data, ptr, ptr->imageImg
** EDIT **
I forgot to add the stack dump as well
2012-06-21 15:52:59.119 iPhone App[25327:16a03] -[UIImage length]: unrecognized selector sent to instance 0x10e41b80
2012-06-21 15:52:59.120 iPhone App[25327:16a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x10e41b80'
*** First throw call stack:
(0x1c35022 0x37dccd6 0x1c36cbd 0x1b9bed0 0x1b9bcb2 0x68e97a 0x5612c8 0x56102b 0x1be82 0x2afbc 0x46ad330 0x46af509 0x1b6c803 0x1b6bd84 0x1b6bc9b 0x1dc77d8 0x1dc788a 0x546626 0x28704 0x20d5)
terminate called throwing an exception(lldb)
You're calling length on UIImage, but UIImage doesn't have a method called length.
After hours of trying not to pull my hair out, I finally figured it out.
the situation
I have a caching system to cache a file on download and then send the stored version back to my callback. For some reason I was trying to save to a filepath that had a question mark (and other symbols) in it which apparently would NOT let ios save the file to the device *note xcode nor the device ever warned me that the file wasn't be saved either. However the record keeping system was showing that everything was working fine which meant the data sent back was empty NSData.
the solution
I made sure that my filepath had NO symbols in it when saving offline data and viola.

Resources