将布尔值 False 放入运行循环 while

发布于 2024-11-19 17:46:44 字数 1659 浏览 5 评论 0原文

我正在尝试使用 Chilkat 库从我的 ftp 服务器获取目录文件列表。 在这种情况下,我想在进程运行时对 UIActivityIndi​​catorView 进行动画处理。但问题是,UIActivityIndi​​catorView 永远不会开始动画。我使用的代码是:

[self.activityIndicator startAnimating];
[selfgetListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

activityIndi​​cator是UIActivityIndi​​catorView的对象,ftpPath是我在FTP服务器中的文件路径的NSString,getListFromPath是使用 Chilkat 算法从 FTP 服务器获取列表的方法,ftpConnect 是 FTP 连接类的对象。

我在调用 getListFromPath 函数之前尝试使用 NSRunLoop,因此我将代码更改为:

[self.activityIndicator startAnimating];

BOOL waitingOnProcessing = YES;
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {

}

[self getListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

这使 activityIndi​​cator 动画化,但 getListFromPath 从未触发。经过试验,我选择再次将代码更改为:

[self.activityIndicator startAnimating];

    BOOL waitingOnProcessing = YES;
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
        waitingOnProcessing = NO;       
    }

    [self getListFromPath:ftpPath withConnection:ftpConnect];
    [self.activityIndicator stopAnimating];

它使 activityIndi​​cator 动画化,并触发 getListFromPath 函数。但我对这段代码感到怀疑,我对这段代码是否正确?或者也许使用 NSRunLoop 有一个不好的做法? 有人可以告诉我吗

谢谢

i'm trying to get directory file listing from my ftp server using Chilkat library.
In this case, i want to animating a UIActivityIndicatorView when the process is running. But the problem is, the UIActivityIndicatorView never start to animate. The code I use is :

[self.activityIndicator startAnimating];
[selfgetListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

activityIndicator is an object of UIActivityIndicatorView, ftpPath is a NSString of my file path in FTP server, and getListFromPath is the method for getting list from FTP server using Chilkat algorithm, ftpConnect is an object of FTP Connection Class.

I was try to use NSRunLoop before called getListFromPath function, so I changed my code into :

[self.activityIndicator startAnimating];

BOOL waitingOnProcessing = YES;
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {

}

[self getListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

this make the activityIndicator animating, but the getListFromPath never fired. After a trial, i choosed to changed again my code into :

[self.activityIndicator startAnimating];

    BOOL waitingOnProcessing = YES;
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
        waitingOnProcessing = NO;       
    }

    [self getListFromPath:ftpPath withConnection:ftpConnect];
    [self.activityIndicator stopAnimating];

it make the activityIndicator animating, and also fired the getListFromPath function. But i'm doubt with this code, am i right with this code? or maybe there is a bad practice for using NSRunLoop?
can somebody tell me

Thank you

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

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

发布评论

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

评论(2

美人如玉 2024-11-26 17:46:45

此代码在“黑匣子”中运行:

[self.activityIndicator startAnimating];
[self getListFromPath:ftpPath];
[self.activityIndicator stopAnimating];

因此当您的方法返回时会发生 UI 更新,并且您不会看到更新。

您需要做的是 startAnimating 您的活动指示器,然后在另一个线程中启动您的 getListFromPath 。当该方法终止时,您回调主线程,告诉它停止对指示器进行动画处理。

使用此方法:

[NSObject performSelectorInBackground:withObject:]

启动 getListFromPath 线程,然后在完成后,调用

[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]

将控制权交还给主线程,这将停止微调器动画。

This code runs as in a "black box" :

[self.activityIndicator startAnimating];
[self getListFromPath:ftpPath];
[self.activityIndicator stopAnimating];

So UI updates occur when your method returns, and you won't see the update.

What you need to do is startAnimating your activity indicator, then start your getListFromPath in another thread. When that method terminates, you call back your main thread telling it to stopAnimating the indicator.

Use this methods:

[NSObject performSelectorInBackground:withObject:]

to start your getListFromPath thread, then when it's done, call

[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]

to give the control back to the main thread, which will stop the spinner animation.

葬花如无物 2024-11-26 17:46:45

我不知道 Chilkat 库,但它一定有某种方式告诉您您从 ftp 服务器收到了答案。当你得到它时,你应该使用 NSNotification 或协议来告诉你的视图控制器你得到了答案。当这种情况发生时,你停止旋转。

I dont know the Chilkat library, but it must have some way of telling you that you receive an answer from your ftp server. When you got it, you should use a NSNotification or a protocol to tell your view controller that you got an answer. When that happen you stop the spinner.

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