Equivalent of viewDidAppear in RCTViewManager - ios

I've created a custom view for a react-native app, which is itself a C++ DSP application and runs on a timer. The view is initialised on startup using it's init method. However in order to save CPU cycles I need to know when the view is visible or not.
If I was using an iOS UIViewController I could employ viewDidAppear or viewWillAppear to send the message to the C++ code to start the timer, and viewDidDisappear to tell it to stop.
I'm looking for a clean way to do the same from within an RCTViewManager.

Related

After my app is suspended by the system, the Initial View Controller does not load properly

I'm having an issue that exclusively occurs when I launch my app after it was suspended (while in background) by iOS. In any other situation, the issue won't occur - which already is quite weird. As using the XCode debugger prevent suspension of the app when it goes to background, I have been collecting logs through the Unified Logging system to investigate.
The logs indicate that when I launch the app after suspension by the system, the storyboard scene that is defined as Initial View Controller does get displayed but the code that sits in the viewDidLoad() function of the associated view controller class is not being run.
This happens exclusively after suspension by the system, and will never happen if I manually (or through the XCode stop button) kill the app and relaunch it.
I'm totally stuck as I don't understand why this is happening and why there would be a difference induced by the system suspension.
My project is declaring the main storyboard as "Main Interface" in the target settings. In this storyboard, a scene is defined as Initial View Controller. This scene has a custom class defined, and when I click the arrow next to the custom class name, XCode does take me to the class declaration code.
The class declaration code looks like this:
import UIKit
import SwiftUI
import Firebase
import os.log
class InitialLoadViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//custom code
}
}
The custom code starts with a logging statement that I don't see printed when the issue occurs.
Hope someone may have an idea about what's happening!
thank you 🙏
I could be completely wrong, but I believe that while your app is not getting reloaded, it is disappearing. Try using viewDidAppear or viewWillAppear instead of viewDidLoad for the code you want to be executed after suspension.
After a lof of debugging I found out my issue was related to some of my app background activities. My app is sometimes receiving silent push notifications that wakes it up in the background to perform a network call that refreshes some local data. This process in itself was working well, but waking up my app in the background was also instantiating some view controllers (ie starting up my UI). But my UI has some dependancy to additional network calls that were not designed to work while the app is active in the background. They were therefore failing and leaving the UI in a fail condition that was not handled when the app is in background.
So I have now basically conditioned some aspects of launching my UI to having my app active in the foreground, rather than just having my app active.

Code placement for launch time improvement

When checking through the time profiler, there is a gap in between Launching and Foreground - Active. I assume this to be the post main time that the app takes to launch.
So, I moved functions that do not need to be triggered until launch from my AppDelegate to the viewWillAppear of the rootViewController of my app. And still, this code contributes to the post main time of the app.
Where should I place such code so that it doesn't affect the post main time of my app?
Try moving those initializations from viewWillAppear to viewDidLoad. That way, they won't start up until the initial view is visible.

Does delegate have function which is executed every time app comes from background

I have application with lot of view controllers and on every time when app comes from background I have to make some request to server and reinitialise some global variables. At the moment I am doing this by repeating code in every view controller in didViewLoad but I wonder is there way to this in delegate to avoid repeating on 10 places ? ( I check didFinishLuanchingWithOptions in delegate but it is called only first time not when app comes from background ).
I think you want to look at these two:
applicationDidBecomeActive
And
applicationWillEnterForeground
Check out the documentation for more details.

iOS 8 viewDidAppear called before appWillBecomeActive?

I'm just experiencing something weird and it seems to be a change in iOS 8.
Previously(iOS7) when testing appWillBecomeActive was called before viewDidAppear. Is it so that in iOS 8 it is the other way around? what would then be a good workaround in order to make my app work on both iOS versions? is there some variable to test if viewDidAppear was called so I could run my setup functions of the view again?
EDIT: it actually seems quite random in iOS8. sometimes viewDidAppear is called before appWillBecomeActive. Sometimes it's the other way around...
appWillBecomeActive is a delegate located in your Application Delegate itself.. there's no guarantee that it will be called before any other UIViewController delegates (viewWillAppear,DidLoad,Init)
if you want to make any logic before loading of any other pages come alive.. you may want to use application:didFinishLaunchingWithOptions: and you may want to load the launching view by yourself or create a new delegate to detect that you finished the logic that you'll put in your application:didFinishLaunchingWithOptions: .

Objective-c iOs Autosaving information when exiting a view

I am programming an ios app and would like to save information on exit of the view. I know how to go about saving the actual information but I'm not sure where I should put the code.
In android, there are methods like onPause() where I can run the save code to capture whenever someone leaves an activity. Is there something in obj-c like that?
You can add your logic for saving the state of the view in viewWillDisappear
I guess you have not gone through the View Life Cycle, here s one nice image which has captured the view life cycle events.
Reference : http://rdkw.wordpress.com/2013/02/24/ios-uiviewcontroller-lifecycle/

Resources