I am trying to refactor my code so that it follows good OO pattern, which is reusability and less repetition.. I have the following code:
PFQuery * query = [PFQuery queryWithClassName:@"Vote"];
query.cachePolicy = kPFCachePolicyNetworkElseCache;
[query whereKey:@"poll" equalTo:self.vote];
[query whereKey:@"note" notEqualTo:nil];
[query countObjectsInBackgroundWithBlock:^(int number, NSError * error) {
note = number;
[self.noteCount addSubview:self.generateCountLabel];
if (note > 0){
[self.note setUserInteractionEnabled:YES];
[self.note setEnabled:YES];
}
}];
PFQuery * query1 = [PFQuery queryWithClassName:@"Vote"];
query1.cachePolicy = kPFCachePolicyNetworkElseCache;
[query1 whereKey:@"poll" equalTo:self.vote];
[query1 whereKey:@"image" notEqualTo:nil];
[query countObjectsInBackgroundWithBlock:^(int number, NSError * error) {
pic = number;
[self.picCount addSubview:self.generateCountLabel];
if (pic > 0){
[self.pic setUserInteractionEnabled:YES];
[self.pic setEnabled:YES];
}
}];
Found it really difficult to partition into one method without doing a lot of if’s. Any idea?
You can have the following method….
Then you can call…
If you firm up your naming you can use dynamic object selectors, i.e. for the ‘note’ one you have @”note”, self.note and self.noteCount so you could take the @”note” and select both the note and noteCount views dynamically (as they both start with ‘note’), however this wouldn’t work for your image one as you have @”image” but then self.pic and self.picCount.
UPDATE
Rename image to pic and then expose the four views (picCount, pic, note and noteCount) as properties. Then you can use something like this…
You will have to play around with it as I don’t have your full class definition to get it exactly right.