将数据重新加载到 NSArray 时出现问题(可能是因为 ARC)

发布于 2024-12-16 18:59:29 字数 1479 浏览 5 评论 0原文

当我的 UITableView 重新加载时,我会调用以下方法:

-(NSArray *)theAccounts {

    if (__theAccounts != nil) {
        return __theAccounts;
    }

    // Create an account store object.
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [[NSArray alloc] initWithArray:[accountStore accountsWithAccountType:accountType]];
            self.theAccounts = accountsArray;
        }
    }];

    return __theAccounts;        

}

Setter 方法

@property (strong, nonatomic) NSArray *theAccounts;

.h:和 .m: 中的

@synthesize theAccounts = __theAccounts;

我希望能够有效地清空 self.theAccounts 并重新加载。因此,我创建了一个重新同步方法,但在重新加载表后它永远不会返回任何值:

-(void)resyncAccounts {
    self.theAccounts = nil;
    [self.tableView reloadData];
}

我在 iOS 5 SDK 上使用 ARC。这会是一个问题吗?我之前用 fetchedResultsController 做过类似的事情,没有出现任何问题,但这不是 ARC。值得注意的是,它确实在第一次调用时返回数据,然后返回 __TheAccounts,直到我尝试 -(void)resyncAccounts{}。

I have the following method called when my UITableView reloads:

-(NSArray *)theAccounts {

    if (__theAccounts != nil) {
        return __theAccounts;
    }

    // Create an account store object.
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [[NSArray alloc] initWithArray:[accountStore accountsWithAccountType:accountType]];
            self.theAccounts = accountsArray;
        }
    }];

    return __theAccounts;        

}

Setter Methods in .h:

@property (strong, nonatomic) NSArray *theAccounts;

and in the .m:

@synthesize theAccounts = __theAccounts;

I would like to be able to effectively empty self.theAccounts and reload. So I created a resync method, but it never returns any values after I reload the table:

-(void)resyncAccounts {
    self.theAccounts = nil;
    [self.tableView reloadData];
}

I am using ARC on iOS 5 SDK. Could this be an issue? I've done similar before with fetchedResultsController and had no issues, but that was not ARC. Worth noting that it does return data the first time it is called, and returns __TheAccounts after that, until I try to -(void)resyncAccounts{}.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-12-23 18:59:29

为什么不在完成处理程序中设置“theAccounts”后重新加载 tableView ?另外,你不应该在将“self.theAccounts”设置为nil后调用它吗?

Why don't you reload the tableView after setting "theAccounts" in the completion handler? Also, shouldn't you call "self.theAccounts" after setting it to nil?

情深如许 2024-12-23 18:59:29

在 getter 中,第一次返回 nil,因为该块尚未执行

in the getter, you are returning nil the first time because the block has not yet executed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文