ビューがロードされたときにparse.comからフェッチされたいくつかのプロパティを初期化して、それらを使用して計算できるようにしようとしています。たとえば、ヘッダー ファイルで次のように宣言します。
TaskViewController.h
@property (nonatomic, assign) int taskTotalCount;
@property (nonatomic, assign) int taskCompletedCount;
@property (nonatomic, assign) int progressCount;
- (void)CountAndSetTotalTask;
- (void)CountAndSetCompletedCount;
- (void)CalculateProgress;
次に、実装では、他のすべての初期化が適切にセットアップされ、それらが viewdidload で呼び出されると仮定して、メソッドの実装を以下に示します。
TaskViewController.m
- (void)CountAndSetCompletedCount {
// Query the tasks objects that are marked completed and count them
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"Goal" equalTo:self.tasks];
[query whereKey:@"completed" equalTo:[NSNumber numberWithBool:YES]];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
if (!error) {
// The count request succeeded. Assign it to taskCompletedCount
self.taskCompletedCount = count;
NSLog(@"total completed tasks for this goal = %d", self.taskCompletedCount);
} else {
NSLog(@"Fail to retrieve task count");
}
}];
}
- (void)CountAndSetTotalTask {
// Count the number of total tasks for this goal
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"Goal" equalTo:self.tasks];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
if (!error) {
// The count request succeeded. Assign it to taskTotalCount
self.taskTotalCount = count;
NSLog(@"total tasks for this goal = %d", self.taskTotalCount);
} else {
NSLog(@"Fail to retrieve task count");
}
}];
}
- (void)CalculateProgress {
int x = self.taskCompletedCount;
int y = self.taskTotalCount;
NSLog(@"the x value is %d", self.taskCompletedCount);
NSLog(@"the y value is %d", self.taskTotalCount);
if (!y==0) {
self.progressCount = ceil(x/y);
} else {
NSLog(@"one number is 0");
}
NSLog(@"The progress count is = %d", self.progressCount);
}
私が遭遇している問題は、taskTotalCount と taskCompletedCount が正しく設定されていて、最初の 2 つのメソッドで異なる数値を返し、NSLog が x と y の両方に対して 0 を返すことです。したがって、2 つのプロパティが設定される前に 3 番目のメソッドがロードされたのか、それとも他の問題なのかはわかりません。ご指摘ありがとうございます。