In the development of my Ionic application, I encountered a challenge regarding downloading data with pagination and inserting it into the database recursively. I am utilizing Angular's $q service to create promises for this purpose. The issue arises when executing the recursive function as although the data is successfully downloaded and inserted, the memory usage keeps increasing throughout the process. Even after the promise chain resolves entirely, the allocated memory remains in use.
Below is the snippet of my recursive function:
// offset: last downloaded row count
// limit: row count to download at each page
// numRows: row count to download at all
function dowloadAndInsert(offset, limit, numRows) {
var deferred = $q.defer();
// Recursion step: We do not reached at the end of data
if((offset + limit) <= numRows) {
// Download the data
downloadData(offset, limit)
.then(function(response) {
// Insert the data
insertData(response)
.then(function(insertTime) {
// Recursion step
dowloadAndInsert(offset + limit, limit, numRows)
.then(function() {
deferred.resolve();
})
.catch(function(reason) {
deferred.reject(reason);
});
})
.catch(function(reason) {
deferred.reject(reason);
});
})
.catch(function(reason) {
deferred.reject(reason);
});
}
// Base case: We reached at the end of data
else {
var remainingRows = numRows % limit; // Means the last limit actually
// If exists, insert remaining rows
if(remainingRows !== 0) {
// Download the last piece of data
downloadData(offset, remainingRows)
.then(function(response) {
// Insert the last piece of data
insertData(response)
.then(function(insertTime) {
// Base case, successfully downloaded and inserted everything
deferred.resolve();
})
.catch(function(reason) {
deferred.reject(reason);
});
})
.catch(function(reason) {
deferred.reject(reason);
});
}
else {
// Base case, successfully downloaded and inserted everything
deferred.resolve();
}
}
return deferred.promise;
}
Note: The response object obtained from the downloadData function consists of a substantial amount of data, sometimes exceeding 100,000 rows with 18 columns. As a result, the total memory usage can reach up to 1GB while testing on an iPad Air 2 device.
Considering the significant volume of data processed in my recursion function, my query regarding memory management presents a unique aspect compared to other inquiries about recursive memory leaks.
Thank you.