我从Parse返回2个不同的查询... 1表示likeCount,1表示dislikeCount。 当我查询每个并将它们设置为等于它们各自的标签时,它们是正确的。我现在试图通过将likeCount除以likeCount + dislikeCount来获得一定比例的喜欢。
当按下按钮并初始化push segue时,将运行查询。有时这会记录正确的数字(通常是第一次)。但是,通常在第一次运行查询之后,一个或两个数字值都不会更新,并输出标签初始文本。
@property (nonatomic, assign) float likeCount;
@property (nonatomic, assign) float dislikeCount;
- (void)queryForUserActivity
{
//Create an Array to check either user photo
NSArray *userPhotos = @[self.firstUserPhoto, self.secondUserPhoto];
PFQuery *queryForLikes = [PFQuery queryWithClassName:kDDActivityClassKey];
[queryForLikes whereKey:kDDActivityTypeKey equalTo:kDDActivityTypeLikeKey];
[queryForLikes whereKey:kDDActivityFirstUserPhotoKey containedIn:userPhotos];
[queryForLikes whereKey:kDDActivitySecondUserPhotoKey containedIn:userPhotos];
[queryForLikes countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
self.totalLikesLabel.text = [NSString stringWithFormat:@"%i", number];
}];
PFQuery *queryForDislikes = [PFQuery queryWithClassName:kDDActivityClassKey];
[queryForDislikes whereKey:kDDActivityTypeKey equalTo:kDDActivityTypeDislikeKey];
[queryForDislikes whereKey:kDDActivityFirstUserPhotoKey containedIn:userPhotos];
[queryForDislikes whereKey:kDDActivitySecondUserPhotoKey containedIn:userPhotos];
[queryForDislikes countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
self.totalDislikesLabel.text = [NSString stringWithFormat:@"%i", number];
}];
PFQuery *queryForApprovalPercent = [PFQuery orQueryWithSubqueries:@[queryForLikes, queryForDislikes]];
[queryForApprovalPercent countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
if (!error) {
self.likeCount = [self.totalLikesLabel.text floatValue];
self.dislikeCount = [self.totalDislikesLabel.text floatValue];
NSLog(@"%f", self.likeCount);
NSLog(@"%f", self.dislikeCount);
float approvalPercentage = (self.likeCount / (self.likeCount + self.dislikeCount)) * 100.0;
self.approvalPercentageLabel.text = [NSString stringWithFormat:@"%2.0f%%", approvalPercentage];
if (approvalPercentage < 5) self.approvalPercentageLabel.textColor = [UIColor redColor];
}
}];
}