开发者

Put a boolean False inside a runloop while

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 stopAnimat开发者_开发知识库ing];

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜