Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Created September 25, 2015 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w-i-n-s/b5684c470e877af75880 to your computer and use it in GitHub Desktop.
Save w-i-n-s/b5684c470e877af75880 to your computer and use it in GitHub Desktop.
MailCore tip1
- (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