MCOIMAPIdleOperation Issue

251 Views Asked by At

I am totally new in IOS Development, And I am making a app using MailCore2 api. Company has told me to use MCOIMApIdleOperation to get the emails from Gmail Server. I have google all the way to find out solution about this but it is not worth it. Here is detail about the Problem, First I want to load the emails from the INBOX folder through MCOIMAPIdleOperation.Below is my code for fetching emails.

- (void)viewDidLoad{
[super viewDidLoad];
//Do any additional setup after loading the view from its nib.
//Made connection with Gmail Imap Server
NSUserDefaults *defaules = [NSUserDefaults standardUserDefaults];
NSString *emailid = [defaules objectForKey:@"emailid" ];
NSString *password = [defaules objectForKey:@"password" ];
session = [[MCOIMAPSession alloc] init];
session.hostname = @"imap.gmail.com";
session.port = 993;
session.username = emailid;
session.password = password;
session.connectionType = MCOConnectionTypeTLS;

idle=[session idleOperationWithFolder:@"INBOX" lastKnownUID:0];
[idle start:^(NSError *err){
     MCOIMAPMessagesRequestKind requestKind = (MCOIMAPMessagesRequestKind)
(MCOIMAPMessagesRequestKindHeaders | MCOIMAPMessagesRequestKindStructure |MCOIMAPMessagesRequestKindInternalDate | MCOIMAPMessagesRequestKindHeaderSubject |MCOIMAPMessagesRequestKindFlags);
MCOIMAPFolderInfoOperation *inboxFolderInfo = [session folderInfoOperation:Folder];
NSLog(@"statrt1");
[inboxFolderInfo start:^(NSError *error, MCOIMAPFolderInfo *info)
 {
     NSLog(@"start2");
     BOOL totalNumberOfMessagesDidChange =
     self.totalNumberOfInboxMessages != [info messageCount];
     self.totalNumberOfInboxMessages = [info messageCount];
     NSUInteger numberOfMessagesToLoad =MIN(self.totalNumberOfInboxMessages, nMessages);

     if (numberOfMessagesToLoad == 0)
     {
         self.isLoading = NO;
         return;
     }
     // If total number of messages did not change since last fetch,
     // assume nothing was deleted since our last fetch and just
     // fetch what we don't have
     MCORange fetchRange;

     if (!totalNumberOfMessagesDidChange && msgbody.count)
     {
         numberOfMessagesToLoad -= msgbody.count;

         fetchRange = MCORangeMake(self.totalNumberOfInboxMessages -msgbody.count -(numberOfMessagesToLoad - 1),(numberOfMessagesToLoad - 1));

     }
     // Else just fetch the last N messages
     else
     {
         fetchRange =MCORangeMake(self.totalNumberOfInboxMessages -(numberOfMessagesToLoad - 1),(numberOfMessagesToLoad - 1));


     }

     MCOIMAPFetchMessagesOperation *imapMessagesFetchOp =[session fetchMessagesByNumberOperationWithFolder:Folder requestKind:requestKind numbers:
                                                          [MCOIndexSet indexSetWithRange:fetchRange]];
    [imapMessagesFetchOp start:^(NSError *error, NSArray *messages, MCOIndexSet *vanishedMessages)
      {
          NSSortDescriptor *sort =[NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO];
          NSMutableArray *combinedMessages = [NSMutableArray arrayWithArray:messages];
          [combinedMessages removeAllObjects];
          [combinedMessages addObjectsFromArray:messages];
          msgbody=[combinedMessages sortedArrayUsingDescriptors:@[sort]];
          [uitable reloadData];
      }];

 }];

}];

}

By Above code mails are fetched successfully.Problem is when new mail is arrive above code is not running again.what to do so that i can get the new mails when they arrive.. Please help me to solve this issue.

0

There are 0 best solutions below