title: Retrieving the latest emails with MailCore2
body: <p>I'm loading my latest emails and I'm saving the latest UID because later if the user reload the list of emails I want to get all the emails from that UID to now...  for this, I'm using </p>
  
  <pre><code>UInt64 start = lUid;
  UInt64 end = UINT64_MAX;
  
  MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
  MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(start, end)];
  
  MCOIMAPFetchMessagesOperation *fetchOperation = [imapSession fetchMessagesOperationWithFolder:@"INBOX"
                                                                                    requestKind:requestKind
                                                                                           uids:uids];
  </code></pre>
  
  <p>this action always return the results but I'm getting the latest email... So I tried to make </p>
  
  <pre><code>UInt64 start = lUid + 1;
  </code></pre>
  
  <p>but it is exactly the same.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I'm looking the logger and the last UID is 34038 so when i made the call the start parameter was 34039... so why im getting the email with id 34038?</p>
  
  <pre><code>2015-01-20 18:33:06.852 App[25022:730644] mailcore - IMAP: 8 UID FETCH 34039:* (UID ENVELOPE BODY.PEEK[HEADER.FIELDS (References)])
  </code></pre>
  
  <p>Anyone know whats going on?</p>
  
  <p><strong>EDIT 2:</strong>
  So this is my code</p>
  
  <pre><code>UInt64 start = lUid + 1;
  UInt64 end = UINT64_MAX;
  
  MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
  MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(start, end)];
  
  MCOIMAPFetchMessagesOperation *fetchOperation = [imapSession fetchMessagesOperationWithFolder:@"INBOX"
                                                                                    requestKind:requestKind
                                                                                           uids:uids];
  
  void(^fetchHandler)(NSError *, NSArray *, MCOIndexSet *) = ^(NSError *error, NSArray *fetchedMessages, MCOIndexSet *vanishedMessages) {
  
      if( error ) {
          completionHandler(UIBackgroundFetchResultFailed);
  
          NSLog(@"Error downloading message headers:%@", error);
      }
  
      NSMutableArray *currentMessages = [NSMutableArray arrayWithArray:fetchedMessages];
  
      if( [currentMessages count] &gt; 0 ) {
  
          MCOIMAPMessage *lastMessage = [fetchedMessages lastObject];
          NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
          [defaults setObject:@(lastMessage.uid) forKey:@"lastUID"];
          [defaults synchronize];
  
          completionHandler(UIBackgroundFetchResultNewData);
  
      }
      else {
          completionHandler(UIBackgroundFetchResultNoData);
      }
  
  };
  
  [fetchOperation start:fetchHandler];
  </code></pre>
  
  <p>and this is the logger when i execute that call</p>
  
  <pre><code>2015-01-21 18:02:46.723 App[5021:473470] mailcore - IMAP: * OK Gimap     ready for requests from 181.167.207.42 b69mb75112208qhb
  2015-01-21 18:02:46.725 App[5021:473470] mailcore - IMAP: 1 CAPABILITY
  2015-01-21 18:02:46.955 App[5021:473470] mailcore - IMAP: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
  1 OK Thats all she wrote! b69mb75112208qhb
  2015-01-21 18:02:46.955 App[5021:473470] mailcore - IMAP: 2     AUTHENTICATE XOAUTH2     dXNlcj1zYW50b21lZ29uemFsb0BnbWFpbC5jb20BYXV0aD1CZWFyZXIgeWEyOS5BZ0VQNnBQNFRJN1ZEMHlwempRNm01bTMwS2J0b0F6VzN6REdBbXc2dmh0VEF1TUJISVVaN3BobEtwY0M4dDlQRVFHcVhETHRlMU5VaFEBAQ==
  2015-01-21 18:02:47.267 App[5021:473470] mailcore - IMAP: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT
  2 OK santomegonzalo@gmail.com authenticated (Success)
  2015-01-21 18:02:47.267 App[5021:473470] mailcore - IMAP: 3 COMPRESS DEFLATE
  2015-01-21 18:02:47.518 App[5021:473470] mailcore - IMAP: 3 OK Success
  2015-01-21 18:02:47.518 App[5021:473470] mailcore - IMAP: 4 ENABLE CONDSTORE
  2015-01-21 18:02:47.767 App[5021:473470] mailcore - IMAP: * ENABLED CONDSTORE
  4 OK Success
  2015-01-21 18:02:47.767 App[5021:473470] mailcore - IMAP: 5 NAMESPACE
  2015-01-21 18:02:48.016 App[5021:473470] mailcore - IMAP: * NAMESPACE (("" "/")) NIL NIL
  5 OK Success
  2015-01-21 18:02:48.017 App[5021:473470] mailcore - IMAP: 6 ID NIL
  2015-01-21 18:02:48.267 App[5021:473470] mailcore - IMAP: * ID ("name"     "GImap" "vendor" "Google, Inc." "support-url"     "http://support.google.com/mail" "version" "gmail_imap_150113.07_p0"     "remote-host" "181.167.207.42")
  6 OK Success
  2015-01-21 18:02:48.268 App[5021:473470] mailcore - IMAP: 7 SELECT INBOX
  2015-01-21 18:02:48.770 App[5021:473470] mailcore - IMAP: * FLAGS     (\Answered \Flagged \Draft \Deleted \Seen $MailFlagBit1 $MailFlagBit0     $Phishing $Forwarded $MailFlagBit2 JunkRecorded $NotJunk NotJunk     $NotPhishing $Junk)
  * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen     $MailFlagBit1 $MailFlagBit0 $Phishing $Forwarded $MailFlagBit2 JunkRecorded $NotJunk NotJunk $NotPhishing $Junk \*)] Flags permitted.
  * OK [UIDVALIDITY 617846230] UIDs valid.
  * 20472 EXISTS
  * 0 RECENT
  * OK [UIDNEXT 34091] Predicted next UID.
  * OK [HIGHESTMODSEQ 4668463]
  7 OK [READ-WRITE] INBO
  2015-01-21 18:02:48.771 App[5021:473470] mailcore - IMAP: X selected.     (Success)
  2015-01-21 18:02:48.771 App[5021:473470] mailcore - IMAP: 8 UID FETCH     34092:* (UID ENVELOPE BODY.PEEK[HEADER.FIELDS (References)])
  2015-01-21 18:02:49.405 App[5021:473470] mailcore - IMAP: * 20472 FETCH     (UID 34090 MODSEQ (4668387) ENVELOPE ("Wed, 21 Jan 2015 20:31:46 -0000"     "Subject" (("User" NIL "pullrequests-reply" "company.org")) (("User" NIL     "toreply" "company.org")) (("User" NIL "toreply" "company.org")) ((NIL NIL     "santomegonzalo" "gmail.com")) NIL NIL "&lt;toreply@company.com&gt;" "    &lt;toreply@company.com&gt;") BODY[HEADER.FIELDS (References)] {52}
  References: &lt;toreply@company.com
  
  )
  8 OK Success
  </code></pre>