-
-
Save w-i-n-s/b5684c470e877af75880 to your computer and use it in GitHub Desktop.
MailCore tip1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)updateAttachmentsPresenceForMessages:(NSArray*)newMessages forAccount:(Account*)account{ | |
NSManagedObjectContext *moc = [self getContextForBGTask]; | |
[moc performBlock:^{ | |
Account *__account = (Account*)[moc objectWithID:[account objectID]]; | |
// get old messages | |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"mcUid" | |
ascending:YES]; | |
NSPredicate *predicate = [NSPredicate predicateWithFormat: | |
@"isDownloadBody == %@ AND \ | |
visible == %@ AND\ | |
account == %@",@(YES),@(YES),__account]; | |
NSArray *allMessages = [self getObjectsOfType:entity_name_Message | |
withSortDescriptors:@[sortDescriptor] | |
andPredicate:predicate | |
inContext:moc]; | |
NSArray *oldMessages = nil; | |
NSPredicate *uidPredicate; | |
Message *theMessage = nil; | |
NSArray *attachments = nil; | |
NSArray *inlineAttachments = nil; | |
NSString *mimeType = nil; | |
Attachment *attach = nil; | |
NMAttachmentType attType = 0; | |
NSPredicate *partIDPredicate; | |
NSSet *twinSet; | |
for (MCOIMAPMessage *message in newMessages) { | |
attachments = [message attachments]; | |
inlineAttachments = [message htmlInlineAttachments]; | |
if ([attachments count] || | |
[inlineAttachments count]) { | |
int uid = [message uid]; | |
uidPredicate = [NSPredicate predicateWithFormat:@"mcUid == %@",@(uid)]; | |
oldMessages = [allMessages filteredArrayUsingPredicate:uidPredicate]; | |
if ([oldMessages count]) { | |
theMessage = [oldMessages objectAtIndex:0]; | |
NSMutableArray *allAttachments = [[NSMutableArray alloc] init]; | |
[allAttachments addObjectsFromArray:attachments]; | |
[allAttachments addObjectsFromArray:inlineAttachments]; | |
for (MCOIMAPPart *part in allAttachments) { | |
partIDPredicate = [NSPredicate predicateWithFormat:@"mcPartID == %@",[part partID]]; | |
twinSet = [(NSSet*)[theMessage attachments] filteredSetUsingPredicate:partIDPredicate]; | |
if ([twinSet count]) | |
continue; | |
mimeType = [part mimeType]; | |
if ([mimeType isEqualToString:@"text/CALENDAR"]) | |
attType = NMAttachmentTypeCalendarInvite; | |
else if ([mimeType isEqualToString:@"image/GIF"] || | |
[mimeType isEqualToString:@"image/PNG"] || | |
[mimeType isEqualToString:@"image/JPEG"]|| | |
[mimeType isEqualToString:@"image/jpeg"]|| | |
[mimeType isEqualToString:@"image/png"]) | |
attType = NMAttachmentTypePicture; | |
else | |
attType = NMAttachmentTypeFile; | |
//create new | |
attach = [NSEntityDescription insertNewObjectForEntityForName:entity_name_Attachment | |
inManagedObjectContext:moc]; | |
[attach setMessage:theMessage]; | |
[attach setFileName:[part filename]]; | |
[attach setMcMimeType:mimeType]; | |
[attach setMcPartID:[part partID]]; | |
[attach setType:@(attType)]; | |
[attach setMcEncoding:@([part encoding])]; | |
/* | |
we can't wait tham attach store in moc and merge in mainMOC | |
that we carefly perform catch attach with account in mainMOC | |
and theMessage and attach in backgroundMOC and wait 5 sec to perform | |
*/ | |
[moc processPendingChanges]; | |
[self saveContextForBGTask:moc]; | |
NSManagedObjectContext *mainMOC = [self managedObjectContext]; | |
[mainMOC performBlock:^{ | |
Account *mmAccount = (Account*)[mainMOC objectWithID: [account objectID]]; | |
Attachment *mmAttach = (Attachment*)[mainMOC objectWithID: [attach objectID]]; | |
Message *mmMessage = (Message*)[mainMOC objectWithID: [theMessage objectID]]; | |
[[PostCollector defaultCollector] downloadMessageAttachmentForMessage:mmMessage | |
andAttachment:mmAttach | |
andMTAccount:mmAccount]; | |
}]; | |
} | |
} | |
} | |
} | |
[self saveContextForBGTask:moc]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment