如何从Parse同时检索多个数据? (Obj-C) [英] How to retrieve multiple data at the same time from Parse? (Obj-C)

查看:130
本文介绍了如何从Parse同时检索多个数据? (Obj-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已澄清我先前的问题。 如何检索多个数据同时? (Parse DB,iOS)



您好,我是Parse和数据库的新用户,我在这里苦苦挣扎。



在视图上,有4个按钮和一个标签。



基本上我想做的是首先检索列 @A(同时显示标签上的数据),然后检索同一行的属性。
当我按任何其他按钮,他们应该显示与 @A


$在同一行中查询的属性b $ b

但是目前这个代码每次按下按钮时都运行一个查询,所以每四个是一个发送不同的随机数据到视图。



此外,日志执行错误:警告:在主线程上正在执行长时间运行的解析操作。打破warnParseOperationOnMainThread()调试。



如果有人能帮我解决这个问题,我真的很高兴。 >

  PFQuery * query = [PFQuery queryWithClassName:@DataClass]; 
//随机获取数据(对象)
int count = [query countObjects];
int randomCount = arc4random()%count;
query.skip = randomCount;

[query getFirstObjectInBackgroundWithBlock:^(PFObject * object,NSError * error){

if(!error){

//检索数据。 ..但异步!
NSString * getTheStr1 = object [@A];
NSString * getTheStr2 = object [@B];
NSString * getTheStr3 = object [@C];
NSString * getTheStr4 = object [@D];

//当我按下按钮时,每个标签上都显示不同的文本。
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];
}
}
}];


解决方案

$ c> [query countOfObjects];



对象计数是一个同步函数,去分析服务器获取信息。也就是说这是一个在主线程上长时间运行的进程。



您应该使用函数...

  [query countObjectsInBackgroundWithBlock ... 

然后在完成块可以用计数信息做一些事情。



生成随机数,然后运行您的查找第一个函数。



编辑以显示如何完成此操作

   - (PFQuery *)query 
{
PFQuery * query = [PFQuery queryWithClassName:@DataClass];

return query;
}

//将标签的按钮放在这里。
//它不应该在这里。
//使用标签从来没有办法。
- (void)getRandomSkipForQuery:(UIButton *)button
{
PFQuery query = [self query];

[query countObjectsInBackgroundWithBlock:^(NSInteger count,NSError * error){
//检查错误...
//不使用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;

[查询getFirstObjectInBackgroundWithBlock:^(PFObject * object,NSError * error){
if(!error){
//哎这是真的坏设计
/ /当我按下一个按钮时,每个都会在标签上显示不同的文字。
switch([button tag]){
case 1:
//变量以小写字母开头
label.text = [NSString stringWithFormat:@%@,object [@一个]];
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 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];
        }
    }
}];

解决方案

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.

这篇关于如何从Parse同时检索多个数据? (Obj-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆