Ios 初始化推送通知时,ionic发生奇怪的内存泄漏,导致冻结
几天以来,我面临着一些非常奇怪的记忆问题 问题是,应用程序在本地被卡住,并开始快速增加内存使用量,直到崩溃。当内存增加时,应用程序完全冻结 经过一些调试后,我发现这段代码导致了错误:Ios 初始化推送通知时,ionic发生奇怪的内存泄漏,导致冻结,ios,angularjs,cordova,memory-leaks,ionic-framework,Ios,Angularjs,Cordova,Memory Leaks,Ionic Framework,几天以来,我面临着一些非常奇怪的记忆问题 问题是,应用程序在本地被卡住,并开始快速增加内存使用量,直到崩溃。当内存增加时,应用程序完全冻结 经过一些调试后,我发现这段代码导致了错误: angular.module('app.shared').factory('PushNotificationService', PushNotificationService); PushNotificationService.$inject = [ '$q', 'Messagin
angular.module('app.shared').factory('PushNotificationService', PushNotificationService);
PushNotificationService.$inject = [
'$q',
'MessagingService'
];
function PushNotificationService($q, MessagingService) {
var me = this;
initialize();
return {
getStartupMessage: getStartupMessage,
fetchToken: fetchToken
};
/**
* Constructor
* @return {[type]} [description]
*/
function initialize() {
me.pusher = null;
me.deviceToken = null;
me.startupMessage = null;
}
/**
* Fetches the push token through device interface
*
* @return {$q} Promises
*/
function fetchToken() {
if (me.pusher != null) {
return $q(function(resolve, reject) {
console.log('PushService.fetchToken(): Got pusher', me.pusher);
// when pusher was already initialized, we do not need to fetch it again
console.log('PushService.fetchToken(): Token was already retrieved', me.deviceToken);
resolve(me.deviceToken);
});
}
console.log('PushService.fetchToken(): Needs to fetch token bc not retrieved yet');
return $q(function(resolve, reject) {
document.addEventListener('deviceready', function() {
console.log('PushService.fetchToken(): Device is ready', typeof resolve, typeof reject);
resolve(true);
}, false);
}).then(function() {
console.log('PushService.fetchToken(): No pusher retrieved yet, do it now');
return __initialize();
}).then(function(push) {
console.log('PushService.fetchToken(): Got pusher and start attaching events to it', push);
return $q(function(resolve, reject) {
push.on('error', function(error) {
console.log('PushNotificationService.fetchToken(): Error while retrieving token', error.message, error);
reject(error);
});
push.on('registration', function(data) {
console.log('PushNotificationService.fetchToken(): Successfully retrieved token', data);
me.deviceToken = data.registrationId;
resolve(data.registrationId);
});
console.log('PushNotificationService.fetchToken(): Eventhandlers of pusher', push._handlers);
});
});
}
/**
* Initializes the push notifications and tries to fetch the Push token
* @return {Cordova.Pusher} [description]
*/
function __initialize() {
me.pusher = PushNotification.init({
android: {
senderID: "288503736094"
},
ios: {
alert: true,
badge: true,
sound: true,
clearBadge: true
},
windows: {}
});
me.pusher.on('notification', __incomingNotification);
return me.pusher;
}
// additional code which is not relevant here...
// .....
// .....
}
它只发生在iOS上,完全是随机的,在崩溃中找不到系统
调试日志如下所示:
PushService.fetchToken(): Needs to fetch token bc not retrieved yet
PushService.fetchToken(): Device is ready function function
PushService.fetchToken(): No pusher retrieved yet, do it now
PushService.fetchToken(): Got pusher and start attaching events to it {"_handlers":{"registration":[],"notification":[null],"error":[]},"options":{"android":{"senderID":"288503736094"},"ios":{"alert":true,"badge":true,"sound":true,"clearBadge":true},"windows":{}}}
PushNotificationService.fetchToken(): Eventhandlers of pusher {"registration":[null],"notification":[null],"error":[null]}
这是cordova ios中的一个问题,导致随机位置出现大量内存泄漏 升级到最新版本修复了它