26

I am getting the error:

"A long-running Parse operation is being executed on the main thread. Break on warnParseOperationOnMainThread() to debug."

and

"Break on warnParseOperationOnMainThread() to debug."

I'm unable to locate the error within my code. Can someone please tell me what I'm doing wrong?

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:@" "];
}];
Bista
  • 7,869
  • 3
  • 27
  • 55
user3159704
  • 271
  • 1
  • 4
  • 6
  • maybe this line `PFQuery *query = [PFQuery queryWithClassName:@"User"];` is a long-running job which will block the main thread. – KudoCC Jan 08 '14 at 07:10

4 Answers4

60

This is a gentle warning to the developers when they make the Parse calls that would block the main thread.

This is where you can see it all happen,, add a symbolic breakpoint on warnBlockingOperationOnMainThread only if you use a Parse API released from 2015+. Otherwise, put it on the warnParseOperationOnMainThread.

It'll break on that function while you are running your code, and will show you a stack trace which should help you to find the blocking function.

See the images below to have a better understanding.

enter image description here

enter image description here

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • 7
    For anyone here in the 2015+ era, you need to use `warnBlockingOperationOnMainThread` in the symbolic breakpoint, instead of `warnParseOperationOnMainThread`. – Mike Feb 13 '15 at 03:01
  • Is it possible to silence this warning? – Maduranga E Apr 16 '17 at 14:37
8

For me this happened when I called:

[[PFUser currentUser] refresh];

The solution was to replace it with:

[[PFUser currentUser] refreshInBackgroundWithBlock:nil];

See also this answer on the Parse Help site.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • 1
    +1 For me it was `[[PFInstallation currentInstallation] save];` vs `[[PFInstallation currentInstallation] saveInBackground];`, but same idea. – Olie Jun 02 '15 at 18:23
2

It Almost happens with all Parse queries or data saving. It avoid this, there is option to perform operation in background. Actually there are two alternatives, one is to perform in background and other is perform in background with block of code.

Pandurang Yachwad
  • 1,695
  • 1
  • 19
  • 29
  • Welcome. Can you provide more details about the alternatives? – mins Apr 19 '15 at 23:57
  • For example for saving data: 1. saveInBackground 2. saveInBackgroundWithBlock or to query first object from the database: 1. query.getFirstObjectInBackgroud 2. query.getFirstObjectInBackgroudWithBlock – Pandurang Yachwad Apr 20 '15 at 00:26
0

Maybe it is a bit late, but here you go. I think the problem come from the fact that you are trying to get to objects fetch at the same time:

[[PFUser currentUser] objectId];

and:

 [query getObjectInBackgroundWithId...];

It will be better to get the userId first, such as:

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

Second:

  // 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 voila!

milguad
  • 7
  • 5