0

I'm trying to break from a loop to the very first loop (so very first loop can continue), but if I break from the point [self saveContext] I'll go one level up, right?

- (void)arrangeAllContacts
{
    NSArray *messages = [HumanResponse allAcceptedRecordsTextMessages];
    __block CoreDataPhotoRecord *photoDetails;

    self.addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    /* NSMutableArray *emails = [NSMutableArray new]; */
    NSArray *mobiles = [NSArray new];
    NSString *name;

    if (!self.allContacts) {
        NSLog(@"NO ADDRESS BOOK ENTRIES TO SCAN");
        return;
    }

    for (int a=0; a<messages.count; a++) {
        photoDetails = (CoreDataPhotoRecord *)messages[a];

        for (int i=0; i<self.allContacts.count; i++ ) {
            ABRecordRef person = (__bridge ABRecordRef)self.allContacts[i];

            /* ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); */
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers);

            for (int k=0; k<phoneNumberCount; k++) {
                CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex(phoneNumbers, k);

                NSString *phoneNumber = (__bridge NSString *)(phoneNumberValue);
                phoneNumber = [self getCleanMobile:phoneNumber];

                if ([phoneNumber isEqualToString:photoDetails.message.mobile]) {
                    name = [self nameForPerson:self.selectedPerson];
                    mobiles = [self numbersForContact:phoneNumbers];
                    /* emails = [self emailsForContact:emails]; */

                    [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
                        photoDetails = [photoDetails MR_inContext:localContext];
                        photoDetails.message.contactName = name;
                        photoDetails.message.contactNumber = mobiles;
                    }];
                    [self saveContext];

                    // How to break to first 'for' loop?
                }
            }
        }
    }


    if (self.addressBook != nil) {
        CFRelease(self.addressBook);
    }
}
Idan Moshe
  • 1,675
  • 4
  • 28
  • 65

3 Answers3

1

if i understand you correctly, you want to "break" from the third loop to the first one(top one in the hierarchy) ?

if that is so , just define a Boolean in the beginning (or outside the loops),and when you want to break, set it to yes, and add an if statement in the beginning of the second loop : if (shouldBrake)beak;

Nevgauker
  • 241
  • 1
  • 12
1

As suggested earlier, defining a boolean and checking it in the for loop:
for (int i=0; notFoundPhoneNumber && i<self.allContacts.count; i++ ) {

Personally I find it more readable to do a while loop for the contacts (our mind tends to skip a condition of a for loop), so you can do:

while (shouldStop)
{
    if (i == self.allContacts.count)
    {
        shouldStop = true;
    }
    ...
}

or do a while on the complex condition notFoundPhoneNumber && i<self.allContacts.count

Any other solution will require redesign to your data structure (and I assume you don't want it)

Boaz
  • 4,864
  • 12
  • 50
  • 90
1

You just make a function for phone number things and return boolean value.

    for (int i=0; i<self.allContacts.count; i++ ) {
        ABRecordRef person = (__bridge ABRecordRef)self.allContacts[i];

        /* ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); */
        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers);
        if ([self phoneNumberThings:phoneNumberCount]) break;
    }

〜〜〜〜

- (BOOL)phoneNumberThings:(int)phoneNumberCount {
    for (int k=0; k<phoneNumberCount; k++) {
         CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex(phoneNumbers, k);

          NSString *phoneNumber = (__bridge NSString *)(phoneNumberValue);
          phoneNumber = [self getCleanMobile:phoneNumber];

          if ([phoneNumber isEqualToString:photoDetails.message.mobile]) {
              name = [self nameForPerson:self.selectedPerson];
              mobiles = [self numbersForContact:phoneNumbers];
              /* emails = [self emailsForContact:emails]; */

              [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
                  photoDetails = [photoDetails MR_inContext:localContext];
                  photoDetails.message.contactName = name;
                  photoDetails.message.contactNumber = mobiles;
              }];
              [self saveContext];

              // How to break to first 'for' loop?
              return YES
          }
    }
    return NO;
}
Robasan
  • 155
  • 10