xcode - ios loading问题
PHP中文网
PHP中文网 2017-04-18 09:04:53
0
3
413
// 操作1
dispatch_async(dispatch_get_main_queue(), ^{
    [SVProgressHUD show];//loading动画
});

// 操作2
NSArray *files = @[model];
DownFileDataControl *downVC = [[DownFileDataControl alloc]init];
downVC.rootViewController = curViewController;
downVC.thisModel = model;
downVC.modelList = files;
[downVC downLoadFlieToThePhoneWithThreeModel:model];//根据model对象里的一个文件地址去读取文件。读取完全后跳转到一个新的页面

(SVProgressHUD 是异步操作的话有办法改成同步吗)

上述代码,在一个方法体内写的,因为读取文件的时间比较长,所以想着在读取前加一个loading效果,但是加上之后,发现loading依然是等操作2执行完毕后才显示效果,操作2执行时候(读取文件)程序有一种假死的感觉,请问,是什么原因,如何让loading先出来,再执行操作2

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
Peter_Zhu

Generally, you don’t need to adjust the ProgressHUD method, but you need to determine which time-consuming operations are, and then place the time-consuming operations in a non-main thread, otherwise the interface will freeze.

[downVC downLoadFlieToThePhoneWithThreeModel:model];

In your question, the implementation of this sentence is the key. What exactly did you do and in what thread did you do it? Check it carefully.

伊谢尔伦

Is your method executed in the main thread? dispatch_async is dispatched to the main thread to asynchronously execute the loading animation. This code will not be executed until the next loop, so the code below will be executed before the loading animation is displayed. Remove dispatch_async and it will be synchronized. Is downLoadFlieToThePhoneWithThreeModel:model synchronous or asynchronous?

小葫芦

Since the thread in GCD will get stuck if it executes, first use [NSThread currentThread].isMainThread to determine the main thread. If it is the main thread, it will be displayed directly.
Operation 2 also checks whether it is in the main thread. If it is in the main thread, it is executed in another thread through GCD.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template