gpt4 book ai didi

ios - 我的解析查询直到应用程序的其余部分完成后才完成,我该如何阻止这种情况发生?

转载 作者:行者123 更新时间:2023-11-29 02:53:40 25 4
gpt4 key购买 nike

下面是解析查询,我试图在 Collection View 中显示类别,但是 numberOfItemsInSection 方法在 getCategories 有时间从解析中提取信息之前运行. numberOfItemsInSection 使用 getCategories _anArrayOfCategories 返回 Collection View 中的类别数。

-(void)getCategories{
[super viewWillAppear:animated]; //calls retrieve messages method below

//get Categories where the class name is Categories
PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
//- (void)selectKeys:(NSArray *)keys
[query selectKeys:@[@"CName"]];
//[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}

else {
_anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);

}
}];

}

有什么建议吗?

最佳答案

要么在查询完成后更新您的表,要么在之前的 Controller 上进行查询并在完成后推送到该 Controller 。请记住,这些是 UI 操作,需要在主线程上完成。

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}

else {
_anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);
dispatch_async(dispatch_get_main_queue(), ^{ // UI operations on the main thread
[self.tableView reloadData];
});

}
}];

编辑:只是为了确保,因为从您的帖子中不清楚,您不想从 numberOfRowsInSection 调用此方法。将它放在 viewDidLoad 或类似的东西中,然后让 numberOfRowsInSection 使用 _anArrayOfCategories 对象。

关于ios - 我的解析查询直到应用程序的其余部分完成后才完成,我该如何阻止这种情况发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com