Skip to content

Commit

Permalink
Capture strong reference to self in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 committed Nov 10, 2023
1 parent 565e4d1 commit 5d710a6
Showing 1 changed file with 27 additions and 48 deletions.
75 changes: 27 additions & 48 deletions mParticle-Apple-SDK/MPBackendController.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,47 +273,29 @@ - (void)confirmEndSessionMessage:(MPSession *)session {
}

- (void)broadcastSessionDidBegin:(MPSession *)session {
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate sessionDidBegin:session];
});

__weak MPBackendController *weakSelf = self;
MParticleSession *mparticleSession = [[MParticleSession alloc] initWithUUID:session.uuid];
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf) {
NSMutableDictionary *mutableInfo = [NSMutableDictionary dictionary];
if (mparticleSession.sessionID != nil) {
mutableInfo[mParticleSessionId] = mparticleSession.sessionID;
}
if (mparticleSession.UUID) {
mutableInfo[mParticleSessionUUID] = mparticleSession.UUID;
}
[[NSNotificationCenter defaultCenter] postNotificationName:mParticleSessionDidBeginNotification
object:weakSelf.delegate
userInfo:[mutableInfo copy]];
}
});
[MParticle executeOnMain:^{
[self.delegate sessionDidBegin:session];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[mParticleSessionId] = mparticleSession.sessionID;
userInfo[mParticleSessionUUID] = mparticleSession.UUID;
[[NSNotificationCenter defaultCenter] postNotificationName:mParticleSessionDidBeginNotification
object:self.delegate
userInfo:userInfo];
}];
}

- (void)broadcastSessionDidEnd:(MPSession *)session {
[self.delegate sessionDidEnd:session];

__weak MPBackendController *weakSelf = self;
MParticleSession *mparticleSession = [[MParticleSession alloc] initWithUUID:session.uuid];
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf) {
NSMutableDictionary *mutableInfo = [NSMutableDictionary dictionary];
if (mparticleSession.sessionID != nil) {
mutableInfo[mParticleSessionId] = mparticleSession.sessionID;
}
if (mparticleSession.UUID) {
mutableInfo[mParticleSessionUUID] = mparticleSession.UUID;
}
[[NSNotificationCenter defaultCenter] postNotificationName:mParticleSessionDidEndNotification
object:weakSelf.delegate
userInfo:[mutableInfo copy]];
}
});
[MParticle executeOnMain:^{
[self.delegate sessionDidEnd:session];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[mParticleSessionId] = mparticleSession.sessionID;
userInfo[mParticleSessionUUID] = mparticleSession.UUID;
[[NSNotificationCenter defaultCenter] postNotificationName:mParticleSessionDidEndNotification
object:self.delegate
userInfo:userInfo];
}];
}

- (void)logUserAttributeChange:(MPUserAttributeChange *)userAttributeChange {
Expand Down Expand Up @@ -816,9 +798,8 @@ - (void)beginUploadTimer {
self.uploadSource = nil;
}

__weak MPBackendController *weakSelf = self;
self.uploadSource = [self createSourceTimer:self.uploadInterval eventHandler:^{
[weakSelf waitForKitsAndUploadWithCompletionHandler:nil];
[self waitForKitsAndUploadWithCompletionHandler:nil];
} cancelHandler:^{}];
}
}
Expand Down Expand Up @@ -1544,7 +1525,6 @@ - (void)startWithKey:(NSString *)apiKey secret:(NSString *)secret firstRun:(BOOL
date = [NSDate date];
}

__weak MPBackendController *weakSelf = self;
dispatch_async([MParticle messageQueue], ^{
[MParticle sharedInstance].persistenceController = [[MPPersistenceController alloc] init];

Expand All @@ -1554,25 +1534,25 @@ - (void)startWithKey:(NSString *)apiKey secret:(NSString *)secret firstRun:(BOOL

MPMessageBuilder *messageBuilder = [MPMessageBuilder newBuilderWithMessageType:MPMessageTypeFirstRun session:self.session messageInfo:nil];

[weakSelf processOpenSessionsEndingCurrent:NO completionHandler:^(void) {}];
[weakSelf waitForKitsAndUploadWithCompletionHandler:nil];
[self processOpenSessionsEndingCurrent:NO completionHandler:^(void) {}];
[self waitForKitsAndUploadWithCompletionHandler:nil];

[weakSelf beginUploadTimer];
[self beginUploadTimer];

if (firstRun) {
MPMessage *message = [messageBuilder build];
message.uploadStatus = MPUploadStatusBatch;

[weakSelf saveMessage:message updateSession:YES];
[self saveMessage:message updateSession:YES];

MPILogDebug(@"Application First Run");
}

void (^searchAdsCompletion)(void) = ^{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[weakSelf processDidFinishLaunching:weakSelf.didFinishLaunchingNotification];
[weakSelf waitForKitsAndUploadWithCompletionHandler:nil];
[self processDidFinishLaunching:self.didFinishLaunchingNotification];
[self waitForKitsAndUploadWithCompletionHandler:nil];
});
};

Expand All @@ -1583,7 +1563,7 @@ - (void)startWithKey:(NSString *)apiKey secret:(NSString *)secret firstRun:(BOOL
searchAdsCompletion();
}

[weakSelf processPendingArchivedMessages];
[self processPendingArchivedMessages];

[MPResponseConfig restore];
[self requestConfig:nil];
Expand Down Expand Up @@ -1670,7 +1650,6 @@ - (MPExecStatus)waitForKitsAndUploadWithCompletionHandler:(void (^ _Nullable)(vo
}

- (MPExecStatus)checkForKitsAndUploadWithCompletionHandler:(void (^ _Nullable)(BOOL didShortCircuit))completionHandler {
__weak MPBackendController *weakSelf = self;
[self requestConfig:^(BOOL uploadBatch) {
if (!uploadBatch) {
if (completionHandler) {
Expand All @@ -1689,7 +1668,7 @@ - (MPExecStatus)checkForKitsAndUploadWithCompletionHandler:(void (^ _Nullable)(B
return;
}

[weakSelf uploadBatchesWithCompletionHandler:^(BOOL success) {
[self uploadBatchesWithCompletionHandler:^(BOOL success) {
if (completionHandler) {
completionHandler(NO);
}
Expand Down

0 comments on commit 5d710a6

Please sign in to comment.