How to retrieve multiple data at the same time from Parse? (Obj-C)

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have clarified my previous question. How can I retrieve multiple data simultaneously? (Parse DB, iOS)

Hello, I am new to Parse and databases, and I am struggling a bit here.

On the view, there are 4 buttons and a label.

So basically what I want to do is retrieve first the object of column @"A" (at the same time displaying the data on the label) , and then retrieve the attributes of the same row. When I press any other buttons, they should display the queried attribute which is in the same row with @"A"

But currently this code is running a query every time I press the button, so each four is a mess sending different random datas to the view.

Also, the log executes an error: Warning: A long-running Parse operation is being executed on the main thread. Break on warnParseOperationOnMainThread() to debug.

I would be really happy if somebody can help me out with the solution for this.

PFQuery *query = [PFQuery        queryWithClassName:@"DataClass"];       //get a data(object) randomly int count = [query countObjects]; int randomCount = arc4random() % count; query.skip = randomCount;  [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {      if (!error) {             //retrieving data... but asynchronously!         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];         }     } }]; 

回答1:

You are getting this because if the line [query countOfObjects];

Count of objects is a synchronous function that goes to the parse server to get information. I.e. It's a long running process on the main thread.

You should use the function...

[query countObjectsInBackgroundWithBlock... 

Then in the completion block of that you can do something with the count information.

I.e. Generate a random number and then run your find first function.

Edit to show how this could be done

- (PFQuery *)query {     PFQuery *query = [PFQuery queryWithClassName:@"DataClass"];       return query; }  // putting the button in here for the tag. // it should not be here though.  // using tags is never the way to go. - (void)getRandomSkipForQuery:(UIButton *)button {     PFQuery query = [self query];      [query countObjectsInBackgroundWithBlock:^(NSInteger count, NSError *error) {         // check for errors...         // don't use arc4random % something         NSInteger randomCount = arc4random_uniform(count);          [self getObjectWithSkip:randomCount button:button];     }]; }  - (void)getObjectWithSkip:(NSInteger)skip button:(UIButton *)button {     PFQuery *query = [self query];     query.skip = skip;      [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {         if (!error) {             //ugh this is really bad design             //when I press a button, each shows a different text on the label.             switch ([button tag]) {                 case 1:                     //variables begin with a lowercase letter                     label.text = [NSString stringWithFormat:@"%@", object[@"A"]];                     break;                  case 2:                     label.text = [NSString stringWithFormat:@"%@", object[@"B"]];                     break;                  case 3:                     label.text = [NSString stringWithFormat:@"%@", object[@"C"]];                     break;                  case 4:                     label.text = [NSString stringWithFormat:@"%@", object[@"D"]];             }         }     }]; } 

I really don't like the design of using the tag to work out what to display. It's just bad.

You'd be better doing something like setting an enum property or something based on which button is pressed. Something like that anyway.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!