将数据重新加载到 NSArray 时出现问题(可能是因为 ARC)
当我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在完成处理程序中设置“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?
在 getter 中,第一次返回 nil,因为该块尚未执行
in the getter, you are returning nil the first time because the block has not yet executed