【问题标题】:How to know, when this block completes execution如何知道这个块何时完成执行
【发布时间】:2013-04-09 01:42:32
【问题描述】:

我正在尝试根据 recordID 访问一个人的 ABAddressBook 属性,但是每次我尝试获取一个属性时,我都会得到一个空值。

这是我尝试过的代码。

    ABAddressBookRef addressBook = NULL;
    addressBook = ABAddressBookCreateWithOptions(addressBook, nil);
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID) [[object valueForKey:@"recordId"] description]);
    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSLog(@"First Name %@", firstName);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSString *name = [NSString stringWithFormat:@"%@" "%@", lastName, firstName];
    NSLog(@"Full Name %@", name);

我在这里做错了什么?

编辑:我已经想通了上述问题。这是我用过的。 现在我遇到的问题是,只有当这段代码完成执行时,我才需要重新加载我的 UITableView。但我无法弄清楚这个块何时完成执行。需要帮助。

CFErrorRef myError = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &myError);
ABAddressBookRequestAccessWithCompletion(addressBook,
                                         ^(bool granted, CFErrorRef error) {
                                             if (granted) {

                                                 ABRecordID recID = [recordId intValue];

                                                 ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recID);
                                                 NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

                                                 NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
                                                 if(!firstName){
                                                     firstName = @"";
                                                 }
                                                 if(!lastName){
                                                     lastName = @"";
                                                 }

                                                 NSString *name = [NSString stringWithFormat:@"%@ %@", lastName, firstName];
                                                 if([firstName length] < 1 && [lastName length] < 1){
                                                     name = @"No Name";
                                                 }
                                                 self.personName = name;
                                                 NSData  *imageData = (NSData *)CFBridgingRelease(ABPersonCopyImageData(person));
                                                 if(imageData){
                                                     self.personImage = [UIImage imageWithData:imageData];
                                                 }
                                                 else{
                                                     self.personImage = [UIImage imageNamed:@"default.png"];
                                                 }

                                                 NSMutableArray *array = [NSMutableArray array];
                                                 [array addObject:self.personName];
                                                 [array addObject:self.personImage];

                                                 [self.personDetailsArray addObject:array];
                                                 [self.tableView reloadData];

                                             } else {
                                                 // Handle the error
                                             }
                                         });

更新块内的 UIElements 是否正确?如果不是,还有什么选择。

我想要的是仅在块执行后才加载/更新我的 UITableView。还没找到有用的东西

【问题讨论】:

  • person 对象是否是一个有效的对象?
  • 否。人员对象无效。但我做错了什么?

标签: iphone ios ios6 abaddressbook


【解决方案1】:

我认为您创建的通讯簿是错误的。试着用这个来创建你的通讯录:

 ABAddressBookRef addressBook = ABAddressBookCreate();

您不需要在第一行声明为 NULL,因此请删除它。然后尝试获取person 对象,看看它是否返回一个有效的引用。

另外,我相信ABAddressBookCreateWithOptions 的参数如下:

ABAddressBookRef ABAddressBookCreateWithOptions (
   CFDictionaryRef options,
   CFErrorRef* error
);

但您传递的不是选项,而是对地址簿的引用。我对ABAddressBookCreateWithOptions 运气不佳,所以我只使用ABAddressBookCreate。它在 iOS 6 中已被弃用,但这对我来说并不是什么大问题。

【讨论】:

  • 试试ABAddressBookCreateWithOptions(NULL, NULL)
  • 是的。做对了。但问题更多在于将我的数据库中的 recordID 转换为 ABRecordID。
  • @XaviValero 如果您能够用您想出的答案解决它,请自己发布答案并接受它以关闭问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 2017-05-25
相关资源
最近更新 更多