NSNotification & didReceiveMemoryWarning in iOS 6 - ios6

As of iOS 6.0 , viewDidUnload method is deprecated. Before iOS 6 I used to removeObserver of NSNotification in viewDidUnload method. But since it is deprecated , I have moved it to didReceiveMemoryWarning. Now if my app receives the low memory waring , notification are removed. So my code written in NSNotification doesn't work.
Can anybody tell me how can I solve this issue ?
Thanks in advance.

I assume that you added the observer in viewDidLoad. The problem is that on iOS 6 views are not unloaded, even in a low memory situation. Therefore, if you remove the observer in didReceiveMemoryWarning, viewDidLoad will not be called again.
There are two relative simple alternatives that you can choose from:
Add the observer in viewWillAppear and remove it in viewWillDisappear.
Add the observer in your initXXX method and remove it in dealloc.
I is possible to add the observer in viewDidLoad and remove it in didReceiveMemoryWarning. But then you have to "manually unload" the view in didReceiveMemoryWarning, so that viewDidLoad is later called again. See e.g. https://stackoverflow.com/a/15805715/1187415 for sample code how to forcibly unload the view.

The problem is you are completely misunderstanding what viewDidUnload means. viewDidUnload is called when the view is unloaded. In pre-iOS 6, when you are done with a view controller, its view does NOT get unloaded. In pre-iOS 6, 99% of the time you will never see viewDidUnload being run, because views are usually not unloaded. In pre-iOS 6, views are only unloaded in response to a memory warning and the view is not visible. In iOS 6, the only change is that views are never unloaded, even in a memory warning.
The behavior of viewDidUnload is identical in pre-iOS 6 and iOS 6 -- it is called when the view is unloaded. Because of this, you should NOT have to change any code for iOS 6. If you do, you did something wrong.
Before iOS 6 I used to removeObserver of NSNotification in viewDidUnload method.
If what you mean is that you added the observer in viewDidLoad, then you must have it removed in dealloc. Otherwise your code will crash. As I said above, 99% of the time, in pre-iOS 6, views are not unloaded. The typical flow is init -> viewDidLoad -> dealloc. The view does not get unloaded in the middle.
You can optionally also remove the observer in viewDidUnload. It is a good idea because it returns it to the state before the view was loaded. However, it is not strictly necessary, because when the view loads again, viewDidLoad will be called, and overwrite whatever state there was before anyway.
So the answer is, you should not have to change anything in response to iOS 6. Whatever you did in viewDidLoad should be undone in both viewDidUnload (optional) and dealloc (required). This is true in both iOS 6 and pre-iOS 6.

Related

Why does react empty attributes when using a web component?

I am pretty new to react and trying to use a web component in the template and there are is a thing i dont understand.
It can be seen in this example below if you run the page (may need to hit reload once). Then watch the console. You will see the log in the constructor() the outerHTML is not "ready" yet its missing all attributes in the markup and they arrive only later on (in the connectedCallback) which is the second log.
https://stackblitz.com/edit/react-ts-jcjuvs?file=App.tsx,index.tsx
Whats happening here? Is there anyway to force the attributes to be available initially? Or not taken off and then put back on?
Note that doing this in a normal page does not have the same effect.
https://stackblitz.com/edit/js-l4uh4f?file=index.js,index.html
a) whats happening?
b) can i make this not happen?
Thanks!
There no no guarantee that a web component will have access to attributes in the constructor and attribute values can change at anytime. You should use the attributeChangedCallback lifecycle callback to know when values are set.
The element's attributes and children must not be inspected, as in the non-upgrade case none will be present, and relying on upgrades makes the element less usable.
Requirements for custom element constructors and reactions

Attempt to invoke virtual method 'void android.graphics.Point.setAntiAlias(boolean)' on a null object reference

Within a Codename1 app, I'm getting a CrashReport: java.lang.NullPointException: Attempt to invoke virtual method 'void android.graphics.Point.setAntiAlias(boolean)' on a null object reference. This seems to occur after I invoke a "show()" on the main form of my application.
Note that the code runs fine in the simulator, but consistently gets this error prior to doing the "show()" on Android.
Interestingly, if I put a Dialog like this:
Dialog.show("Wait a sec", "Showing interrupt point", "OK", null);
before the .show(), and then click "OK", then everything runs well with no exceptions at all.
But a sleep(5000) instead of the Dialog does not help - still get the exception. So it at least seems like its not a race condition.
I have try-catches wrapped around all of the potentially offending code, and have NOT been able to isolate this. It always gets caught by the CrashReport, and only when running on the Android device.
Any ideas?
The symptoms to this one are pretty strange (i.e. workaround with a Dialog), and it would be interesting to have an explanation. However, since deprecated cn1 (Map) code has seemingly been implicated, I'm going to let this one go and replace this code with the latest Google native maps code. If it recurs, I will post another question then.

Whats the difference between OnApplicationFocus() and OnApplicationPause()?

Speaking of mobile devices, whats the difference between those two methods? If I press the home key both are called. Is there any situation when one is called and not the other?
Since this UnityAnswer is one of the first (if not the first) to be
returned on a search for OnApplicationFocus/Pause & iOS, an important
update in Unity 4.6.1 has changed the behavior for iOS.
As of 4.6.1, both OnApplicationFocus and OnApplicationPause will be
called in iOS.
The order is :
App initially starts:
OnApplicationFocus(true) is called
App is soft closed:
OnApplicationFocus(false) is called
OnApplicationPause(true) is called
App is brought forward after soft closing:
OnApplicationPause(false) is called
OnApplicationFocus(true) is called
Hope that helps
http://answers.unity.com/answers/970958/view.html

Understanding AngularJS and Google Chrome memory management

i was wondering why - even on the simple SPA application with AngularJS there seems to be a DOM leakage. I may be misinterpreting this but the way I look at this is that DOM elements allocated are not being released properly.
The procedure to reproduce is as follows:
navigate to the page on the screenshot with simple AngularJS application
turn on timeline recording in developer tools
force garbage collection
add an item, and then remove it
force garbage collection
repeat last two steps for atleast 3 times
On the screenshot you can see that after you add an item and remove it there seems to be two more DOM elements more after garbage collection(jump from 502 to 504 DOM elements).
I was hoping that someone could shed some light on this before i get deeper on investigating what is happening. Reason for this test was more complicated AngularJS SPA that I am working on and which also seems to leak memory.
I'm doing a similar thing now. What I've noticed is a couple of things:
1) look at any usage of $(window).fn() -- where fn is any of the functions on the window object; if you're doing that more than once you're adding multiple event handlers to the global object which causes a memory leak
2) $rootScope.$watch() -- similarly, if you're doing this more than once (say, when loading a directive) then you're adding multiple handlers to the global object
3) In my tests (where I go back and forth between two pages) it seems that chrome consumes a large amount of memory (in my case, almost 1GB) before garbage collection kicks in. Maybe when you click the "garbage collection" chrome is not actually doing a GC? Or it's GC for javacsript but not for dom elements?
4) If you add an event handler to the dom element, then remove it from the dom, the handlers never get GC'ed.

Implementing Google Analytics 2 in IOS 6

While updating an app to ios 6, I found google analytics also needs update.
Following this method, I discovered it needs a ViewController which extends GAITrackedViewController class
https://developers.google.com/analytics/devguides/collection/ios/v2/
The guide mentions the following code in ViewDidLoad method, whereas I want it in some specific method. It doesn't seems to work. Kindly advise.
self.trackedViewName = #"My App";
Using GAITrackedViewController means giving up control over when [tracker trackView:viewName] gets called. Setting the trackedViewName allows GAITrackedViewController to call [tracker trackView:self.trackedViewName] whenever it wants (presumably in viewDidAppear).
Also, GAITrackedViewController uses a KVO on this property, so view tracking doesn't get turned on until self.trackedViewName = #"view name" is performed.
Calling this in viewDidLoad assures that it's setup correctly by the time the first viewDidAppear gets called.

Resources