如何在查询之外使用pfobject的值 [英] How to use the value of a pfobject outside of the query

查看:106
本文介绍了如何在查询之外使用pfobject的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次开发人员,我从目标C开始并进行解析.我了解如何将数据发布到pfclass,了解如何在pfquery中检索数据,但是我不清楚的是如何从查询中获取数据并在应用程序的其他位置使用它.

I'm a first time developer and I'm starting out in objective C and parse. I understand how to post data to a pfclass, I understand how to retrieve the data in a pfquery, but what I am unclear on is how to take that data from a query and use it elsewhere in the app.

我使用的示例与我正在创建的调查应用程序有关.

The example I am using is related to a survey app I am creating.

当用户选择调查答案时

应用程序将用户选择的类别,问题和答案发布到我的第一 pfclass中.

The app posts the category, the question, and the answer that was chosen by the user to my first pfclass.

NSString *quizString = [NSString stringWithFormat:@"%i",CategoryLoaded];
NSString *questionString = [NSString stringWithFormat:@"%i",QuestionSelected];
NSString *answerString = [NSString stringWithFormat:@"b"];

PFObject *newAnswerVote = [PFObject objectWithClassName:@"QuizData"];
newAnswerVote[@"quiz"] = quizString;
newAnswerVote[@"question"] = questionString;
newAnswerVote[@"answer"] = answerString;

[newAnswerVote saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded == YES){
    }
    else{
    }
}

然后,我进入我的第二类,查询该用户选择的确切结果,并向其计数中添加一个额外的增量.这样,我记录了所有用户选择此答案的次数.

Then I go to my second class, and query the exact result this user has selected and add an additional increment to it's tally. This way I have a record of how many times this answer has been chosen by all users.

PFQuery *query = [PFQuery queryWithClassName:@"AnswerStorage"];
[query whereKey:@"Quiz" equalTo: [NSString stringWithFormat:@"%i",CategoryLoaded]];
[query whereKey:@"Question" equalTo: [NSString stringWithFormat:@"%i",QuestionSelected]];
[query whereKey:@"Answer" equalTo: [NSString stringWithFormat:@"b"]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *Total, NSError *error){
    [Total incrementKey:@"Total"];

    [Total saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded == YES){
        }
        else{
        }; 

在这里我被困住了.我需要的是该应用程序可以确定此特定问题和调查中4个可能答案中每个答案的总票数.然后,我希望该应用程序确定哪个答案的投票最多,然后根据用户是否选择了投票最多的答案而得出两个单独的结果.

Here's where I am stuck. What I need is for the app to determine the total votes for each of 4 possible answers in this specific question and survey. Then I want the app to determine which answer has the most votes, and then have two separate outcomes based on whether the user picked the most voted answer or not.

现在,我不是要寻找任何一个人来为我用代码拼写所有这些功能,但是如果您可以帮助我理解我如何获取PFQuery的结果并将其转换为应用可以执行的整数值了解查询完成后,我想我可以弄清楚其余部分.

Now, I'm not looking for any one to spell all those functions out in code for me, but if you could help me understand how I take the result of a PFQuery and turn it into an integer value that the app can understand once the query is complete I think I can get the rest of it figured out.

感谢您的帮助!

编辑-----------

一切都解决了.谢谢!

推荐答案

如果我理解的正确,那么您必须执行以下操作:
Total的值增加后,您必须找到具有最高Total的答案. 您可以使用类似以下内容:

If I understand you right, you had to do the following:
After the value of Total has been incremented, you had to find the answer with the highest Total. You could use something like:

PFQuery *query = [PFQuery queryWithClassName:@"AnswerStorage"];
[query whereKey:@"Quiz" equalTo: [NSString stringWithFormat:@"%i",CategoryLoaded]];
[query whereKey:@"Question" equalTo: [NSString stringWithFormat:@"%i",QuestionSelected]];
[query whereKey:@"Answer" equalTo: [NSString stringWithFormat:@"b"]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *total, NSError *error){
    [total incrementKey:@"Total"];
    NSUInteger myVote = total[@"Total"];

    [total saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded == YES){
            PFQuery *queryForHighestVote = [PFQuery queryWithClassName:@"AnswerStorage"];
            [queryForHighestVote orderByDescending:@"Total"];
            [queryForHighestVote getFirstObjectInBackgroundWithBlock:^(PFObject *firstAnswerStorageObject, NSError *error){
                if (nil != error) {
                    // query failed
                } else {
                    NSUInteger highestVote = firstAnswerStorageObject[@"Total"];
                    if (myVote == highestVote) {
                    } else {
                    }
                }
        }
        else {
            // Increment not saved
        } 

PS:我将Total更改为total,因为变量通常以LC字符开头,而类以UC字符开头.

PS: I changed Total to total, since variables usually start with LC characters, while classes start with UC characters.

这篇关于如何在查询之外使用pfobject的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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