iOS - 何时在主线程中调用 UI 更改函数
iOS - When To Call UI Changing Functions In The Main Thread
每次我对我的服务器进行 API 调用以获取数据时,我已经知道我必须使用以下块来执行 UI 更改命令,因为我的 API调用在后台线程中执行:
dispatch_async(dispatch_get_main_queue(), ^{
//do UI stuff
});
但是,如果我有一个函数可以 UI 更改 API 调用块之外的内容怎么办?例如:
-(void)doALotOfUIChanging
{
//do a lot of UI changing
}
在我的 API 调用块中,我是否需要像这样在主线程中调用 UI 更改函数?:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self doALotOfUIChanging];
});
}
errorCallback:^(NSString *error)
{
NSLog(@"%@", error);
}];
或者我不必在主线程中调用它,因为该函数已经像这样在 API 调用块之外了吗?:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
[self doALotOfUIChanging];
}
errorCallback:^(NSString *error)
{
NSLog(@"%@", error);
}];
我也有对其他视图控制器执行转场的函数,所以我也想知道我是否也应该在主线程中调用它们。我正在清理一些代码,我不想在可能不需要的情况下不断重写 dispatch_async 函数,因此非常感谢任何帮助或建议。谢谢。
简短回答:是的,您应该在主线程上更新 UI。
Threads and Your User Interface
If your application has a graphical user interface, it is recommended
that you receive user-related events and initiate interface updates
from your application’s main thread. This approach helps avoid
synchronization issues associated with handling user events and
drawing window content. Some frameworks, such as Cocoa, generally
require this behavior, but even for those that do not, keeping this
behavior on the main thread has the advantage of simplifying the logic
for managing your user interface.
There are a few notable exceptions where it is advantageous to perform
graphical operations from other threads. For example, you can use
secondary threads to create and process images and perform other
image-related calculations. Using secondary threads for these
operations can greatly increase performance. If you are not sure about
a particular graphical operation though, plan on doing it from your
main thread.
参考:
首先,你不应该在块中使用 self 。您可以使用 __weak yourClassName *weakSelf = self 代替。
关于您的问题,所有 UI 更改都应在主线程上完成。所以你需要这样做:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf doALotOfUIChanging];
});
}
希望对您有所帮助。 :)
每次我对我的服务器进行 API 调用以获取数据时,我已经知道我必须使用以下块来执行 UI 更改命令,因为我的 API调用在后台线程中执行:
dispatch_async(dispatch_get_main_queue(), ^{
//do UI stuff
});
但是,如果我有一个函数可以 UI 更改 API 调用块之外的内容怎么办?例如:
-(void)doALotOfUIChanging
{
//do a lot of UI changing
}
在我的 API 调用块中,我是否需要像这样在主线程中调用 UI 更改函数?:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self doALotOfUIChanging];
});
}
errorCallback:^(NSString *error)
{
NSLog(@"%@", error);
}];
或者我不必在主线程中调用它,因为该函数已经像这样在 API 调用块之外了吗?:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
[self doALotOfUIChanging];
}
errorCallback:^(NSString *error)
{
NSLog(@"%@", error);
}];
我也有对其他视图控制器执行转场的函数,所以我也想知道我是否也应该在主线程中调用它们。我正在清理一些代码,我不想在可能不需要的情况下不断重写 dispatch_async 函数,因此非常感谢任何帮助或建议。谢谢。
简短回答:是的,您应该在主线程上更新 UI。
Threads and Your User Interface
If your application has a graphical user interface, it is recommended that you receive user-related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content. Some frameworks, such as Cocoa, generally require this behavior, but even for those that do not, keeping this behavior on the main thread has the advantage of simplifying the logic for managing your user interface.
There are a few notable exceptions where it is advantageous to perform graphical operations from other threads. For example, you can use secondary threads to create and process images and perform other image-related calculations. Using secondary threads for these operations can greatly increase performance. If you are not sure about a particular graphical operation though, plan on doing it from your main thread.
参考:
首先,你不应该在块中使用 self 。您可以使用 __weak yourClassName *weakSelf = self 代替。
关于您的问题,所有 UI 更改都应在主线程上完成。所以你需要这样做:
[apiObject getDataFromObject:my.Object successCallback:^(Array *data)
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf doALotOfUIChanging];
});
}
希望对您有所帮助。 :)