AWS SNS取消订阅循环未释放内存

问题

当我退订我的终端订阅时,乐器内存图从32MB变为300MB,应用程序被杀死Jetsam。 据我的理解,AWS BFTasks自行清理......如果我删除了do...while并允许它处理前100个订阅,那么所有工作都可以正常工作。

处理

我的开发应用程序中约有8000个订阅,它使用AWS SNS将警报推送到iOS设备和电子邮件。 在用户选择他们想要的订阅后,我删除他们现有的订阅,然后订阅他们到新的订阅。 [_awsSnsClient listSubscriptions:request]返回最多100个订阅,我循环浏览这些[_awsSnsClient unsubscribe:unsubscribeRequest] ,根据需要执行[_awsSnsClient unsubscribe:unsubscribeRequest] 。 然后获得下一个100 ...

- (void) awsUsubscribeAllSubscriptions {
    DLog(@"Removing existing subscriptions");

    AWSSNSListSubscriptionsInput *request = [AWSSNSListSubscriptionsInput new];

    __block Boolean sentLastUnsubscribe = NO; // YES when the last unsubscribe request was sent
    __block int pendingUnsubscribe = 0; // Number of outsianding unsubscribes (async process not yet complete)

    do {
        [[[_awsSnsClient listSubscriptions:request] continueWithSuccessBlock:^id(BFTask *task) {
            AWSSNSListSubscriptionsResponse *response = (AWSSNSListSubscriptionsResponse *)task.result;
            NSString *nextToken = response.nextToken;
            NSArray *subscriptions = response.subscriptions;
            for (AWSSNSSubscription *subscription in subscriptions) {
                if ([subscription.endpoint isEqualToString:_awsPlatformEndpoint.endpointArn]) {
                    AWSSNSUnsubscribeInput *unsubscribeRequest = [AWSSNSUnsubscribeInput new];
                    unsubscribeRequest.subscriptionArn = subscription.subscriptionArn;
                    pendingUnsubscribe++;
                    [[[_awsSnsClient unsubscribe:unsubscribeRequest] continueWithSuccessBlock:^id(BFTask *task) {
                        DLog(@"Unsubscribed from:%@",subscription.subscriptionArn);
                        return nil;
                    }] continueWithBlock:^id(BFTask *task) {
                        pendingUnsubscribe--;
                        if (task.error) {
                            // failed with error
                            ALog(@"Error unsubscribing to: %@, %@, %@", subscription, [task.error localizedDescription], [task.error localizedFailureReason]);
                        }
                        // If we have processed the last unsubscribe, then create topics and subscribe
                        if (sentLastUnsubscribe  && (pendingUnsubscribe <= 0)) {
                            [self awsCreateTopicsAndSubscriptions];
                        }
                        return nil;
                    }];
                }
            }
            request.nextToken = nextToken;
            if(!nextToken) sentLastUnsubscribe = YES; // Reached the end of the SNS subscriptions
            return nil;
        }] continueWithBlock:^id(BFTask *task) {
            if (task.error) {
                // failed with error
                ALog(@"Error listing subscriptions: %@, %@", [task.error localizedDescription], [task.error localizedFailureReason]);
            }
            return nil;
        }];
    } while (!sentLastUnsubscribe);
}

是否有更好的方法从端点中删除所有SNS订阅? 在这个循环中,有没有办法让强制释放任何保留的数据?

使用递归和- waitUntilFinished 。 内存负载现在低得多。

-(void) awsUnsubscribeFromAllSubscriptionsWithNextToken:(NSString *)nextToken AndSubscribe:(BOOL)subscribe {
AWSSNSListSubscriptionsInput *request = [AWSSNSListSubscriptionsInput new];
request.nextToken = nextToken;
[[[_awsSnsClient listSubscriptions:request] continueWithSuccessBlock:^id(BFTask *task) {
    AWSSNSListSubscriptionsResponse *response = (AWSSNSListSubscriptionsResponse *)task.result;
    NSArray *subscriptions = response.subscriptions;
    for (AWSSNSSubscription *subscription in subscriptions) {
        if ([subscription.endpoint isEqualToString:_awsPlatformEndpoint.endpointArn]) {
            DLog(@"Unsubscribe from %@", subscription.topicArn);
            AWSSNSUnsubscribeInput *unsubscribeRequest = [AWSSNSUnsubscribeInput new];
            unsubscribeRequest.subscriptionArn = subscription.subscriptionArn;
            [[_awsSnsClient unsubscribe:unsubscribeRequest] waitUntilFinished];
        }
    }
    if(response.nextToken) {
        [self awsUnsubscribeFromAllSubscriptionsWithNextToken:response.nextToken AndSubscribe:subscribe];
    } else if (subscribe) {
        [self awsCreateTopicsAndSubscriptions];
    }
    return nil;
}] continueWithBlock:^id(BFTask *task) {
    if (task.error) {
        // failed with error
        ALog(@"Error listing subscriptions: %@, %@", [task.error localizedDescription], [task.error localizedFailureReason]);
    }
    return nil;
}];

}


请注意- listSubscriptions:是一个异步方法,并立即返回。 您正在for循环中调用异步方法。 这意味着你可能会在短时间内呼叫- listSubscriptions:数百甚至数千次。

你可以做的最简单的事情就是调用- waitUntilFinished async块末尾的- waitUntilFinished 。 它使整个方法同步

但是,一般来说,您应该尽可能避免调用- waitUntilFinishedAWSKinesisRecorderTests.m具有递归调用- getRecords:的功能。 你可以采用类似的模式。

链接地址: http://www.djcxy.com/p/32209.html

上一篇: AWS SNS Unsubscribe loop is not releasing memory

下一篇: How to unsubscribe an iOS Device from an amazon SNS topic?