$q promises out of memory


#1

I made functionality that create like 50 promises in some order and after all done how to clear them because when i run function once more memory goes to 150mb and iPad is hanging out app … Is there any way to clear promise after its success?


#2

Do you do something like:

var promises = [];
for (var i = 0; i < 50; i++) {
  promises.push(
    $http.get(endpoint + "/" + i).then(function(response) {
      // Do something
    })
  );
}

$q.all(promises).then(function() {
  // Finally everything is done
});

#3

no something complicated ill show you example!

 $scope.CountOfMasters = 0;
$scope.CurrentMasterCount = 0;
$scope.IssesPromises = [];
$scope.EsbContractsRek = [];
$scope.SychWithClick = false;
$scope.SynchronizeMaster = function (click) {
    $scope.IssesPromises = [];
    $scope.SychWithClick = click;
    syncService.GetMasterESBForUser(function (d) {
        $scope.hideLoginIn();
        $scope.CountOfMasters = d.ESBCount;
        $scope.CurrentMasterCount = 0;
        if ($scope.CountOfMasters > 0) {
            $scope.EsbContractsRek = d.EsbContracts;
            console.log('rekurencja start');
            $scope.EsbRek($scope.EsbContractsRek.pop()).then(function (s) {
                console.log('rekurencja done');
            }, function (e) {
                app.Logger(e);
                $scope.hideDataDownload();
            });
        }
    }, app.UserData.ServerID);
};
$scope.EsbRek = function (esbContract) {
    var q = $q.defer();
    if ($scope.EsbContractsRek.length == 0) {
        q.resolve($scope.SaveFromMasterWithCheck(esbContract).then(function (s) {
            var q = $q.defer();
            console.log('last esb');
            q.resolve((function () {
                console.log('done esb rek');
                if($scope.SychWithClick){
                    $scope.SyncES();
                }
            }()));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
    } else {
        $scope.SaveFromMasterWithCheck(esbContract).then(function (s) {
            console.log('next esb');
			$timeout(function(){
				$scope.EsbRek($scope.EsbContractsRek.pop());
			},200);
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        });
    };
    return q.promise;
};
$scope.SaveFromMasterWithCheck = function (entry) {
    console.log(entry);
    var q = $q.defer();
    $scope.showDataDownload();
    q.resolve($scope.SaveMasterCustomer(entry).then(function (s) {
        var q = $q.defer();
        $scope.hideDataDownload();
        q.resolve(true);
        return q.promise;
    }));
    return q.promise;
};
$scope.SaveMasterCustomer = function (entry) {
    var q = $q.defer();
    var selectCustomre = "select * from Customers where ServerID = ?";
    var customerInsertQuery = "insert or replace into Customers(ServerID,BuyerID,CompanyName,ShortName) values (?,?,?,?)";
    var customerUpdateQuery = "update Customers set BuyerID = ?,CompanyName = ?,ShortName = ? where ServerID = ?";
    q.resolve($cordovaSQLite.execute(db, selectCustomre, [
        entry.CustomerContract.ServerID
    ]).then(function (s) {
        console.log('customer select done');
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('customer will make instert');
            var customerChangePromise = $cordovaSQLite.execute(db, customerInsertQuery, [
                entry.CustomerContract.ServerID,
                entry.CustomerContract.BuyerID,
                entry.CustomerContract.CompanyName,
                entry.CustomerContract.ShortName
            ]);
        } else {
            console.log('customer will make update');
            entry.CustomerContract.id = s.rows.item(0).id;
            var customerChangePromise = $cordovaSQLite.execute(db, customerUpdateQuery, [
                entry.CustomerContract.BuyerID,
                entry.CustomerContract.CompanyName,
                entry.CustomerContract.ShortName,
                entry.CustomerContract.ServerID
            ]);
        }
        q.resolve(customerChangePromise.then(function (s) {
            var q = $q.defer();
            if (entry.CustomerContract.id == null) {
                console.log('saved id = ' + s.insertId);
                entry.CustomerContract.id = s.insertId;
            } else {
                console.log('updated id =' + entry.CustomerContract.id);
            }
            q.resolve($scope.SaveMasterESB(entry));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterESB = function (entry) {
    var q = $q.defer();
    var selectEsb = "select * from ExecutiveSummaries where ServerID = ?";
    var esbInsertQuery = "INSERT or REPLACE INTO ExecutiveSummaries(ServerID,OpportunityReference,ESStatusID,ModifiedBy,ModifiedDate,CreatedBy,CreatedDate,DateRequired" +
        ",DeletedBy,Deleted,DeletedDate,OpportunityTitle,CustomerID,BuyerID,IsDecisionGroup,IsWritingStyleFormal," +
        "Step2_Intention,Step2_Achievment,Step2_GivenReason,Step2_SolutionAttribute,Step2_ComplianceID,CheckOutDate,CheckOutPerson,NextSteps,NextStepsOther" +
        ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    var esbUpdateQuery = "update ExecutiveSummaries set OpportunityReference = ?,ESStatusID = ?,ModifiedBy = ?,ModifiedDate = ?,CreatedBy = ?,CreatedDate = ?,DateRequired = ?" +
        ",DeletedBy = ?,Deleted = ?,DeletedDate = ?,OpportunityTitle = ?,CustomerID = ?,BuyerID = ?,IsDecisionGroup = ?,IsWritingStyleFormal = ?," +
        "Step2_Intention = ?,Step2_Achievment = ?,Step2_GivenReason = ?,Step2_SolutionAttribute = ?,Step2_ComplianceID = ?,CheckOutDate = ?,CheckOutPerson = ?,NextSteps = ?,NextStepsOther = ? where ServerID = ?";
    q.resolve($cordovaSQLite.execute(db, selectEsb, [
        entry.ServerID
    ]).then(function (s) {
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('esb will make instert');
            var esbChangePromise = $cordovaSQLite.execute(db, esbInsertQuery, [
                entry.ServerID,
                entry.OpportunityReferences,
                entry.ESStatusID,
                entry.ModifiedBy,
                entry.ModifiedDate,
                entry.CreatedBy,
                entry.CreatedDate,
                entry.DateOverview,
                entry.DeletedBy,
                entry.Deleted,
                entry.DeletedDate,
                entry.OpportunityTitle,
                entry.CustomerContract.id,
                entry.BuyerID,
                entry.IsDecisionGroup,
                entry.IsWritingStyleFormal,
                entry.Step2_Intention,
                entry.Step2_Achievement,
                entry.Step2_GivenReason,
                entry.Step2_SolutionAttribute,
                entry.Step2_ComplianceID,
                entry.CheckOutDate,
                entry.CheckOutPerson,
                entry.NextSteps,
                entry.NextStepsOther
            ]);
        } else {
            console.log('esb will make update');
            entry.id = s.rows.item(0).id;
            var esbChangePromise = $cordovaSQLite.execute(db, esbUpdateQuery, [
                entry.OpportunityReferences,
                entry.ESStatusID,
                entry.ModifiedBy,
                entry.ModifiedDate,
                entry.CreatedBy,
                entry.CreatedDate,
                entry.DateOverview,
                entry.DeletedBy,
                entry.Deleted,
                entry.DeletedDate,
                entry.OpportunityTitle,
                entry.CustomerContract.id,
                entry.BuyerID,
                entry.IsDecisionGroup,
                entry.IsWritingStyleFormal,
                entry.Step2_Intention,
                entry.Step2_Achievement,
                entry.Step2_GivenReason,
                entry.Step2_SolutionAttribute,
                entry.Step2_ComplianceID,
                entry.CheckOutDate,
                entry.CheckOutPerson,
                entry.NextSteps,
                entry.NextStepsOther,
                entry.ServerID
            ]);
        }
        q.resolve(esbChangePromise.then(
            function (s) {
                if (entry.id == null || entry.id == 0) {
                    console.log('saved id = ' + s.insertId);
                    entry.id = s.insertId;
                } else {
                    console.log('updated id = ' + entry.id)
                }
                var q = $q.defer();
                q.resolve($scope.SaveMasterProfile(entry));
                return q.promise;
            }, function (e) {
                app.Logger(e);
                $scope.hideDataDownload();
            }
        ));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterProfile = function (entry) {
    var q = $q.defer();
    var selectProfile = "select * from Profile where ServerID = ?";
    var profileInsertQuery = "insert or replace into Profile(ServerID,BuyerID,ExecutiveSummaryID) values(?,?,?)";
    var profileUpdateQuery = "update Profile set BuyerID = ?,ExecutiveSummaryID = ? where ServerID = ?";
    var profileSelectPromise = $cordovaSQLite.execute(db, selectProfile, [
        entry.ProfileContract.ServerID
    ]);
    console.log('selecting profile');
    q.resolve(profileSelectPromise.then(
        function (s) {
            console.log('profile select done');
            var q = $q.defer();
            if (s.rows.length <= 0) {
                console.log('profile will make instert');
                var profileChangePromise = $cordovaSQLite.execute(db, profileInsertQuery, [
                    entry.ProfileContract.ServerID,
                    entry.ProfileContract.BuyerID,
                    entry.id
                ]);
            } else {
                console.log('profile will make update');
                entry.ProfileContract.id = s.rows.item(0).id;
                var profileChangePromise = $cordovaSQLite.execute(db, profileUpdateQuery, [
                    entry.ProfileContract.BuyerID,
                    entry.id,
                    entry.ProfileContract.ServerID
                ]);
            }
            q.resolve(profileChangePromise.then(function (s) {
                var q = $q.defer();
                if (entry.ProfileContract.id == null) {
                    console.log('new id= ' + s.insertId)
                    entry.ProfileContract.id = s.insertId;
                } else {
                    console.log('updated id = ' + entry.ProfileContract.id)
                }
                q.resolve($scope.SaveMasterRequest(entry));
                return q.promise;
            }, function (e) {

            }));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }
    ));
    return q.promise;
};
$scope.SaveMasterRequest = function (entry) {
    var q = $q.defer();
    var selectRequest = "select * from ES_CreatedRequests where ServerID = ?";
    var requestInsertQuery = "INSERT or REPLACE INTO  ES_CreatedRequests ( ServerID, ExecutiveSummaryID, FilePath , Emails , Password , FormatType , CreatedBy , CreatedDate , TemplateID , RequestID , RequestDate , StatusID , CompletedDate ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
    var requestUpdateQuery = "update ES_CreatedRequests set ExecutiveSummaryID = ?, FilePath = ?, Emails  = ?, Password  = ?, FormatType  = ?, CreatedBy  = ?, CreatedDate  = ?, TemplateID  = ?, RequestID  = ?, RequestDate  = ?, StatusID  = ?, CompletedDate = ? where ServerID = ?";
    var requestSelectPromise = $cordovaSQLite.execute(db, selectRequest, [
        entry.CompletionContract.ServerID
    ]);
    console.log('select request');
    q.resolve(requestSelectPromise.then(function (s) {
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('insert request');
            var requestChangePromise = $cordovaSQLite.execute(db, requestInsertQuery, [
                entry.CompletionContract.ServerID,
                entry.id,
                entry.CompletionContract.FilePath,
                entry.CompletionContract.Emails,
                entry.CompletionContract.Password,
                entry.CompletionContract.FormatType,
                entry.CompletionContract.CreatedBy,
                entry.CompletionContract.CreatedDate,
                entry.CompletionContract.TemplateID,
                entry.CompletionContract.RequestID,
                entry.CompletionContract.RequestDate,
                entry.CompletionContract.StatusID,
                entry.CompletionContract.CompletedDate
            ]);
        } else {
            console.log('update request');
            entry.CompletionContract.id = s.rows.item(0).id;
            var requestChangePromise = $cordovaSQLite.execute(db, requestUpdateQuery, [
                entry.id,
                entry.CompletionContract.FilePath,
                entry.CompletionContract.Emails,
                entry.CompletionContract.Password,
                entry.CompletionContract.FormatType,
                entry.CompletionContract.CreatedBy,
                entry.CompletionContract.CreatedDate,
                entry.CompletionContract.TemplateID,
                entry.CompletionContract.RequestID,
                entry.CompletionContract.RequestDate,
                entry.CompletionContract.StatusID,
                entry.CompletionContract.CompletedDate,
                entry.CompletionContract.ServerID
            ]);
        }
        q.resolve(requestChangePromise.then(function (s) {
            var q = $q.defer();
            if (entry.CompletionContract.id == null) {
                console.log('new id= ' + s.insertId);
                entry.CompletionContract.id = s.insertId;
            } else {
                console.log('updated id = ' + entry.CompletionContract.id)
            }
            entry.IssuesContract.forEach(function (issue) {
                $scope.IssesPromises.push($scope.SaveMasterIssues(issue, entry.ProfileContract.id));
            });
            entry = null;
            console.info(entry);
            q.resolve($scope.SaveIssueRek($scope.IssesPromises.pop()));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterIssues = function (issue, profileId) {
    var q = $q.defer();
    q.resolve($q.all([$scope.SaveMasterImage(issue), $scope.SaveMasterQuote(issue), $scope.SaveMasterSupporting(issue)]).then(
        function (s) {
            var q = $q.defer();
            console.log('gonna save evidence')
            q.resolve($scope.SaveMasterEvidence(issue, profileId));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }
    ));
    return q.promise;
};
$scope.SaveMasterEvidence = function (issue, profileId) {
    var q = $q.defer();
    var evidenceSelectQuery = "select * from HB_Evidence where ServerID = ?";
    var evidenceInsertQuery = "INSERT or REPLACE INTO HB_Evidence(ServerID,ImageID,[Order],SupportingDocumetID,QuoteID,BuyerID) VALUES(?,?,?,?,?,?)";
    var evidenceUpdateQuery = "update HB_Evidence set ImageID = ?,[Order] = ?,SupportingDocumetID = ?,QuoteID = ?,BuyerID = ? where ServerID = ?";
    var evidenceSelectPromise = $cordovaSQLite.execute(db, evidenceSelectQuery, [
        issue.EvidenceContract.ServerID
    ]);
    q.resolve(evidenceSelectPromise.then(function (s) {
        var q = $q.defer();
        console.log('select evidence done');
        if (s.rows.length <= 0) {
            console.log('insert evidence');
            var evidencePromise = $cordovaSQLite.execute(db, evidenceInsertQuery, [
                issue.EvidenceContract.ServerID,
                issue.EvidenceContract.ImageLibaryContract.id,
                issue.EvidenceContract.Order,
                issue.EvidenceContract.SupportingDocumentContract.id,
                issue.EvidenceContract.QuoteContract.id,
                issue.EvidenceContract.BuyerID
            ]);
        } else {
            console.log('update evidence');
            issue.EvidenceContract.id = s.rows.item(0).id;
            var evidencePromise = $cordovaSQLite.execute(db, evidenceUpdateQuery, [
                issue.EvidenceContract.ImageLibaryContract.id,
                issue.EvidenceContract.Order,
                issue.EvidenceContract.SupportingDocumentContract.id,
                issue.EvidenceContract.QuoteContract.id,
                issue.EvidenceContract.BuyerID,
                issue.EvidenceContract.ServerID
            ]);
        }
        q.resolve(evidencePromise.then(function (s) {
            var q = $q.defer();
            if (issue.EvidenceContract.id == null) {
                console.log('new id= ' + s.insertId);
                issue.EvidenceContract.id = s.insertId;
            } else {
                console.log('update id = ' + issue.EvidenceContract.id)
            }
            q.resolve($scope.SaveMasterHotButton(issue, profileId));
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterHotButton = function (issue, profileId) {
    var q = $q.defer();
    var hblSelectQuery = "select * from HotButtonList where ServerID  = ?";
    var hblInsertQuery = "INSERT or REPLACE INTO HotButtonList(" +
        "ServerID,EvidenceID,Title,CreatedByID,ModifiedByID,ProfileID," +
        "CreatedDate,ModifiedDate,DeletedBy,Deleted,DeletedDate,Eng_MajorIssue," +
        "Step1_MajorIssue,Step1_PrimaryCause,Step1_Capability,Step1_ValueProposition," +
        "Step2_ImpactAreaID,Step2_ImpactAmountID,Step2_ImpactHowMuch,Step3_IsUnique," +
        "Step3_DescribeApproach,Step3_Diffrence,Step3_Advantage,Step4_EvidenceDeliveredBefore," +
        "Step1_WhatFeature,Step1_HowFeature,SummariesSelected) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    var hblUpdateQuery = "update HotButtonList" +
        " set EvidenceID = ?,Title = ?,CreatedByID = ?,ModifiedByID = ?,ProfileID = ?," +
        "CreatedDate = ?,ModifiedDate = ?,DeletedBy = ?,Deleted = ?,DeletedDate = ?,Eng_MajorIssue = ?," +
        "Step1_MajorIssue = ?,Step1_PrimaryCause = ?,Step1_Capability = ?,Step1_ValueProposition = ?," +
        "Step2_ImpactAreaID = ?,Step2_ImpactAmountID = ?,Step2_ImpactHowMuch = ?,Step3_IsUnique = ?," +
        "Step3_DescribeApproach = ?,Step3_Diffrence = ?,Step3_Advantage = ?,Step4_EvidenceDeliveredBefore = ?," +
        "Step1_WhatFeature = ?,Step1_HowFeature = ?,SummariesSelected = ? where ServerID = ?";
    var hblSelectPromise = $cordovaSQLite.execute(db, hblSelectQuery, [
        issue.ServerID
    ]);
    q.resolve(hblSelectPromise.then(function (s) {
        console.log('select hbl done');
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('insert hbl');
            var hblQueryPromise = $cordovaSQLite.execute(db, hblInsertQuery, [
                issue.ServerID,
                issue.EvidenceContract.id,
                issue.Title,
                issue.CreatedByID,
                issue.ModifiedByID,
                profileId,
                issue.CreatedDate,
                issue.ModifiedDate,
                issue.DeletedBy,
                issue.Deleted,
                issue.DeletedDate,
                issue.Eng_MajorIssue,
                issue.Step1_MajorIssue,
                issue.Step1_PrimaryCause,
                issue.Step1_Capability,
                issue.Step1_ValueProposition,
                issue.Step2_ImpactAreaID,
                issue.Step2_ImpactAmountID,
                issue.Step2_ImpactHowMuch,
                issue.Step3_IsUnique,
                issue.Step3_DescribeApproach,
                issue.Step3_Diffrence,
                issue.Step3_Advantage,
                issue.Step4_EvidenceDeliveredBefore,
                issue.Step1_WhatFeature,
                issue.Step1_HowFeature,
                issue.SummariesSelected
            ]);
        } else {
            console.log('update hbl');
            issue.id = s.rows.item(0).id;
            var hblQueryPromise = $cordovaSQLite.execute(db, hblUpdateQuery, [
                issue.EvidenceContract.id,
                issue.Title,
                issue.CreatedByID,
                issue.ModifiedByID,
                profileId,
                issue.CreatedDate,
                issue.ModifiedDate,
                issue.DeletedBy,
                issue.Deleted,
                issue.DeletedDate,
                issue.Eng_MajorIssue,
                issue.Step1_MajorIssue,
                issue.Step1_PrimaryCause,
                issue.Step1_Capability,
                issue.Step1_ValueProposition,
                issue.Step2_ImpactAreaID,
                issue.Step2_ImpactAmountID,
                issue.Step2_ImpactHowMuch,
                issue.Step3_IsUnique,
                issue.Step3_DescribeApproach,
                issue.Step3_Diffrence,
                issue.Step3_Advantage,
                issue.Step4_EvidenceDeliveredBefore,
                issue.Step1_WhatFeature,
                issue.Step1_HowFeature,
                issue.SummariesSelected,
                issue.ServerID
            ]);
        }
        q.resolve(hblQueryPromise.then(function (s) {
            var q = $q.defer();
            q.resolve(true);
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        issue = null;
        console.info(issue);
        return q.promise;
    }));
    return q.promise;
};
$scope.SaveMasterImage = function (issue) {
    var q = $q.defer();
    var selectImage = "select * from HB_ImageLibrary where ServerID = ?";
    var imageInsertQuery = "insert or replace into HB_ImageLibrary(ServerID,Image,Title,Conclusion) VALUES(?,?,?,?)";
    var imageUpdateQuery = "update HB_ImageLibrary set Image = ?,Title = ?,Conclusion = ? where ServerID = ?";
    var imagePromise = $cordovaSQLite.execute(db, selectImage, [
        issue.EvidenceContract.ImageLibaryContract.ServerID
    ]);
    q.resolve(imagePromise.then(function (s) {
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('insert image');
            var imageChangePromise = $cordovaSQLite.execute(db, imageInsertQuery, [
                issue.EvidenceContract.ImageLibaryContract.ServerID,
                issue.EvidenceContract.ImageLibaryContract.Image,
                issue.EvidenceContract.ImageLibaryContract.Title,
                issue.EvidenceContract.ImageLibaryContract.Conclusion
            ]);
        } else {
            console.log('update image');
            issue.EvidenceContract.ImageLibaryContract.id = s.rows.item(0).id;
            var imageChangePromise = $cordovaSQLite.execute(db, imageUpdateQuery, [
                issue.EvidenceContract.ImageLibaryContract.Image,
                issue.EvidenceContract.ImageLibaryContract.Title,
                issue.EvidenceContract.ImageLibaryContract.Conclusion,
                issue.EvidenceContract.ImageLibaryContract.ServerID
            ]);
        }
        q.resolve(imageChangePromise.then(function (s) {
            var q = $q.defer();
            if (issue.EvidenceContract.ImageLibaryContract.id == null) {
                console.log('insert id = ' + s.insertId);
                issue.EvidenceContract.ImageLibaryContract.id = s.insertId;
            }
            q.resolve(true);
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterQuote = function (issue) {
    var q = $q.defer();
    var selectQuote = "select * from HB_Quote where ServerID = ?";
    var quoteInsertQuery = "insert or replace into HB_Quote(ServerID,QuoteText,QuoteLegend) VALUES (?,?,?)";
    var quoteUpdateQuery = "update HB_Quote set QuoteText = ?,QuoteLegend = ? where ServerID = ?";
    var quotePromise = $cordovaSQLite.execute(db, selectQuote, [
        issue.EvidenceContract.QuoteContract.ServerID
    ]);
    q.resolve(quotePromise.then(function (s) {
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('insert quote');
            var quoteChangePromise = $cordovaSQLite.execute(db, quoteInsertQuery, [
                issue.EvidenceContract.QuoteContract.ServerID,
                issue.EvidenceContract.QuoteContract.QuoteText,
                issue.EvidenceContract.QuoteContract.QuoteLegend
            ]);
        } else {
            console.log('update quote');
            issue.EvidenceContract.QuoteContract.id = s.rows.item(0).id;
            var quoteChangePromise = $cordovaSQLite.execute(db, quoteUpdateQuery, [
                issue.EvidenceContract.QuoteContract.QuoteText,
                issue.EvidenceContract.QuoteContract.QuoteLegend,
                issue.EvidenceContract.QuoteContract.ServerID
            ]);
        }
        q.resolve(quoteChangePromise.then(function (s) {
            var q = $q.defer();
            if (issue.EvidenceContract.QuoteContract.id == null) {
                console.log('insert id = ' + s.insertId);
                issue.EvidenceContract.QuoteContract.id = s.insertId;
            }
            q.resolve(true);
            return q.promise;
        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveMasterSupporting = function (issue) {
    var q = $q.defer();
    var selectSuport = "select * from HB_SuportingDocument where ServerID = ?";
    var suportInsertQuery = "insert or replace into HB_SuportingDocument(ServerID,Extract,Title,Link)VALUES(?,?,?,?)";
    var suportUpdateQuery = "update HB_SuportingDocument set Extract = ?,Title = ?,Link = ? where ServerID = ?";
    var supportPromise = $cordovaSQLite.execute(db, selectSuport, [
        issue.EvidenceContract.SupportingDocumentContract.ServerID
    ]);
    q.resolve(supportPromise.then(function (s) {
        var q = $q.defer();
        if (s.rows.length <= 0) {
            console.log('insert supporting');
            var suportChangePromise = $cordovaSQLite.execute(db, suportInsertQuery, [
                issue.EvidenceContract.SupportingDocumentContract.ServerID,
                issue.EvidenceContract.SupportingDocumentContract.Extract,
                issue.EvidenceContract.SupportingDocumentContract.Title,
                issue.EvidenceContract.SupportingDocumentContract.Link
            ]);
        } else {
            console.log('update supporting');
            issue.EvidenceContract.SupportingDocumentContract.id = s.rows.item(0).id;
            var suportChangePromise = $cordovaSQLite.execute(db, suportUpdateQuery, [
                issue.EvidenceContract.SupportingDocumentContract.Extract,
                issue.EvidenceContract.SupportingDocumentContract.Title,
                issue.EvidenceContract.SupportingDocumentContract.Link,
                issue.EvidenceContract.SupportingDocumentContract.ServerID
            ]);
        }
        q.resolve(suportChangePromise.then(function (s) {
            var q = $q.defer();
            if (issue.EvidenceContract.SupportingDocumentContract.id == null) {
                console.log('insert id = ' + s.insertId);
                issue.EvidenceContract.SupportingDocumentContract.id = s.insertId;
            }
            q.resolve(true);
            return q.promise;

        }, function (e) {
            app.Logger(e);
            $scope.hideDataDownload();
        }));
        return q.promise;
    }, function (e) {
        app.Logger(e);
        $scope.hideDataDownload();
    }));
    return q.promise;
};
$scope.SaveIssueRek = function (element) {
    var q = $q.defer();
    $scope.IssueToSave = [];
    $scope.IssueToSave.push(element);
    if ($scope.IssesPromises.length == 0) {
        app.Logger('taking last issue');
        q.resolve(true);
    } else {
        $q.all($scope.IssueToSave).then(function (s) {
            app.Logger('taking next issue');
            q.resolve($scope.SaveIssueRek($scope.IssesPromises.pop()));
        }, function (e) {
        });
    }
    return q.promise;
};

and $scope.EsbContractsRek = d.EsbContracts; are contracts from service from restapi :stuck_out_tongue:


#4

I think you’re best solution is to break that up into multiple steps and checkpoints. the code looks pretty messy and I think you’d get a better debugging environment and the memory would probably play nicer. Try re-writing that in maybe 3 steps to see if you can make the memory manage better.

It’s coming down to code efficiency vs “how do I get around these limitations”