gpt4 book ai didi

ios - 我应该在主线程上启动和停止 UIActivityIndi​​cator 实例吗?

转载 作者:行者123 更新时间:2023-11-28 18:32:52 24 4
gpt4 key购买 nike

示例:

[query findObjectsInBackgroundWithBlock:^(NSArray *favObjects, NSError *error) {
for (PFObject *favObject in favObjects) {
dispatch_async(dispatch_get_main_queue(), ^{
[cell setSizeLabel:[_arrayOfSizes objectAtIndex:[[favObject valueForKey:@"size"] integerValue]]];
[[cell sizeDropDownButton] setTitle:[cell sizeLabel] forState:UIControlStateNormal];
});

if ([[[cell currentObject] valueForKey:@"alternativeColour"] boolValue]) {

// Colours are stored in db on parse as numbers, I use array below to determine which colour is being red back by valueForkey below

dispatch_async(dispatch_get_main_queue(), ^{
[cell setColourLabel:[_arrayOfColours objectAtIndex:[[favObject valueForKey:@"colour"] integerValue]]];
[[cell colourDropDownButton] setTitle:[cell colourLabel] forState:UIControlStateNormal];
});
}

[[cell priceLabelSpinner] stopAnimating];
[[cell titleLabelSpinner] stopAnimating];
[[cell sizeDropDownButton] setHidden:NO];
[[cell colourDropDownButton] setHidden:NO];

}

}];

您可以看到我正在主队列上设置按钮标签标题。但是在下面,我将停止在后台队列上旋转以及取消隐藏按钮。这是正确的吗?

最佳答案

任何可能导致您查看更改的东西都应该从主线程调用:

Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself but all other manipulations should occur on the main thread.

因此,考虑到 Apple 的建议,您应该将代码重写为:

[query findObjectsInBackgroundWithBlock:^(NSArray *favObjects, NSError *error) {
for (PFObject *favObject in favObjects) {
dispatch_async(dispatch_get_main_queue(), ^{
[cell setSizeLabel:[_arrayOfSizes objectAtIndex:[[favObject valueForKey:@"size"] integerValue]]];
[[cell sizeDropDownButton] setTitle:[cell sizeLabel] forState:UIControlStateNormal];
});

if ([[[cell currentObject] valueForKey:@"alternativeColour"] boolValue]) {

// Colours are stored in db on parse as numbers, I use array below to determine which colour is being red back by valueForkey below

dispatch_async(dispatch_get_main_queue(), ^{
[cell setColourLabel:[_arrayOfColours objectAtIndex:[[favObject valueForKey:@"colour"] integerValue]]];
[[cell colourDropDownButton] setTitle:[cell colourLabel] forState:UIControlStateNormal];
});
}

dispatch_async(dispatch_get_main_queue(), ^{
[[cell priceLabelSpinner] stopAnimating];
[[cell titleLabelSpinner] stopAnimating];
[[cell sizeDropDownButton] setHidden:NO];
[[cell colourDropDownButton] setHidden:NO];
});

}

}];

关于ios - 我应该在主线程上启动和停止 UIActivityIndi​​cator 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com