我是Parse和数据库的新手,我目前很困惑。
在视图上,有4个按钮和一个标签。这是我的代码(对不起,它非常混乱):
我刚刚听说UI更新和操作不应该在主线程中完成,因为PFQuery
不直接支持UI。
显然,日志执行错误:Warning: A long-running Parse operation is being executed on the main thread. Break on warnParseOperationOnMainThread() to debug.
我想使用块外部的查询数据更新label.text
,那么如何设置数据(值)以便我可以在代码中的其他方法中使用它?
我知道这是一个非常基本的问题,但如果有人可以帮我解决这个问题,我会非常高兴。
PFQuery *query = [PFQuery queryWithClassName:@"DataClass"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSString *getTheStr1 = object[@"A"];
NSString *getTheStr2 = object[@"B"];
NSString *getTheStr3 = object[@"C"];
NSString *getTheStr4 = object[@"D"];
//when I press a button, each shows a different text on the label.
UIButton *button = (UIButton *)sender;
switch ([button tag]) {
case 1:
Label.text = [NSString stringWithFormat:@"%@", getTheStr1];
break;
case 2:
Label.text = [NSString stringWithFormat:@"%@", getTheStr2];
break;
case 3:
Label.text = [NSString stringWithFormat:@"%@", getTheStr3];
break;
case 4:
Label.text = [NSString stringWithFormat:@"%@", getTheStr4];
}
}
}];
谢谢。
我来自上一个问题 How can I retrieve multiple data simultaneously? (Parse DB, iOS)
答案 0 :(得分:0)
你反过来说:UI只能在主线程上更新。您应该尽量避免在主线程上执行所有其他类型的长时间运行操作(例如网络请求),这样就不会阻止它。
您的代码似乎没问题,有什么问题?
答案 1 :(得分:0)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
PFQuery *query = [PFQuery queryWithClassName:@"DataClass"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSString *getTheStr1 = object[@"A"];
NSString *getTheStr2 = object[@"B"];
NSString *getTheStr3 = object[@"C"];
NSString *getTheStr4 = object[@"D"];
//when I press a button, each shows a different text on the label.
UIButton *button = (UIButton *)sender;
switch ([button tag]) {
case 1:
Label.text = [NSString stringWithFormat:@"%@", getTheStr1];
break;
case 2:
Label.text = [NSString stringWithFormat:@"%@", getTheStr2];
break;
case 3:
Label.text = [NSString stringWithFormat:@"%@", getTheStr3];
break;
case 4:
Label.text = [NSString stringWithFormat:@"%@", getTheStr4];
}
}
}];
});