Skip to content

Commit

Permalink
Attempt to fix AIM crash and main thread issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Jan 17, 2014
1 parent 0627e09 commit 682f4b9
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions Off the Record/OTROscarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ -(OTRBuddyStatus)convertAimStatus:(AIMBuddyStatus *)status
-(OTRManagedBuddy *)updateManagedBuddyWith:(AIMBlistBuddy *)buddy
{
OTRBuddyStatus buddyStatus = [self convertAimStatus:buddy.status];
OTRManagedAccount *localAccount = [self.account MR_inThreadContext];

OTRManagedBuddy *otrBuddy = [OTRManagedBuddy fetchOrCreateWithName:buddy.username account:self.account];

Expand All @@ -102,17 +103,19 @@ -(OTRManagedBuddy *)updateManagedBuddyWith:(AIMBlistBuddy *)buddy
otrBuddy.photo = photo;

[otrBuddy addToGroup:buddy.group.name];
otrBuddy.account = self.account;
otrBuddy.account = localAccount;
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];
[context MR_saveToPersistentStoreAndWait];
return otrBuddy;
}

-(void)updateMangedBuddyWith:(AIMBlistBuddy *)buddy withStatus:(AIMBuddyStatus *)status
{
OTRBuddyStatus buddyStatus = [self convertAimStatus:status];
OTRManagedBuddy *otrBuddy = [self updateManagedBuddyWith:buddy];
[otrBuddy newStatusMessage:status.statusMessage status:buddyStatus incoming:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
OTRBuddyStatus buddyStatus = [self convertAimStatus:status];
OTRManagedBuddy *otrBuddy = [self updateManagedBuddyWith:buddy];
[otrBuddy newStatusMessage:status.statusMessage status:buddyStatus incoming:YES];
});
}


Expand Down Expand Up @@ -198,13 +201,15 @@ - (void)aimFeedbagHandlerGotBuddyList:(AIMFeedbagHandler *)feedbagHandler {

aimBuddyList = [theSession.session buddyList];

for(AIMBlistGroup *group in aimBuddyList.groups)
{
for(AIMBlistBuddy *buddy in group.buddies)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
for(AIMBlistGroup *group in aimBuddyList.groups)
{
[self updateManagedBuddyWith:buddy];
for(AIMBlistBuddy *buddy in group.buddies)
{
[self updateManagedBuddyWith:buddy];
}
}
}
});
}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyAdded:(AIMBlistBuddy *)newBuddy {
Expand All @@ -223,10 +228,6 @@ - (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyDeleted:(AIMBlistBudd

NSManagedObjectContext * context = [NSManagedObjectContext MR_contextForCurrentThread];
[context MR_saveToPersistentStoreAndWait];

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];
}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender groupAdded:(AIMBlistGroup *)newGroup {
Expand All @@ -237,51 +238,35 @@ - (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender groupAdded:(AIMBlistGroup
- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender groupDeleted:(AIMBlistGroup *)oldGroup {
[self checkThreading];
//DDLogInfo(@"Group removed: %@", [oldGroup name]);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];
}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender groupRenamed:(AIMBlistGroup *)theGroup {
[self checkThreading];
//DDLogInfo(@"Group renamed: %@", [theGroup name]);
//DDLogInfo(@"Blist: %@", theSession.session.buddyList);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];
}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyDenied:(NSString *)username {
//DDLogInfo(@"User blocked: %@", username);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];

}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyPermitted:(NSString *)username {
//DDLogInfo(@"User permitted: %@", username);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];

}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyUndenied:(NSString *)username {
//DDLogInfo(@"User un-blocked: %@", username);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];

}
- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender buddyUnpermitted:(NSString *)username {
//DDLogInfo(@"User un-permitted: %@", username);

[[NSNotificationCenter defaultCenter]
postNotificationName:kOTRBuddyListUpdate
object:self];
}

- (void)aimFeedbagHandler:(AIMFeedbagHandler *)sender transactionFailed:(id<FeedbagTransaction>)transaction {
Expand All @@ -303,14 +288,11 @@ - (void)aimICBMHandler:(AIMICBMHandler *)sender gotMessage:(AIMMessage *)message

OTRManagedMessage *otrMessage = [OTRManagedMessage newMessageFromBuddy:messageBuddy message:msgTxt encrypted:YES delayedDate:nil];

[OTRCodec decodeMessage:otrMessage];
[OTRCodec decodeMessage:otrMessage completionBlock:^(OTRManagedMessage *message) {
[OTRManagedMessage showLocalNotificationForMessage:message];
}];

if(otrMessage && otrMessage.isEncryptedValue == NO)
{
[messageBuddy receiveMessage:otrMessage.message];

}


NSArray * tokens = [CommandTokenizer tokensOfCommand:msgTxt];
if ([tokens count] == 1) {
if ([[tokens objectAtIndex:0] isEqual:@"blist"]) {
Expand Down

0 comments on commit 682f4b9

Please sign in to comment.