正在主线程上执行长时间运行的Parse操作

时间:2014-01-08 06:58:06

标签: ios objective-c parse-platform

我收到错误:

  

“正在主线程上执行长时间运行的Parse操作。在warnParseOperationOnMainThread()中断以进行调试。”

  

“打破warnParseOperationOnMainThread()进行调试。”

我无法在我的代码中找到错误。有人可以告诉我我做错了吗?

PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query getObjectInBackgroundWithId:[[PFUser currentUser] objectId] block:^(PFObject *object, NSError *error) {

    self.firstName = object[@"firstname"];
    self.lastName = object[@"lastname"];

    self.nameLabel.text = [[NSArray arrayWithObjects:self.firstName, self.lastName, nil] componentsJoinedByString:@" "];
}];

4 个答案:

答案 0 :(得分:56)

当开发人员进行阻止主线程的Parse调用时,这是一个温和的警告。

只有在使用2015年以上发布的Parse API时,才能在warnBlockingOperationOnMainThread上添加符号断点。否则,请将其放在warnParseOperationOnMainThread

在运行代码时它会破坏该函数,并会显示一个堆栈跟踪,它可以帮助您找到阻塞函数。

请参阅下面的图片以便更好地理解。

enter image description here

enter image description here

答案 1 :(得分:6)

对我来说,这发生在我打电话时:

[[PFUser currentUser] refresh];

解决方案是将其替换为:

[[PFUser currentUser] refreshInBackgroundWithBlock:nil];

另见this answer on the Parse Help site

答案 2 :(得分:2)

几乎所有Parse查询或数据保存都会发生。它避免了这种情况,可以选择在后台执行操作。实际上有两种选择,一种是在后台执行,另一种是在后台执行代码块。

答案 3 :(得分:0)

也许有点晚了,但是你走了。我认为问题来自于你试图同时获取对象的事实:

[[PFUser currentUser] objectId];

和:

 [query getObjectInBackgroundWithId...];

最好先获得userId,例如:

//First fetch and store the id in a string so you can reuse it whenever you want
NSString *userId = [PFUser currentUser].objectId; 

第二:

  // Do your second fetch here:          
  PFQuery *query = [PFQuery queryWithClassName:@"User"];        

 [query getObjectInBackgroundWithId:userId block:^(PFObject *object, NSError *error) {

 self.firstName = object[@"firstname"];
 self.lastName = object[@"lastname"];

 self.nameLabel.text = [[NSArray arrayWithObjects:self.firstName, self.lastName, nil]    componentsJoinedByString:@" "];
 }];

Et瞧!