UITableView 在 NSoperationQueue 启动之前重新加载

发布于 2025-01-03 21:40:56 字数 528 浏览 0 评论 0原文

我正在使用 NSOperationQueue 在 vi​​ewWillAppear 中调用后台服务(我也尝试将其放入 viewDidLoad 中)。我根据从服务获得的结果填充 UITableView 。当我调试应用程序时,首先调用表,然后调用操作队列。这样一来,桌子对我来说就空了。 operationQueue 完成其工作后如何填充表。

这是代码:

viewWillAppear

-(void)viewWillAppear:(BOOL)animated
{
    operationQueue=[[NSOperationQueue alloc] init];
    ParseOperation *obj=[[ParseOperation alloc] initWithMembers:ID];
    [operationQueue addOperation:obj];
    [obj release];

}  

I am using NSOperationQueue for calling a service in background in viewWillAppear(I also tried putting it in viewDidLoad for that matter). I am populating the UITableView based on results I get from service. When I debug the application, the table gets called first and then the operationQueue. In this way the table is empty for me. How to populate the table after operationQueue does its job.

Here is the code :

viewWillAppear :

-(void)viewWillAppear:(BOOL)animated
{
    operationQueue=[[NSOperationQueue alloc] init];
    ParseOperation *obj=[[ParseOperation alloc] initWithMembers:ID];
    [operationQueue addOperation:obj];
    [obj release];

}  

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

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

发布评论

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

评论(3

枕梦 2025-01-10 21:40:56

您可以将“reloadTable”添加为依赖于解析操作的特定操作,在解析操作定义之后立即启动队列之前(因此为了安全起见,将队列初始化为挂起,然后仅在添加所有操作后才启动它) ):


// ... inside your operation code definition
NSInvocationOperation *reloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myReload) object:nil];
[reloadOp addDependency:obj] // adding here the parse operation as a dependency
[operationQueue addOperation:reloadOp];

需要将“myReload”操作定义为独立的方法,才能保证在主线程中调用reloadTable方法:


-(void)myReload {
[表 PerformSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

这样,您的重新加载代码将仅在解析操作终止后运行。如果您有更多操作要运行并且所有这些操作都需要在表重新加载之前执行,那么这非常有用。请注意,依赖项不知道操作正常终止或已被取消。

您可以考虑的另一个好方法是定义一个 GCD 串行队列并按顺序添加两个块(第一个块是解析,第二个块是表重新加载)。在这种情况下,GCD 将保证两个块的正确执行顺序 (FIFO)。

You can add the "reloadTable" as a specific operation with dependency on the parse operation, immediately after the parse operation definition and just before starting the queue (so for safety initialize the queue as suspended and then start it only once all operations have been added):


// ... inside your operation code definition
NSInvocationOperation *reloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myReload) object:nil];
[reloadOp addDependency:obj] // adding here the parse operation as a dependency
[operationQueue addOperation:reloadOp];

The "myReload" operation needs to be defined as a stand-alone method in order to ensure that the reloadTable method is called in the main thread:


-(void)myReload {
[table performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

In this way your reload code will be run only after the parse operation terminates. This is useful if you have more operations to be run and all them needs to be executed before the table reload. Note that the dependency is unaware of the fact that the operation terminated normally or has been cancelled.

Another good way you may consider is to define a GCD serial queue and add the two blocks in sequence (first block is parsing, second block is table reload). In such case GCD will guarantee the proper execution order of the two blocks (FIFO).

旧夏天 2025-01-10 21:40:56

您必须为您的 NSOperation 设置一个完成块。您必须在表视图上调用重新加载数据的位置。

- (void)setCompletionBlock:(void (^)(void))block

编辑:

添加这个

[obj setCompletionBlock:^{
                                NSLog(@"I have fnish");
                                 [self.tableView reloadData];
                                } 
     ]; 

You have to set a completion bloc for your NSOperation. Where you have to call reload data on your table view.

- (void)setCompletionBlock:(void (^)(void))block

Edit:

add this

[obj setCompletionBlock:^{
                                NSLog(@"I have fnish");
                                 [self.tableView reloadData];
                                } 
     ]; 
并安 2025-01-10 21:40:56

operationQueue 完成后调用 [tableView reloadData]

Call [tableView reloadData] after the operationQueue finished.

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