如何从Parse同步加载数据?

时间:2014-08-04 16:34:35

标签: c# parse-platform pfquery

PFQuery的专业人士?我遇到了问题。

查询不遵循我编写的代码顺序,比如在我的Xcode文件中,顺序是query1,query2,query3,然后是query4。但是,当代码执行时,查询内容的内容不会遵循1,2,3,4顺序。这真的给我带来了麻烦。请帮忙。这是我的代码:

-(void)viewWillAppear:(BOOL)animated{

 SAC_rateSum=0;
 SAC_rateNumbers=0;

NSMutableArray *SAC_CG_rateNumber = [NSMutableArray array];

PFQuery * SAC_CG_rateAQuery = [PFQuery queryWithClassName:@"SAC_CG_Rate"];

[SAC_CG_rateAQuery selectKeys:@[@"rate"]];
[SAC_CG_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_CG_rateNumber addObject:[object objectForKey:@"rate"]];
    }

    for (NSNumber *n in SAC_CG_rateNumber) {

        SAC_rateSum +=[n doubleValue];

    }

    SAC_rateNumbers+=SAC_CG_rateNumber.count;
    NSLog(@"CG:%.0f",SAC_rateNumbers);

    }];

NSMutableArray *SAC_MP_rateNumber = [NSMutableArray array];

PFQuery * SAC_MP_rateAQuery = [PFQuery queryWithClassName:@"SAC_MP_Rate"];

[SAC_MP_rateAQuery selectKeys:@[@"rate"]];
[SAC_MP_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_MP_rateNumber addObject:[object objectForKey:@"rate"]];

    }
    for (NSNumber *n in SAC_MP_rateNumber) {

        SAC_rateSum +=[n doubleValue];
    }

   SAC_rateNumbers+=SAC_MP_rateNumber.count;
    NSLog(@"MP:%.0f",SAC_rateNumbers);
}];





NSMutableArray *SAC_NS_rateNumber = [NSMutableArray array];

PFQuery * SAC_NS_rateAQuery = [PFQuery queryWithClassName:@"SAC_NS_Rate"];

[SAC_NS_rateAQuery selectKeys:@[@"rate"]];
[SAC_NS_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_NS_rateNumber addObject:[object objectForKey:@"rate"]];

        }

    for (NSNumber *n in SAC_NS_rateNumber) {

        SAC_rateSum +=[n doubleValue];
    }

   SAC_rateNumbers += SAC_NS_rateNumber.count;

    NSLog(@"NS:%.0f",SAC_rateNumbers);


}];
NSMutableArray *SAC_SB_rateNumber = [NSMutableArray array];

PFQuery * SAC_SB_rateAQuery = [PFQuery queryWithClassName:@"SAC_SB_Rate"];

[SAC_SB_rateAQuery selectKeys:@[@"rate"]];
[SAC_SB_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_SB_rateNumber addObject:[object objectForKey:@"rate"]];

    }
    for (NSNumber *n in SAC_SB_rateNumber) {

        SAC_rateSum +=[n doubleValue];
    }

    SAC_rateNumbers+=SAC_SB_rateNumber.count;
    NSLog(@"SB:%.0f",SAC_rateNumbers);
}];


NSMutableArray *SAC_WiU_rateNumber = [NSMutableArray array];

PFQuery * SAC_WiU_rateAQuery = [PFQuery queryWithClassName:@"SAC_WiU_Rate"];

[SAC_WiU_rateAQuery selectKeys:@[@"rate"]];
[SAC_WiU_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_WiU_rateNumber addObject:[object objectForKey:@"rate"]];
    }

    for (NSNumber *n in SAC_WiU_rateNumber) {

        SAC_rateSum+=[n doubleValue];
    }

   SAC_rateNumbers+=SAC_WiU_rateNumber.count;
    NSLog(@"WIU:%.0f",SAC_rateNumbers);
}];


        NSMutableArray *SAC_WS_rateNumber = [NSMutableArray array];

PFQuery * SAC_WS_rateAQuery = [PFQuery queryWithClassName:@"SAC_WS_Rate"];

[SAC_WS_rateAQuery selectKeys:@[@"rate"]];
[SAC_WS_rateAQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {

        [SAC_WS_rateNumber addObject:[object objectForKey:@"rate"]];
              }

    for (NSNumber *n in SAC_WS_rateNumber) {

       SAC_rateSum +=[n doubleValue];
    }


    SAC_rateNumbers+=SAC_WS_rateNumber.count;
    NSLog(@"WS:%.0f",SAC_rateNumbers);
    self.dininghallAscoreLabel.text=[NSString stringWithFormat:@"         %.1f", SAC_rateSum/SAC_rateNumbers];
    self.dininghallAratesLabel.text=[NSString stringWithFormat:@"%.0f rates",SAC_rateNumbers];
    NSLog(@"%.1f",SAC_rateSum);
    NSLog(@"TOTAL: %.0f",SAC_rateNumbers);

}];





   }

非常感谢!

1 个答案:

答案 0 :(得分:0)

也许最好用一个例子回答你的问题:

如果我准备4道菜的食物:鸡蛋,牛排,炒菜和米饭

然后我把它们一个接一个地放在炉子上,为什么不按顺序完成烹饪呢?

当然,从示例中很容易看出每道菜需要一段时间才能烹饪,而且不同的东西不能同时完成...... async 也是如此代码!

当你打电话给findObjectsInBackgroundWithBlock:时,就像是说“把它放在厨房里,当它完成时,用结果(块部分)”做这个。“

当您的查询完成时,块中的代码稍后运行。每次订单都不一样,并且由于网络问题,其中一个或多个订单实际上可能会失败。