Start a new topic

Kinvey Titanium - performance issue - What am I doing wrong ?

Hello,

I must be doing something wrong, but I cannot find my mistake since three days now.



I am new to Titanium Alloy and am preparing my next project with Kinvey as the backend. I followed the Kinvey Titanium Alloy installation procedure as closely as possible.



The app is working as desired, but I have big performance issues when fetching all the records, using the kinvey titanium sdk. I have found that the "fetch operation" returns the correct number of records, but when displaying the data in the view, the transformation is done a first time with the correct number of records, and then a second time with the squared number of records. This second time is of course incorrect and leads to the app crashing when the number of records get higher than 12-15. Hope you can help me find the mistake I am doing :smile:



Here is the titanium alloy code ::



# index.xml





















# index.js



var promise = Kinvey.init({

appKey : 'kid_VVXK2Lt00O',

appSecret : '427be2c9a2f7454e820a66828819ca4e'

});

promise.then(function(activeUser) {

if(activeUser === null){

var promise = Kinvey.User.login({

username : 'dstadel',

password : '1234'

}, {

success: function(response) {

alert("I am logged in, hurrah !");

fetchDriverRoute();

}

});

} else {

console.log("User déjà loggé !!!");

fetchDriverRoute();

}

}, function(error) {

var promise = Kinvey.ping();

promise.then(function(response) {

console.log('Kinvey Ping Success. Kinvey Service is alive, version: ' + response.version + ', response: ' + response.kinvey);

}, function(error) {

console.log('Kinvey Ping Failed. Response: ' + error.description);

});

});



function transformFunction(model) {

var transform = model.toJSON();

transform.fullName = transform.name + ' ' + transform.firstName;

console.log("I pass through Transform !");

return transform;

}



function fetchDriverRoute(){

console.log("Je passe à heroesfetch() !");

var promise = heroes.fetch({

success : function(collection, response, options) {

console.log(response);

},

error: function(error){



alert("Network error !");

},

timeout:KinveyTimeout

});

}

$.tabGroup.open();



# alloy.js

var Kinvey = Alloy.Globals.Kinvey = require('kinvey-titanium-1.1.7');

heroe = new Kinvey.Backbone.Model();

heroes = new Kinvey.Backbone.Collection([]);

heroe.url = 'personnes';

heroes.url = 'personnes';

KinveyTimeout = 6000;



# config.json

{

"global": {},

"env:development": {},

"env:test": {},

"env:production": {},

"os:android": {},

"os:blackberry": {},

"os:ios": {},

"os:mobileweb": {},

"dependencies": {},

"sourcemap": false,

"adapters": []

}



# tiapp.xml





com.dstadel.tdlk5

ToDoListKinvey5

1.0

Didier

http://

not specified

2014 by Didier

dreamstime_xs_34510620.png

false

false

true

e7012386-8e56-4977-bc85-332b8a2b2aae

dp







UISupportedInterfaceOrientations~iphone



UIInterfaceOrientationPortrait



UISupportedInterfaceOrientations~ipad



UIInterfaceOrientationPortrait

UIInterfaceOrientationPortraitUpsideDown

UIInterfaceOrientationLandscapeLeft

UIInterfaceOrientationLandscapeRight



UIRequiresPersistentWiFi



UIPrerenderedIcon



UIStatusBarHidden



UIStatusBarStyle

UIStatusBarStyleDefault







true



14






android:minSdkVersion="10" android:targetSdkVersion="14"/>











true

true



default







true

false

false

true

true

false



3.2.3.GA



ti.alloy






I see. This looks like a Titanium bug / feature. Whenever a Backbone Model is added to a collection, an `add` event is emitted. On this event, Titanium seems to take the collection, and call `transformFunction` for every model in the collection. Therefore, for two models the `transformFunction` is called four times, for three six and so on. Looks like this is more a Backbone vs. Titanium issue.



The workaround below actually calls transform only once, which may be useful if you want to test things out:



```

var promise = heroes.fetch({

success : function(collection, response, options) {

collection.trigger('reset');// Trigger event manually.

},

error: function(error){

alert("Network error !");

},

silent: true,

timeout:KinveyTimeout

});

```
Hello Mark,

Thank you for your comment.

I read the bug report you mention and I am not sure I have the same issue. It seems correct that two events are fired instead of one, which would not be a massive issue. The problem is that the second time, the number of transform is squared. This leads to unresponsiveness of the app as soon as the number of records is higher than ten-fifteen. To be noted also that I added the transformFunction for the sole purpose of debugging, since I get the same performance issues/crash without transform.

Please advise asap if this is a Kinvey issue, and if not, I will follow my post to Titanium.
I can see the `transformFunction` executing twice for every row. Internally, it looks like Backbone fires two events where it should fire one. At this point, I am not sure whether this is a Kinvey or Titanium/Backbone issue. I found a bug report [here](https://developer.appcelerator.com/question/157503/alloy-collection-transform-executes-twice-per-row), but apparently that should have been fixed in the latest Alloy release, which doesn’t seem to be so.
Login or Signup to post a comment