2014-09-12 41 views
1

我嘗試使用我的應用程序創建功能,以便在iOS上輕鬆添加和/或從羣組中刪除人員。下面的代碼完美地工作,如果我:iOS地址簿將多個人添加到羣組

1)一次 2)添加一個人一次刪除一個人

然而,如果我選擇多人添加和/或刪除同時只有第一個動作 - 首先添加(如果有)或首先刪除(如果沒有添加) - 提交到地址簿。這種方法可以通過任何一個單獨的動作反覆調用,並且會成功。我無法確定爲什麼我無法在單次調用保存的上下文中保存多個添加/刪除操作。應該指出的是,沒有錯誤是通過代碼進行的,布爾人用來確定成功的代碼表明一切都很完美。

我嘗試過調整代碼的各種組合,例如:創建本地地址簿,檢索groupRef,將地址簿全部保存在每個for循環中。我也調查了恢復地址簿後保存到無濟於事。想法或經驗?

-(IBAction)save:(id)sender 
{ 
    @synchronized(self) 
    { 
     CFErrorRef error = NULL; 
     BOOL success = NO; 
     BOOL successfulSave = NO; 

     ABAddressBookRef localAddressBook = ABAddressBookCreateWithOptions(NULL, &error); 
     group = ABAddressBookGetGroupWithRecordID(localAddressBook, groupID); 

     for (CFIndex addItemCount = 0; addItemCount < [theAddList count]; ++addItemCount) 
     { 
      NSNumber *addID = [theAddList objectAtIndex:addItemCount]; 
      ABRecordRef thePersonID = ABAddressBookGetPersonWithRecordID(localAddressBook, [addID intValue]); 
      success = success || ABGroupAddMember(group, thePersonID, &error); 
     } 

     for (CFIndex removeItemCount = 0; removeItemCount < [theRemoveList count]; ++removeItemCount) 
     { 
      NSNumber *removeID = [theRemoveList objectAtIndex:removeItemCount]; 
      ABRecordRef thePersonID = ABAddressBookGetPersonWithRecordID(localAddressBook, [removeID intValue]); 
      success = success || ABGroupRemoveMember(group, thePersonID, &error); 
     } 

     if (success) 
     { 
      successfulSave = ABAddressBookSave(localAddressBook, &error); 
     } 

     if (!(successfulSave && success)) 
     { 
      UIAlertView *addressBookUpdateAlert = [[UIAlertView alloc] initWithTitle:@"AddressBook Error" message:@"An update to your address book could not be processed at this time. Try again shortly." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
      // we can't update a GUI element from a background thread - appears sensitivity didn't appear until iOS 8 
      [[NSOperationQueue mainQueue] addOperationWithBlock: 
      ^{ 
       [addressBookUpdateAlert show]; 
      }]; 
     } 
     else 
     { 
      [[self navigationController] popViewControllerAnimated:YES]; 
     } 
    } 
} 

回答

0

我無法解釋,但我做了應該做了捕獲錯誤的一個更好的工作,而是似乎導致代碼奇蹟般地開始工作,一個微小的變化 - 它的方式通常是代碼因爲你在某處寫了內存。

無論如何,我做了以下內容,並開始工作:

  1. 設置成功= YES進入循環
  2. 前更改了||測試在既有& &測試循環

所有我改變了,但不知何故,剛開始工作......奇怪......我要去測試/錘的東西的情況下,我看到的東西。 ..