Skip to content

Instantly share code, notes, and snippets.

@soulfly
Created February 3, 2015 12:34
Show Gist options
  • Save soulfly/8bcbf0f7a054e669f7d7 to your computer and use it in GitHub Desktop.
Save soulfly/8bcbf0f7a054e669f7d7 to your computer and use it in GitHub Desktop.
4
-(void)completedWithResult:(Result *)result context:(void *)contextInfo
{
NSMutableDictionary *dict = (__bridge NSMutableDictionary *)contextInfo;
ChatMessage *message = dict[@"message"];
QBUUser *user = [[DataManager shared] currentUser];
self.isProcessing = NO;
if([result isKindOfClass:QBCOCustomObjectResult.class])
{
if(result.success){
QBCOCustomObjectResult *objectResult = (QBCOCustomObjectResult *)result;
[self.context refreshObject:message mergeChanges:YES];
message.remoteId = objectResult.object.ID;
[[DataManager shared] updateSyncDateWithTimeStamp:objectResult.object.createdAt.timeIntervalSince1970];
NSError *error;
if (![self.context save:&error])
{
NSLog(@"Could not save Chat Message: %@", [error localizedDescription]);
}
//now if the read flag is different from the remote object then update it
if (![[objectResult.object.fields valueForKey:@"Read"] isEqualToString:@"true"] && [message.read integerValue] == 1)
{
[objectResult.object.fields setObject:message.read forKey:@"Read"];
[QBCustomObjects updateObject:objectResult.object delegate:self];
}
if ([message.recipientId integerValue] != user.ID)
{
//NSLog(@"\n\n\n\n\n\n\n completedWithResult \n\n\n\n\n %@ -> message.datetime : %f \n\n\n\n\n %@ -> objectResult.object.fields[@\"DateTime\"] : %@ \n\n\n\n\n\n\n\n", message.text, [message.dateTime timeIntervalSince1970], message.text, objectResult.object.fields[@"DateTime"]);
QBChatMessage *qbChatMsg = [Util makeQBChatMessageFrom:message];
// [self callQuickbloxSendMessageAPIFromMessageQueueManagerWithMessage:qbChatMsg andResultObject:objectResult];
[self callQuickbloxSendMessageAPIFromMessageQueueManagerWithQBMessage:qbChatMsg ChatMessageObject:message andResultObject:objectResult];
/* if ([queuedChatMessagesArray containsObject:objectResult.object])
{
[queuedChatMessagesArray removeObject:objectResult.object];
}
*/
}
else
{
//Set a notification that the messages have been updated for opponent
[[NSNotificationCenter defaultCenter] postNotificationName:@"MessagesUpdated"
object:[NSNumber numberWithInteger:message.opponentId.integerValue]];
}
// Failed
}else{
NSLog(@"CO_FAIL: dict: %@", dict);
[self queueFailed:dict message:message maxRetry:3];
}
}
else if ([[message class] isSubclassOfClass:[ChatMessage class]] && result.success == NO)
{
[self queueFailed:dict message:message maxRetry:MAX_NO_OF_TRY];
}
}
- (void)queueFailed:(NSDictionary *)dict message:(ChatMessage *)message maxRetry:(int)maxRetry{
// failure case
NSString *retry_str = dict[@"retrycount"];
NSInteger retryCount = [retry_str integerValue];
NSLog(@"retryCount: %ld", (long)retryCount);
if (retryCount < maxRetry)
{
[self addRemoteMessage:message WithRetryCount:retry_str];
}
else
{
QBChatMessage *msg = [Util makeQBChatMessageWithAsItisFieldsFrom:message];
[self.delegate messageFailedToReachUsrMessagesTable:msg withFileData:nil];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment