I am working on a sizeable app in Fuse and I have been running into issues with it crashes on my device and emlator on load. I am creating a number of JS Objects (Models) with many Observable properties and there seems to be a limit where it causes the app to crash. I haven’t tried lazy loading my data yet, I just have it hard coded as an example and once I get to 40 or so objects created, I run out of memory.
So my question is this… what could be a better way of organizing this data. I ran into binding issues when all of the properties weren’t Observables, but maybe that’s a lot of overhead I don’t need and there is a better way to do it? I bind most of these properties to my views as I list out the businesses and then drill in for more detail.
Below is an example of my Business.js object I’m working with. Once I get to about 40 items, it crashes with out of memory errors. It maxes out around 640 MB on my iPhone 5.
var Observable = require('FuseJS/Observable');
function BusinessBase() {
this.BusinessID = Observable();
this.SmallImageID = Observable();
this.Business_Website = Observable();
this.UserSelected = Observable();
this.Business_PhoneNumber = Observable();
this.Address1 = Observable();
this.Address2 = Observable();
this.City = Observable();
this.State = Observable();
this.ZipCode = Observable();
this.Business_ShortDescription = Observable();
this.Business_LongDescription = Observable();
this.Latitude = Observable();
this.Longitude = Observable();
this.SortCategory = Observable();
this.Trending = Observable();
this.BusinessImageVC = Observable("");
this.Business_Name = Observable();
this.Business_Category = Observable();
this.Business_SubCategory = Observable();
this.DistanceMiles = Observable();
this.businessLogo = "OfferLogo";
this.set = function(obj) {
if(obj.BusinessID) this.BusinessID.value = obj.BusinessID;
if(obj.Business_Name) this.Business_Name.value = obj.Business_Name;
if(obj.Business_ShortDescription) this.Business_ShortDescription.value = obj.Business_ShortDescription;
if(obj.UserSelected) this.UserSelected.value = obj.UserSelected == 1 ? true : false;
if(obj.Distance) this.DistanceMiles.value = Number(obj.Distance).toFixed(1);
if(obj.Business_Catagory) this.Business_Category.value = obj.Business_Catagory;
if(obj.Business_SubCatagory) this.Business_SubCategory.value = obj.Business_SubCatagory;
if(obj.Business_Website) this.Business_Website.value = obj.Business_Website;
if(obj.SmallImageID) this.SmallImageID.value = obj.SmallImageID;
if(obj.Latitude) this.Latitude.value = obj.Latitude;
if(obj.Longitude) this.Longitude.value = obj.Longitude;
if(obj.SortCategory) this.SortCategory.value = obj.SortCategory;
if(obj.Business_LongDescription) this.Business_LongDescription.value = obj.Business_LongDescription;
if(obj.Business_PhoneNumber) this.Business_PhoneNumber.value = obj.Business_PhoneNumber;
if(obj.Address1) this.Address1.value = obj.Address1;
if(obj.Address2) this.Address2.value = obj.Address2;
if(obj.City) this.City.value = obj.City;
if(obj.State) this.State.value = obj.State;
if(obj.ZipCode) this.ZipCode.value = obj.ZipCode;
if(obj.Trending) this.Trending.value = obj.Trending;
if(obj.ImageVC) this.BusinessImageVC.value = obj.ImageVC;
this.businessLogo = "OfferLogo";
}
}
function GetBusinessFromID(id, businessList) {
for(var i = 0; i < businessList.length; i++) {
var business = businessList.getAt(i);
if(business && business.BusinessID && business.BusinessID.value == id) {
return business;
}
}
return null;
}
function BuildBusinesses(rawObj, businessList, sortBy) {
debug_log("BEGIN BuildBusinesses()");
debug_log("rawObj.length: " + rawObj.length);
var tempArray = [];
for(var i = 0; i < rawObj.length; i++) {
tempArray.push(buildBusiness(rawObj[i]));
}
SortBusinessList(sortBy, tempArray);
businessList.replaceAll(tempArray);
debug_log("END BuildBusinesses()");
}
function buildBusiness(rawObj) {
var business = new BusinessBase();
business.set(rawObj);
return business;
}
function SortBusinessList(sortBy, tempArray) {
tempArray.sort(sortMethod);
function sortMethod(v1, v2) {
if(v1[sortBy.value] < v2[sortBy.value]) return -1;
if(v1[sortBy.value] > v2[sortBy.value]) return 1;
return 0;
}
}
module.exports = {
BusinessBase: BusinessBase,
GetBusinessFromID: GetBusinessFromID,
BuildBusinesses: BuildBusinesses,
SortBusinessList: SortBusinessList
};
In addition, I have many other views I’ve removed from my project to test this, and the only thing that makes a large difference in memory is how these objects are being created and handled. I can add 40 new views and not change the amount of data I’m creating here and the memory footprint barely moves. That leads me to beleive it’s this and not the construction of my views in UX (which makes me feel really good that the UX is very efficient).