HIgh CPU after loading main view in ios

Hello,

I have an application that runs correctly in android, now I am trying to build the app in ios, but after launching the app in ios (in simulator and in device) the app takes more than 100% of cpu and it’s impossible to do anything. Also with the safari inspector can’t do anything as it’s blank, although in the device the main screen has loaded correctly, but can’t interact with it.

Also running the app with ionic serve and entering the ip address in the safari browser in the device and emulator, it works correctly.

Any help?

Can you provide more details? Is this ionic 1 or 2? Is this just a blank starter app or a full blown code base? The reasons could differ. If it is a huge project, my suggestion, would be to remove pages one by one and find out which controller is causing slowness. 95% its gotta be project code. May be you have an $interval or a bad timeout or a memory leak

Hi Srijth,

The ionic version is 1.7.16

If been able to find some clues.

At my controller I call a service to load the data throught an http request:

ApiModel.call("/sectores").then(function(sectores){
      $scope.sectores = sectores;
  }, function(error){
    console.log(error);;
});

And this is the code of my ApiModel service:

....
this.call = function(method, params){
    var q = $q.defer();

    var response;
    if(params == null || Object.keys(params).length == 0){
        response = $http.get(APP_CONSTANTS.API + method + "?r=" + new Date().getTime());
    }
    else{
        response = $http.post(APP_CONSTANTS.API + method + "?r=" + new Date().getTime(), params);
    }
    alert ("After request");
    
    response.success(function(result) {
        q.resolve(result);
    }).error(function(res){
        q.reject({error: 'http', message: res});
    });

    return q.promise;
}
....

Note that there is an alert in the code. If I maintain the alert it is shown after the app has loaded and after clicking OK in the alert it works correctly, but if I remove the alert, the app freezes and the interface is not responding and the application is reaching like 120% of cpu usage in Activity monitor.

Its really weird! Can you do wrap the call to this method in a $timeout like below (they are bad, but lets just see if it works fine)? I am guessing something else happens within your controller parallel to this call.

$timeout(funciton(){
ApiModel.call("/sectores").then(function(sectores){
      $scope.sectores = sectores;
  }, function(error){
    console.log(error);;
});
}, 5000);

What happens when your controller is loaded?
May be there are calls this like:

callSomething();

ApiModel.call()…

So instead of firing off parallel requests try to chain them like

callSomething().
then(doThat)
.then(doTheOtherThing)
.then(function(){
  ApiModel.call....
});

Hello Srijth,

Wraping the call in a timeout doesn’t work either.

I’ve spend so much hours without figuring out what can be.

I’ve removed all my code from the controller and the view, even the controller parameters. Then I’ve been adding the view code line by line, and executing in each line to see if the CPU usage increases or not. And the same with the controller.

The problem is at some point (aparently random) the CPU usage increases when I’ve added a new piece of code, then I remove the piece of coded adedd, and rerun the application, and the CPU usage keeps being 100%. It’s frustating. This happens even I add or remove a piece of code to the controller or to the view.

Now this becomes really weird.

The same code executed to times, the first time is eating memory (I’ve seen it eating up to 4Gb of memory and keep going) and the second time working fine, without any change in the code.

You can see in the first one, in the logs it doesn’t says “Ionic Ready” which is a console.log that is at app.js

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {     
       console.log("Ionic ready");
       ....
    }
}